आप फेसबुक के 3 डी टच में के रूप में एक ही परिणाम हासिल करना चाहते हैं की सुविधा आप अपने खुद के 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
}
}
बनाने की जरूरत है और अब आप viewDidLoad
विधि में अपने ViewController
में इस कदम को जोड़ सकते हैं
override func viewDidLoad() {
super.viewDidLoad()
let gesture = ForceTouchGestureRecognizer(target: self, action: #selector(imagePressed(sender:)))
self.view.addGestureRecognizer(gesture)
}
अब आप स्टोरीबोर्ड में अपने कंट्रोलर यूआई का प्रबंधन कर सकते हैं। cover view
UICollectionView
और UIImageView
से ऊपर एक केंद्र में जोड़ें और इसे कोड में IBOutlets
से कनेक्ट करें।
अब आप इशारा के लिए हैंडलर विधियां जोड़ सकते हैं
समारोह imagePressed (इस: ForceTouchGestureRecognizer) {
let location = sender.location(in: self.view)
guard let indexPath = collectionView?.indexPathForItem(
at: location) else { return }
देना छवि = 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
}
}
इस बिंदु से आपको अपने दृश्य को अनुकूलित करने की आवश्यकता है यह वैसे ही दिखता है जैसे फेसबुक करता है।
इसके अलावा, मैं प्रदर्शित करने के लिए यह
बस इस बाहर, महान विचार का परीक्षण किया लेकिन 'UICollectionView', यह स्क्रॉल अक्षम साथ अच्छी तरह से काम नहीं करता है और – kye
आप ठीक कह रहे हैं कार्यों नल GitHub https://github.com/ChernyshenkoTaras/3DTouchExample पर छोटा सा उदाहरण प्रोजेक्ट बनाया। मैंने गिटहब पर जवाब दिया है और कोड तय कर लिया है। अब स्क्रॉलिंग तय की गई है, लेकिन आपको 'imagePressed:' फ़ंक्शन में संग्रहित दृश्य आइटम को संभालने की आवश्यकता है –