【问题标题】:touchesBegan and touchesMoved also called during pinchtouchesBegan 和 touchesMoved 在捏合期间也调用
【发布时间】:2020-09-28 05:24:23
【问题描述】:

我在视图中添加了捏合手势和平移手势。我也在使用 touchesBegan 和 touchesMoved 方法。令人惊讶的是,当我捏​​住我的视图时,它有时也会调用 touchesBegan 和 touchesMoved。如何在捏住时停止调用此方法?这就是我所做的。

class CustomView: UIView {
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        if let touchCount = event?.touches(for: self)?.count {
            if touchCount == 1 {
                // Doing some task
            }
        }
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesMoved(touches, with: event)
        if let touchCount = event?.touches(for: self)?.count {
            if touchCount == 1 {
                // Doing some task
            }
        }
    }
}

在我的 ViewController 中:我创建了一个 CustomView 类的 customView 对象并添加了一些手势,如下所示:

let pinch = UIPinchGestureRecognizer(target: self, action: #selector(pinch(recognizer:)))
customView.addGestureRecognizer(pinch)    
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePan(recognizer:)))
panGesture.minimumNumberOfTouches = 2
self.customView.addGestureRecognizer(panGesture)

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    touchesBegan 和touchesMoved 非常非常通用——每当你的手指触碰或移动时,它们就会被调用。这包括按钮按下以及手指可以触摸的几乎所有东西。

    因此,如果您使用手势识别器进行平移和捏合,请坚持使用手势识别器。使用UITapGestureRecognizer 而不是依赖touchesBegan,使用另一个UIPanGestureRecognizer 代替touchesMoved。

    【讨论】:

    • 但我只使用了手势。在 panGesture 中,您可以检查状态是开始、更改还是结束。
    猜你喜欢
    • 2011-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 2012-12-22
    • 1970-01-01
    相关资源
    最近更新 更多