【问题标题】:UIButton touchDragEnter and touchDragExit called too oftenUIButton touchDragEnter 和 touchDragExit 调用太频繁
【发布时间】:2018-05-06 03:35:23
【问题描述】:

如何避免 UIButtons .touchDragEnter 和 .touchDragExit 函数快速触发? This question demonstrates the issue perfectly,但唯一的答案没有描述如何解决它。当用户手指在按钮上时,我试图为按钮设置动画,并在他们的手指滑下时再次为其设置动画。有没有更好的方法来做到这一点?如果没有,当用户的手指在 .enter 和 .exit 状态之间时,我应该如何阻止我的动画代码多次触发?

【问题讨论】:

    标签: ios uibutton uikit core-animation uiviewanimation


    【解决方案1】:

    您可以改为跟踪触摸点本身的位置并确定触摸点何时移入和移出按钮

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let point = t.location(in: self)
            // moving in to the button
            if button.frame.contains(point) && !wasInButton {
                // trigger animation
                wasInButton = true
            }
            // moving out of the button
            if !button.frame.contains(point) && wasInButton {
                // trigger animation
                wasInButton = false
            }
        }
    }
    

    wasInButton 可以是一个布尔变量,当按钮的框架中有触摸时设置为 true:

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let point = t.location(in: self)
            if button.frame.contains(point) {
                wasInButton = true
                // trigger animation
            } else {
                wasInButton = false
            }
        }
    

    这将要求您对按钮的超级视图进行子类化。而且由于您可能不想在点离开按钮的框架后立即进行动画处理(因为用户的手指或拇指仍会覆盖大部分按钮),您可以改为在封装按钮的更大框架中进行命中测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-30
      • 1970-01-01
      • 2016-05-29
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多