2017-06-21 9 views
7

के रूप में दिखाया जा रहा है, जबकि UIViewController से स्पर्श का स्थान प्राप्त करना संभव है, जिसे वर्तमान में previewingContext 3 डी टच के लिए नियंत्रक के रूप में उपयोग किया जा रहा है? (जब स्पर्श बाएं से दाएं स्थानांतरित होता है तो मैं पूर्वावलोकन नियंत्रक के भीतर की छवि को बदलना चाहता हूं)UIViewController को 3DTouch पूर्वावलोकन

मैंने touchesBegan और touchesMoved दोनों को निकाला है, उनमें से कोई भी निकाल दिया नहीं गया है।

class ThreeDTouchPreviewController: UIViewController { 

func getLocationFromTouch(touches: Set<UITouch>) -> CGPoint?{ 
     guard let touch = touches.first else { return nil } 
     return touch.location(in: self.view) 
    } 
    //Not fired 
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 

     let location = getLocationFromTouch(touches: touches) 
     print("LOCATION", location) 
    } 
    //Not fired 
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
     let location = getLocationFromTouch(touches: touches) 
     print("LOCATION", location) 
    } 
} 

यहां तक ​​कि एक यूआईपीएन जेस्चर जोड़ने की कोशिश की।

फेसबुक की 3 डी टच सुविधा को दोहराने का प्रयास जहां उपयोगकर्ता वर्तमान छवि को प्रदर्शित करने के लिए बाएं से दाएं ओर उंगली ले जा सकता है। संदर्भ के लिए वीडियो: https://streamable.com/ilnln

उत्तर

6

आप फेसबुक के 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 viewUICollectionView और 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 
} 

}

इस बिंदु से आपको अपने दृश्य को अनुकूलित करने की आवश्यकता है यह वैसे ही दिखता है जैसे फेसबुक करता है।

इसके अलावा, मैं प्रदर्शित करने के लिए यह

+0

बस इस बाहर, महान विचार का परीक्षण किया लेकिन 'UICollectionView', यह स्क्रॉल अक्षम साथ अच्छी तरह से काम नहीं करता है और – kye

+0

आप ठीक कह रहे हैं कार्यों नल GitHub https://github.com/ChernyshenkoTaras/3DTouchExample पर छोटा सा उदाहरण प्रोजेक्ट बनाया। मैंने गिटहब पर जवाब दिया है और कोड तय कर लिया है। अब स्क्रॉलिंग तय की गई है, लेकिन आपको 'imagePressed:' फ़ंक्शन में संग्रहित दृश्य आइटम को संभालने की आवश्यकता है –

संबंधित मुद्दे