Swift   发布时间:2022-04-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了swift – UISwipeGestureRecognizer无法识别视图外部启动的滑动手势大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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 UIKit

class 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无法识别视图外部启动的滑动手势所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。