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

ios – 在UIViewController显示为3DTouch预览时检测UITouches,第1张

概述是否有可能从UIViewController检测触摸并获取触摸的位置,UIViewController当前用作3D Touch的previewingContext视图控制器? (当触摸从左向右移动时,我想更改预览控制器内的图像) 我尝试了两个touchesBegan和touchesMoved都没有被解雇. class ThreeDTouchPreviewController: UIViewCont 是否有可能从UIVIEwController检测触摸并获取触摸的位置,UIVIEwController当前用作3D touch的prevIEwingContext视图控制器? (当触摸从左向右移动时,我想更改预览控制器内的图像)

我尝试了两个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)    }}

甚至尝试添加UIPanGesture.

尝试复制FaceBook的3D touch功能,用户可以从左向右移动手指以更改当前显示的图像.
上下文视频:https://streamable.com/ilnln

解决方法 如果您想获得与FaceBook的3D touch功能相同的结果,您需要创建自己的3D touch手势类
import UIKit.UIGestureRecognizerSubclassclass 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)}

现在,您可以在Storyboard中管理控制器UI.在中心添加UICollectionVIEw和UIImageVIEw上方的封面视图,并在代码中将其与IBOutlets连接.

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

func imagepressed(sender:ForcetouchGestureRecognizer){

let location = sender.location(in: self.vIEw)guard let indexPath = collectionVIEw?.indexPathForItem(    at: location) else { return }

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

}

从这一点开始,您需要自定义视图,使其外观与Facebook一样.

我还在GitHub https://github.com/ChernyshenkoTaras/3DTouchExample上创建了一个小示例项目来演示它

总结

以上是内存溢出为你收集整理的ios – 在UIViewController显示为3DTouch预览时检测UITouches全部内容,希望文章能够帮你解决ios – 在UIViewController显示为3DTouch预览时检测UITouches所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1099218.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存