我希望在以下条件下有一个触发其方法的按钮:
基本上,只要触摸按钮的任何部分,无论触摸的来源如何,我都希望触发该方法,但希望通过操纵UIButton属性来完成此操作,例如touchedUpInside
等。
感谢您的建议!
答案 0 :(得分:3)
关于:
- 当用户按下按钮并将他/她的手指拖出按钮的区域时
我最近做过类似的事情。这是我在 Swift 中的代码。
(在此示例中,您将UIButton子类化,如: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
}
}
}
...
我希望它有所帮助。
答案 1 :(得分:2)
制作-(IBAction)btnTap
等方法并连接到这些属性
Touch Down
方法Touch Up Inside
Touch Drag Inside
答案 2 :(得分:0)
您需要将以下UIControlEvents添加到按钮:
UIControlEventTouchUpInside
UIControlEventTouchDragOutside
UIControlEventTouchDragInside
就是这样
[self.myButton addTarget:self action:@selector(dragMoving:withEvent: )
forControlEvents: UIControlEventTouchDragInside | UIControlEventTouchDragOutside];
[self.myButton addTarget:self action:@selector(dragEnded:withEvent: )
forControlEvents: UIControlEventTouchUpInside |
UIControlEventTouchUpOutside];
[self.myButton addTarget:self action:@selector(myButtonTouchDown:withEvent: )
forControlEvents: UIControlEventTouchDown];