问题描述
我想要一个在以下条件下触发其方法的按钮:
- 当用户点击按钮时
- 当用户按下按钮并将他/她的手指拖出按钮区域时
- 当用户将手指从按钮区域外拖到按钮区域内时
基本上,无论何时触摸按钮的任何部分,无论触摸的来源如何,我都希望触发该方法,但希望通过操作 UIButton 属性来完成此操作,如 感谢您的建议! 关于: 我最近做了类似的事情.这是我在 Swift 中的代码. (在本例中,您继承了 UIButton,例如:touchedUpInside 等.p>
class FadingButton: UIButton
)<代码>...//用户点击按钮,然后移动手指.覆盖 func touchesMoved(_ touches: Set
我希望它对某人有所帮助.
I want to have a button that triggers its method under the following conditions:
- When the user taps the button
- When the user presses the button and drags his/her finger out of the button's area
- When the user drags his/her finger from outside the button's area to inside the button's area
Basically, anytime any part of the button is touched, regardless of origin of touch, I want the method triggered, but want to accomplish this by manipulating UIButton properties, like touchedUpInside
and such.
Thanks for the suggestions!
Regarding:
- When the user presses the button and drags his/her finger out of the button's area
I recently have done something similar. Here is my code in Swift.
(In this example you subclassed UIButton, like: class FadingButton: UIButton
)
...
// The user tapped the button and then moved the finger around.
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
// Get the point to which the finger moved
if let point: CGPoint = touches.first?.location(in: self) {
// If the finger was dragged outside of the button...
if ((point.x < 0 || point.x > bounds.width) ||
(point.y < 0 || point.y > bounds.height)) {
// Do whatever you need here
}
}
}
...
I hope it helps someone.
这篇关于当用户将手指拖入按钮区域时触发 UIButton 的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!