• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 自定义 UIGestureRecognizer 并不总是调用它的 action 方法

[复制链接]
菜鸟教程小白 发表于 2022-12-11 20:20:49 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个自定义 UIGestureRecognizer,它可以正确识别预期的手势(两根手指的 Z 手势)并在 touchesEnded 中设置 state = .recognized。问题是即使手势被识别,它有时会调用 Action 方法,有时不会。据我所知,这种情况不确定地发生。

有人知道为什么吗?

代码如下:

import UIKit
import UIKit.UIGestureRecognizerSubclass

class ZGestureRecognizer: UIGestureRecognizer {

    private var topSwipeStartPoint = CGPoint.zero
    private var diagonalSwipeStartPoint = CGPoint.zero
    private var bottomSwipeStartPoint = CGPoint.zero
    private let minDeltaX: CGFloat = 20
    private let minDeltaY: CGFloat = 20
    private var strokePhase = StrokePhase.notStarted
    var trackedTouch: UITouch?


    enum StrokePhase {
        case notStarted
        case topSwipe
        case diagonalSwipe
        case bottomSwipe
    }


    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
        if !(touches.count == 1 || touches.count == 2) {
            state = .failed
            return
        }

        if trackedTouch == nil {
            trackedTouch = touches.min { $0.location(in: self.view?.window).x < $1.location(in: self.view?.window).x }
            strokePhase = .topSwipe
            topSwipeStartPoint = trackedTouch!.locationInWindow!
        }
    }


    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) { 
        guard let trackedTouch = trackedTouch,
            trackedTouch.phase != .cancelled,
            trackedTouch.phase != .ended
        else {
           self.state = .failed
           return
        }

        let newPoint = trackedTouch.locationInWindow!
        let previousPoint = trackedTouch.previousLocationInWindow!

        switch strokePhase {
        case .topSwipe:
            if newPoint.x < previousPoint.x { // if we have started moving back to the left
                let deltaX = previousPoint.x - topSwipeStartPoint.x

                if deltaX > minDeltaX && touches.count == 2 {
                   diagonalSwipeStartPoint = previousPoint
                   strokePhase = .diagonalSwipe
                }
                else { // too short right swipe or not 2 touches
                   state = .failed
                   return
                }
             }

        case .diagonalSwipe:
            if newPoint.x > previousPoint.x { // if we have started moving back to the right
                let deltaX = diagonalSwipeStartPoint.x - previousPoint.x
                let deltaY = previousPoint.y - diagonalSwipeStartPoint.y

                if deltaX > minDeltaX && deltaY > minDeltaY && touches.count == 2 {
                   bottomSwipeStartPoint = previousPoint
                   strokePhase = .bottomSwipe
                }
                else { // too short right swipe
                   state = .failed
                   return
                }
             }

        case .bottomSwipe:
            if newPoint.x < previousPoint.x || touches.count != 2 {
               state = .failed
               return
            }

        default: break
        }
    }


   override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent) {
      state = .failed
   }


   override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
      guard strokePhase == .bottomSwipe else {
         state = .failed
         return
      }

      let endPoint = trackedTouch!.locationInWindow!
      let bottomSwipeDeltaX = endPoint.x - bottomSwipeStartPoint.x

      if bottomSwipeDeltaX > minDeltaX {
         state = .recognized
      }
      else {
         state = .failed
      }
   }


   override func reset() {
      strokePhase = .notStarted
      trackedTouch = nil
   }

}

由于手势识别在做适当的手势时总是以 state = .recognized 行结束,我认为代码不相关,但我很乐意出错。



Best Answer-推荐答案


我已经模拟了你的代码。我无法获得正确的手势(您没有提到所需的手势)。因此,在我的情况下, state = .recognized 从未被执行。然后,我在 touchesEnded 中硬编码 state = .recognized 以检查每次设置 state = .recognized 时是否调用 Action 方法。我发现它很好。

我发现所有重写的方法都缺少 super 调用。你可以把所有的 super 电话和检查一遍。另外,把登录触摸结束。我认为可能存在逻辑问题。因为,我不知道触发它所需的手势我无法调试代码。

关于ios - 自定义 UIGestureRecognizer 并不总是调用它的 action 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51222708/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap