开发者

iphone, App thinks I held the button 2 times

I've go such class

@interface UIGestureHolder : UILongPressGestureRecognizer {

    int tag;
}
@property (nonatomic, readwrite) int tag;

@end



@implementation UIGestureHolder
@synthesize tag;
@end

Then I assign this to a button so that when it's held buttonheld function is appealed:

UIGestureHolder *longpressGesture = [[UIGestureHolder alloc] initWithTarget:self action:@selector(buttonHeld:)];
        longpressGesture.minimumPressDuration = 1.5;

        [longpressGesture setDelegate:self];
        longpressGesture.tag=i;
        [contactButton addGestureRecognizer:longpressGesture];
        [longpressGesture release];

Buttonheld function:

-(void)buttonHeld:(id)sender
{


    int i = (开发者_如何学运维(UIControl *) sender).tag;

    ......

}

When I hold the button for 1.5 seconds or whatever time I want, runtime tells me I held the button two times 1.5 each, why? It causes me exceptions later on.

Why runtime thinks that I hold the button two times?


Gesture recognizers inform you when their states change. Change the code to:

-(void)buttonHeld:(UILongPressGestureRecognizer*)longPressRecognizer 
 {
    if(longPressRecognizer.state == UIGestureRecognizerStateBegan) 
    {
        int i = ((UIControl *) sender).tag;
        ......
    }
}


Maybe the button also has single tap enabled? Button behavior is to tap on it once & something happens, right?

When assigning responder to your button, what have you done? Try disabling that & see if you get the same...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜