ios 如何检测 UIButton 的触摸结束事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15667746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect touch end event for UIButton?
提问by Brain89
I want to handle an event occurs when UIButtontouch is ended. I know that UIControl has some events implementing touches (UIControlEventTouchDown, UIControlEventTouchCancel, etc.). But I can't catch any of them except UIControlEventTouchDownand UIControlEventTouchUpInside.
我想处理UIButton触摸结束时发生的事件。我知道 UIControl 有一些实现触摸的事件(UIControlEventTouchDown、UIControlEventTouchCancel 等)。但除了UIControlEventTouchDown和UIControlEventTouchUpInside之外,我无法捕捉到它们中的任何一个。
My button is a subview of some UIView. That UIView has userInteractionEnabledproperty set to YES.
我的按钮是一些 UIView 的子视图。该 UIView 将userInteractionEnabled属性设置为YES。
What's wrong?
怎么了?
回答by Shamsudheen TK
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");
}
回答by Alex
You need to create your own custom class that extends UIButton
.
your header file should look like this.
您需要创建自己的自定义类来扩展UIButton
. 你的头文件应该是这样的。
@interface customButton : UIButton
{
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
then make your implementation file
然后制作你的实现文件
回答by Maverick
@Ramshad's accepted answerin Swift 3.0syntax using following method of UIControlclass
@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")
}
回答by Bary Levy
Swift 3.0 version:
斯威夫特 3.0 版本:
let btn = UIButton(...)
btn.addTarget(self, action: #selector(MyView.onTap(_:)), for: .touchUpInside)
func onTap(_ sender: AnyObject) -> Void {
}
回答by Ahmad Labeeb
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
}
}
回答by Lal Krishna
Just add IBOutlets for UIButton with Events TouchDown:
and Primary Action Triggered:
只需为带有事件TouchDown:
和主要操作的UIButton 添加 IBOutletsTriggered:
- (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)");
}