swift – UISwipeGestureRecognizer无法识别视图外部启动的滑动手势

swift – UISwipeGestureRecognizer无法识别视图外部启动的滑动手势,第1张

概述func addSwipe() { self.isUserInteractionEnabled = true let directions: [UISwipeGestureRecognizerDirection] = [.right, .left] for direction in directions { let gesture = UISwipeGest
func addSwipe() {    self.isUserInteractionEnabled = true    let directions: [UISwipeGestureRecognizerDirection] = [.right,.left]    for direction in directions {        let gesture = UISwipeGestureRecognizer(target: self,action: #selector(VIEwCardContainer.handleSwipe(sender:)))        gesture.direction = direction        self.addGestureRecognizer(gesture)    }}@objc func handleSwipe(sender: UISwipeGestureRecognizer) {    print(sender.direction)}

我的UIVIEws:

+----------------+|                ||     +--------+ ||  V1 |  V2    | |+-----------------

我在V2中注册了UISwipeGestureRecognizer但是如果从V1开始通过V2启动滑动手势,则在V2中将无法识别滑动手势.

反正有没有让它工作?提前致谢!

解决方法 编辑:我为你做了一个工作示例项目.您可以从红色方块(v1)滑动到蓝色方块(v2),并在日志中看到检测到滑动.

https://github.com/QuantumProductions/so_46781856

import UIKitclass VIEwController: UIVIEwController {    var vIEw2: UIVIEw!    overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()        vIEw.backgroundcolor = UIcolor.black        let vIEw1 = UIVIEw(frame: CGRect(x: 0,y: 0,wIDth: 100,height: 100))        vIEw1.backgroundcolor = UIcolor.red        vIEw.addSubvIEw(vIEw1)        vIEw2 = UIVIEw(frame: CGRect(x: 100,height: 100))        vIEw2.backgroundcolor = UIcolor.blue        vIEw.addSubvIEw(vIEw2)        let swipeRecognizer = UISwipeGestureRecognizer(target: self,action: #selector(swipeDetected(_:)))        swipeRecognizer.direction = .right        vIEw.addGestureRecognizer(swipeRecognizer)    }    func swipeDetected(_ sender: UIGestureRecognizer) {        let location = sender.location(oftouch: 0,in: vIEw2)        print("Location in vIEw2")        print(location)        print("You swiped right")    }}

原答案:

这是故意的.滑动检测基于视图查看.尝试像联系人这样的Apple应用程序,你会发现从滚动视图中向上移动手指后,你无法按下导航栏中的按钮.

如果您希望触摸识别器在V1和V2中有效:

>在V1和EX的父视图中添加手势识别器. V2
>检测触摸是否在V2的矩形内(使用touch.locationInVIEw:V2)
>如果为true,请运行预期的功能

如果这是一个vIEwcontroller,你需要将识别器添加到self.vIEw(从你的例子中不清楚识别器添加到哪个视图,因为没有关于你的func所在的类的上下文)

您可能需要禁用V1和V2上的用户交互,如果它正在吃掉触摸并在父视图上保持启用状态.

总结

以上是内存溢出为你收集整理的swift – UISwipeGestureRecognizer无法识别视图外部启动的滑动手势全部内容,希望文章能够帮你解决swift – UISwipeGestureRecognizer无法识别视图外部启动的滑动手势所遇到的程序开发问题。

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

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

原文地址:https://54852.com/web/1005044.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存