首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在UIViewController显示为3DTouch预览时检测UITouches

在UIViewController显示为3DTouch预览时检测UITouches
EN

Stack Overflow用户
提问于 2017-06-21 11:37:47
回答 1查看 368关注 0票数 7

是否可以从当前用作3D触摸的previewingContext视图控制器的UIViewController检测触摸并获得触摸的位置?(当触摸从左向右移动时,我希望更改预览控制器中的图像)

我已经试过touchesBegantouchesMoved了,他们都没被解雇。

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-30 20:07:11

如果你想获得和FaceBook 3D Touch功能一样的效果,你需要创建你自己的3D Touch gesture class

代码语言:javascript
复制
    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中添加此手势

代码语言:javascript
复制
override func viewDidLoad() {
    super.viewDidLoad()
    let gesture = ForceTouchGestureRecognizer(target: self, action: #selector(imagePressed(sender:)))
    self.view.addGestureRecognizer(gesture)
}

现在,您可以在故事板中管理控制器UI。在UICollectionView上添加cover view,在中间添加UIImageView,并在代码中将其与IBOutlets连接。

现在,您可以为手势添加处理程序方法

ForceTouchGestureRecognizer imagePressed(发送方: func ){

代码语言:javascript
复制
let location = sender.location(in: self.view)

guard let indexPath = collectionView?.indexPathForItem(
    at: location) else { return }

让图像= self.imagesindexPath.row

代码语言:javascript
复制
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上创建了一个小示例项目来演示它

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44666454

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档