如何在UIButton中实现两个没有重叠的IBAction?

时间:2013-03-23 15:37:28

标签: ios objective-c uibutton ibaction

我从UIButton拖动 2 IBActions ,其中一个使用touchDown事件,第二个拖动内部。

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

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

但是当我向内拖动时,touchDown动作也会被触发。

如何在dragInside时禁用touchDown事件。

谢谢!

5 个答案:

答案 0 :(得分:4)

我使用拖动事件

解决了这样的问题

在.xib文件中或以编程方式向您的按钮添加事件。

以编程方式:

[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
    }

}

答案 1 :(得分:3)

此方法已经过测试,应该按照我的想法进行测试。您可以更改计时器中的延迟以获得所需的效果。我不得不将3个动作连接到按钮以使其工作 - 第三个动作是将系统重置为起始条件的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;
}

答案 2 :(得分:1)

尝试这种方式:

isClicked是BOOL类型的属性。设置为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。

答案 3 :(得分:0)

我不确定它会起作用,但你可以试试

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

答案 4 :(得分:0)

我来自Two action methods for an UIButton; next track and seek forward:

根据您的要求更改它。

就个人而言,我只是在视图控制器或按钮子类中使用整数跟踪按钮的状态。如果您跟踪按钮的功能,您可以控制每个操作的功能。在.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;

}

之后,只需将按钮的操作链接到我在IBactions之前的评论中注明的内容。我没有测试过这个,但它应该可以工作。