1

我想提供UIButton不同的功能,具体取决于它是按下并释放一次,还是按住(理想情况下为 1.5 秒)并在屏幕上移动。我目前正在使用此代码:

  [button addTarget:self action:@selector(open:)
     forControlEvents:UIControlEventTouchDown]

    panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
    [panRecognizer setMinimumNumberOfTouches:1];
    [panRecognizer setMaximumNumberOfTouches:1];
    [panRecognizer setDelegate:self];
    [button addGestureRecognizer:panRecognizer];

-(IBAction)open:(id)sender {}

-(void)move:(id)sender{}

move:工作正常,但open:没有。

4

2 回答 2

0

我这样做了,让我知道它是否适合您:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didRecognizeTapGesture:)];
    tapGesture.cancelsTouchesInView    = NO;
    tapGesture.delegate                = self;
    [tapGesture requireGestureRecognizerToFail:self.scrollView.panGestureRecognizer];
    [self.scrollView addGestureRecognizer:tapGesture];
    self.tapGesture = tapGesture;
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if (touch.view == self.button)
    {
        return NO;
    }
    return YES;
}

- (void)didRecognizeTapGesture:(UITapGestureRecognizer*)gesture
{
    if (gesture.state == UIGestureRecognizerStateEnded)
    {
        NSLog(@"GESTURE ENDED");
    }
}

- (void)didPressButton:(UIButton*)sender
{
    NSLog(@"BUTTON TOUCH UP INSIDE");
}
于 2014-01-06T21:09:35.133 回答
0

也许你可以使用按钮的拖动事件,比如

[button addTarget:self action:@selector(wasDragged:withEvent:) 
    forControlEvents:UIControlEventTouchDragInside];

你可以看看这个链接: http: //www.cocoanetics.com/2010/11/draggable-buttons-labels/

可以在move方法中放一个flag,当进入touch up方法时,检查按钮是否移动,决定触发open相关逻辑

#define BUTTON_DRAGGED_TAG -100
#define BUTTON_DEFAULT_TAG 0

- (void)onButtonTouchUpInside:(UIButton *)button withEvent:(UIEvent *)event
{
    if (button.tag != BUTTON_DRAGGED_TAG)
    {
        //doOpen
    }

    button.tag = BUTTON_DEFAULT_TAG;
}

- (void)onButtonTouchUpOutside:(UIButton *)button withEvent:(UIEvent *)event
{
    button.tag = BUTTON_DEFAULT_TAG;
 }

- (void)onButtonTouchCancel:(UIButton *)button withEvent:(UIEvent *)event
{
    button.tag = BUTTON_DEFAULT_TAG;
}

- (void)onButtonDraged:(UIButton *)button withEvent:(UIEvent *)event
{

    button.tag = BUTTON_DRAGGED_TAG;
}
于 2013-04-24T08:59:54.667 回答