Nếu bạn muốn đạt được kết quả tương tự như trong không gian 3D cảm ứng FaceBook của tính năng mà bạn cần để tạo của riêng bạn 3D Touch gesture class
import UIKit.UIGestureRecognizerSubclass
class ForceTouchGestureRecognizer: UIGestureRecognizer {
var forceValue: CGFloat = 0
var isForceTouch: Bool = false
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesBegan(touches, with: event)
handleForceWithTouches(touches: touches)
state = .began
self.isForceTouch = false
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesMoved(touches, with: event)
handleForceWithTouches(touches: touches)
if self.forceValue > 6.0 {
state = .changed
self.isForceTouch = true
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesEnded(touches, with: event)
state = .ended
handleForceWithTouches(touches: touches)
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesCancelled(touches, with: event)
state = .cancelled
handleForceWithTouches(touches: touches)
}
func handleForceWithTouches(touches: Set<UITouch>) {
if touches.count != 1 {
state = .failed
return
}
guard let touch = touches.first else {
state = .failed
return
}
forceValue = touch.force
}
}
và bây giờ bạn có thể thêm cử chỉ này trong ViewController
của bạn trong viewDidLoad
phương pháp
override func viewDidLoad() {
super.viewDidLoad()
let gesture = ForceTouchGestureRecognizer(target: self, action: #selector(imagePressed(sender:)))
self.view.addGestureRecognizer(gesture)
}
Bây giờ bạn có thể quản lý giao diện người dùng bộ điều khiển trong Bảng phân cảnh. Thêm cover view
trên UICollectionView
và UIImageView
ở giữa và kết nối với mã IBOutlets
.
Bây giờ bạn có thể thêm các phương pháp xử lý đối với cử chỉ
func imagePressed (sender: ForceTouchGestureRecognizer) {
let location = sender.location(in: self.view)
guard let indexPath = collectionView?.indexPathForItem(
at: location) else { return }
let image = self.images [indexPath.row]
switch sender.state {
case .changed:
if sender.isForceTouch {
self.coverView?.isHidden = false
self.selectedImageView?.isHidden = false
self.selectedImageView?.image = image
}
case .ended:
print("force: \(sender.forceValue)")
if sender.isForceTouch {
self.coverView?.isHidden = true
self.selectedImageView?.isHidden = true
self.selectedImageView?.image = nil
} else {
//TODO: handle selecting items of UICollectionView here,
//you can refer to this SO question for more info: https://stackoverflow.com/questions/42372609/collectionview-didnt-call-didselectitematindexpath-when-superview-has-gesture
print("Did select row at indexPath: \(indexPath)")
self.collectionView?.selectItem(at: indexPath, animated: true, scrollPosition: .centeredVertically)
}
default: break
}
}
Từ thời điểm này, bạn cần tùy chỉnh chế độ xem của mình để tạo nó trông giống như cách Facebook làm.
Ngoài ra tôi tạo ra dự án ví dụ nhỏ trên GitHub https://github.com/ChernyshenkoTaras/3DTouchExample để chứng minh nó
Chỉ cần thử nghiệm điều này ra, ý tưởng tuyệt vời nhưng không làm việc tốt với 'UICollectionView', nó vô hiệu hóa di chuyển và khai thác hành động – kye
Bạn nói đúng. Tôi đã trả lời và sửa mã trên GitHub. Bây giờ di chuyển là cố định, nhưng bạn cần phải xử lý các mục collectionView chọn trong hàm 'imagePressed:' –