[英]How to detect touch end event for UIButton?
I want to handle an event occurs when UIButton touch is ended. 我想处理UIButton touch结束时发生的事件。 I know that UIControl has some events implementing touches (UIControlEventTouchDown, UIControlEventTouchCancel, etc.). 我知道UIControl有一些实现触摸的事件(UIControlEventTouchDown,UIControlEventTouchCancel等)。 But I can't catch any of them except UIControlEventTouchDown and UIControlEventTouchUpInside . 但除了UIControlEventTouchDown和UIControlEventTouchUpInside,我无法捕获它们。
My button is a subview of some UIView. 我的按钮是一些UIView的子视图。 That UIView has userInteractionEnabled property set to YES . UIView将userInteractionEnabled属性设置为YES 。
What's wrong? 怎么了?
You can set 'action targets' for your button according to the ControlEvents
您可以根据ControlEvents
为按钮设置“操作目标”
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
Example: 例:
[yourButton addTarget:self
action:@selector(methodTouchDown:)
forControlEvents:UIControlEventTouchDown];
[yourButton addTarget:self
action:@selector(methodTouchUpInside:)
forControlEvents: UIControlEventTouchUpInside];
-(void)methodTouchDown:(id)sender{
NSLog(@"TouchDown");
}
-(void)methodTouchUpInside:(id)sender{
NSLog(@"TouchUpInside");
}
You need to create your own custom class that extends UIButton
. 您需要创建自己的扩展UIButton
的自定义类。 your header file should look like this. 你的头文件应该是这样的。
@interface customButton : UIButton
{
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
then make your implementation file 然后制作你的实施文件
i think it is more easy 我认为这更容易
UILongPressGestureRecognizer *longPressOnButton = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressOnButton:)];
longPressOnButton.delegate = self;
btn.userInteractionEnabled = YES;
[btn addGestureRecognizer:longPressOnButton];
- (void)longPressOnButton:(UILongPressGestureRecognizer*)gesture
{
// When you start touch the button
if (gesture.state == UIGestureRecognizerStateBegan)
{
//start recording
}
// When you stop touch the button
if (gesture.state == UIGestureRecognizerStateEnded)
{
//end recording
}
}
Just add IBOutlets for UIButton with Events TouchDown:
and Primary Action Triggered:
只需为UIButton添加带有事件TouchDown:
IBOutlets TouchDown:
并Triggered:
主要操作Triggered:
- (IBAction)touchDown:(id)sender {
NSLog(@"This will trigger when button is Touched");
}
- (IBAction)primaryActionTriggered:(id)sender {
NSLog(@"This will trigger Only when touch end within Button Boundary (not Frame)");
}
@Ramshad's accepted answer in Swift 3.0 syntax using following method of UIControl class @Ramshad使用以下UIControl类方法在Swift 3.0语法中接受了答案
open func addTarget(_ target: Any?, action: Selector, for controlEvents: UIControlEvents)
Example: 例:
myButton.addTarget(self, action: #selector(MyViewController.touchDownEvent), for: .touchDown)
myButton.addTarget(self, action: #selector(MyViewController.touchUpEvent), for: [.touchUpInside, .touchUpOutside])
func touchDownEvent(_ sender: AnyObject) {
print("TouchDown")
}
func touchUpEvent(_ sender: AnyObject) {
print("TouchUp")
}
Swift 3.0 version: Swift 3.0版本:
let btn = UIButton(...)
btn.addTarget(self, action: #selector(MyView.onTap(_:)), for: .touchUpInside)
func onTap(_ sender: AnyObject) -> Void {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.