如何在UIButton中实现两个IBActions而不重叠?

我从一个UIButton拖动2个IBAction ,一个用touchDown事件,另一个用Drag Inside拖动。

 - (IBAction)clickButton:(UIButton *)sender { NSLog(@"Click Button"); } - (IBAction)dragInsideButton:(UIButton *)sender { NSLog(@"Drag Button"); } 

但是当我拖入里面的时候,touchDown动作也被触发了。

如何在dragInside时禁用touchDown事件。

谢谢!

我已经通过使用拖动事件解决了这个问题

以.xib文件或以编程方式将事件添加到button。

编程方式是:

 [mybut addTarget:self action:@selector(dragBegan:withEvent: ) forControlEvents: UIControlEventTouchDown]; [mybut addTarget:self action:@selector(dragMoving:withEvent: ) forControlEvents: UIControlEventTouchDragInside]; [mybut addTarget:self action:@selector(dragEnded:withEvent: ) forControlEvents: UIControlEventTouchUpInside | UIControlEventTouchUpOutside]; 

那么事件的定义是:

 - (void) dragBegan: (UIButton *) c withEvent:ev { NSLog(@"dragBegan......"); count=NO;//bool Value to decide the Down Event c.tag=0; [self performSelector:@selector(DownSelected:) withObject:mybut afterDelay:0.1]; //user must begin dragging in 0.1 second else touchDownEvent happens } - (void) dragMoving: (UIButton *) c withEvent:ev { NSLog(@"dragMoving.............."); c.tag++; } - (void) dragEnded: (UIButton *) c withEvent:ev { NSLog(@"dragEnded.............."); if (c.tag>0 && !count) { NSLog(@"make drag events"); } } -(void)DownSelected:(UIButton *)c { if (c.tag==0) { NSLog(@"DownEvent"); count=YES;//count made Yes To interrupt drag event } } 

这个方法经过testing,应该做我认为你正在做的事情。 您可以更改计时器中的延迟以获得所需的效果。 我必须将3个动作连接到button才能使其工作 – 第三个动作是将系统重置为启动条件的touchUp。

 @interface LastViewController () @property (nonatomic) BOOL touchedDown; @property (strong,nonatomic) NSTimer *downTimer; @end @implementation LastViewController - (void)viewDidLoad { [super viewDidLoad]; self.touchedDown = NO; } -(IBAction)clickDown:(id)sender { self.downTimer = [NSTimer scheduledTimerWithTimeInterval:.3 target:self selector:@selector(buttonAction:) userInfo:nil repeats:NO]; } -(IBAction)dragInside:(id)sender { [self.downTimer invalidate]; [self buttonAction:self]; } -(void) buttonAction:(id) sender { if ([sender isKindOfClass:[NSTimer class]]) { self.touchedDown = YES; NSLog(@"click down"); }else{ if (! self.touchedDown) { NSLog(@"Drag"); } } } -(IBAction)touchUpAction:(id)sender { self.touchedDown = NO; } 

试试这个方法:

isClicked是BOOLtypes的属性。 设置为YES

 - (IBAction)clickButton:(UIButton *)sender { //touch down if(isClick==YES){ NSLog(@"Click Button"); //do all your stuffs here } isClick=YES; } - (IBAction)dragInsideButton:(UIButton *)sender { NSLog(@"Drag Button"); isClick=NO; } 

除此之外,您还可以实施removeTarget:Action:

哪一个方法被调用第一次isClick = NO,我期望clickButton在button操作中被首先调用。

我从一个UIButton的两个动作方法中获得; 下一步追求前进:

根据您的要求更改它。

就我个人而言,我只是跟踪你的视图控制器或button子类中的整数button的状态。 如果你追踪button的function,你可以控制每个动作的作用。 在你的.h文件里放入这样的东西:

 enum { MyButtonScanning, MyButtonStalling, MyButtonIdle }; @interface YourClass : UIViewController { NSInteger buttonModeAt; } @property (nonatomic) NSInteger buttonModeAt; -(IBAction)buttonPushedDown:(id)sender; -(void)tryScanForward:(id)sender; -(IBAction)buttonReleasedOutside:(id)sender; -(IBAction)buttonReleasedInside:(id)sender; @end 

然后在你的.m文件中引入这些东西:

 @implementation YourClass ///in your .m file @synthesize buttonModeAt; ///link this to your button's touch down -(IBAction)buttonPushedDown:(id)sender { buttonModeAt = MyButtonStalling; [self performSelector:@selector(tryScanForward:) withObject:nil afterDelay:1.0]; } -(void)tryScanForward:(id)sender { if (buttonModeAt == MyButtonStalling) { ///the button was not released so let's start scanning buttonModeAt = MyButtonScanning; ////your actual scanning code or a call to it can go here [self startScanForward]; } } ////you will link this to the button's touch up outside -(IBAction)buttonReleasedOutside:(id)sender { if (buttonModeAt == MyButtonScanning) { ///they released the button and stopped scanning forward [self stopScanForward]; } else if (buttonModeAt == MyButtonStalling) { ///they released the button before the delay period finished ///but it was outside, so we do nothing } self.buttonModeAt = MyButtonIdle; } ////you will link this to the button's touch up inside -(IBAction)buttonReleasedInside:(id)sender { if (buttonModeAt == MyButtonScanning) { ///they released the button and stopped scanning forward [self stopScanForward]; } else if (buttonModeAt == MyButtonStalling) { ///they released the button before the delay period finished so we skip forward [self skipForward]; } self.buttonModeAt = MyButtonIdle; } 

之后,只需将button的操作链接到我在IBactions之前的注释中提到的内容。 我没有testing过,但应该可以工作。

我不确定它会工作,但你可以尝试

  - (IBAction)dragInsideButton:(UIButton *)sender { [sender removeTarget:self forSelector:@selector(clickButton:) forControlEvent:UIControlEventTouchDown]; }