首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >UIButton touchDragEnter和touchDragExit来电太频繁

UIButton touchDragEnter和touchDragExit来电太频繁
EN

Stack Overflow用户
提问于 2018-05-06 11:35:23
回答 1查看 146关注 0票数 0

如何避免UIButtons .touchDragEnter和.touchDragExit函数被快速触发?This question demonstrates the issue perfectly,但唯一的答案没有描述如何解决它。我试着在用户手指放在按钮上时对按钮进行动画处理,当手指滑出时再对其进行动画处理。有没有更好的方法呢?如果不是,当用户的手指正好处于.enter和.exit状态之间时,我应该如何阻止动画代码多次触发?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-11 06:24:34

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

代码语言:javascript
复制
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:

代码语言:javascript
复制
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
        }
    }

这需要你继承按钮的superview子类。由于您可能不希望在指针离开按钮的框架时立即显示动画(因为用户的手指或拇指仍将覆盖按钮的大部分),因此您可以在封装按钮的较大框架中进行点击测试。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50196024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档