【问题标题】:UIButton touchDragEnter and touchDragExit called too oftenUIButton touchDragEnter 和 touchDragExit 调用太频繁
【发布时间】:2018-05-06 03:35:23
【问题描述】:
【问题讨论】:
标签:
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
}
}
这将要求您对按钮的超级视图进行子类化。而且由于您可能不想在点离开按钮的框架后立即进行动画处理(因为用户的手指或拇指仍会覆盖大部分按钮),您可以改为在封装按钮的更大框架中进行命中测试。