如何避免UIButtons .touchDragEnter和.touchDragExit函数被快速触发?This question demonstrates the issue perfectly,但唯一的答案没有描述如何解决它。我试着在用户手指放在按钮上时对按钮进行动画处理,当手指滑出时再对其进行动画处理。有没有更好的方法呢?如果不是,当用户的手指正好处于.enter和.exit状态之间时,我应该如何阻止动画代码多次触发?
发布于 2018-06-11 06:24:34
相反,您可以跟踪触摸点本身的位置并确定触摸点何时移入和移出按钮
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
}
}
这需要你继承按钮的superview子类。由于您可能不希望在指针离开按钮的框架时立即显示动画(因为用户的手指或拇指仍将覆盖按钮的大部分),因此您可以在封装按钮的较大框架中进行点击测试。
https://stackoverflow.com/questions/50196024
复制相似问题