在 UIViewController 作为 3DTouch 预览展示时检测 UITouches

7

在作为3D Touch预览上下文视图控制器使用的UIViewController中,是否可能检测到触摸并获取触摸位置?(当触摸从左向右移动时,我想更改预览控制器内的图像)

我尝试了touchesBegantouchesMoved,但它们都没有被触发。

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)
    }
}

甚至尝试添加UIPanGesture。

试图复制FaceBook的3D Touch功能,用户可以从左到右移动手指来更改当前显示的图像。 视频链接: https://streamable.com/ilnln

1个回答

6

如果你想要实现与Facebook的3D Touch功能相同的结果,你需要创建自己的3D Touch手势类

    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
    }
}

现在,您可以在ViewControllerviewDidLoad方法中添加此手势。

override func viewDidLoad() {
    super.viewDidLoad()
    let gesture = ForceTouchGestureRecognizer(target: self, action: #selector(imagePressed(sender:)))
    self.view.addGestureRecognizer(gesture)
}

现在您可以使用Storyboard管理控制器UI。在中心上方添加一个覆盖视图,并在UICollectionViewUIImageView之上,通过代码将其与IBOutlets连接起来。
现在您可以为手势添加处理程序方法。

func imagePressed(sender: 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
}

从这一点开始,您需要自定义视图以使其看起来与Facebook相同。

另外,我创建了一个小的示例项目在GitHub上https://github.com/ChernyshenkoTaras/3DTouchExample以演示它。


刚刚测试了一下,这个想法很好,但是在 UICollectionView 中效果不佳,会禁用滚动和点击操作。 - kye
你说得对。我已经在GitHub上回答并修复了代码。现在滚动已经修复了,但是你需要在imagePressed:函数中处理collectionView项目的选择。 - Taras Chernyshenko

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接