本文介绍了如何在UIButton中实现两个没有重叠的IBAction?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I drag 2 IBActions from a UIButton, one with touchDown event and second with drag Inside.

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

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

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

But when I drag inside, the touchDown action also gets fired.

如何在dragInside时禁用touchDown事件。

How to disable touchDown event when dragInside.

谢谢!

推荐答案

我已经解决了这样的问题使用拖动事件

i have solved a problem like this with using drag Events

在.xib文件中或以编程方式将事件添加到按钮。

add events to your button in .xib file or programatically.

以编程方式:

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

}

这篇关于如何在UIButton中实现两个没有重叠的IBAction?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 08:23