Ios 如何检测UIButton的触摸结束事件?
我想处理ui按钮触摸结束时发生的事件。我知道UIControl有一些实现触摸的事件(UIControlEventTouchDown、UIControlEventTouchCancel等)。但是除了UIControlEventTouchDown和UIControlEventTouchUpInside之外,我无法捕捉到它们中的任何一个 我的按钮是某个UIView的子视图。该UIView的userInteractionEnabled属性设置为YESIos 如何检测UIButton的触摸结束事件?,ios,cocoa-touch,uibutton,touch,Ios,Cocoa Touch,Uibutton,Touch,我想处理ui按钮触摸结束时发生的事件。我知道UIControl有一些实现触摸的事件(UIControlEventTouchDown、UIControlEventTouchCancel等)。但是除了UIControlEventTouchDown和UIControlEventTouchUpInside之外,我无法捕捉到它们中的任何一个 我的按钮是某个UIView的子视图。该UIView的userInteractionEnabled属性设置为YES 怎么了?您可以根据控制事件为按钮设置“行动目标” -
怎么了?您可以根据
控制事件为按钮设置“行动目标”
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
例如:
[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");
}
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")
}
您需要创建自己的自定义类来扩展
UIButton
。
您的头文件应该如下所示
@interface customButton : UIButton
{
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
然后制作您的实现文件我认为这更容易
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
}
}
只需为UIButton添加事件
触地:
和主要动作触发:
- (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)");
}
@拉姆沙德在Swift 3.0语法中被接受
使用以下UIControl类的方法
例如:
[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");
}
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版本:
let btn = UIButton(...)
btn.addTarget(self, action: #selector(MyView.onTap(_:)), for: .touchUpInside)
func onTap(_ sender: AnyObject) -> Void {
}
哦,效果很好。我的疏忽和我开了一个残酷的玩笑。谢谢,谢谢。这很有效。但如果用户在触摸时拖动,问题就会出现。TouchUpInside没有在那个tym期间被调用:(不幸的是,我的高级程序员禁止创建一个新类。但是这个解决方案也可以正常工作