Add Longpress gesture and Click event on UIButton

ipad, iphone, long-press, uibutton, uigesturerecognizer

Solution

Change the line as follows,

     [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

You click event will be fired as soon you touch the button if you use touchdown event. TouchupInside fires the action on touchup.

To get title ,

 - (void)longPressDetected:(UIGestureRecognizer *)gestureRecognizer
{
    UIButton *button = (UIButton *)[gestureRecognizer view];
    NSLog(@"%@",[button titleForState:UIControlStateNormal]);
}

Problem

I am new to iPhone. I want to add both a long press gesture and a click event to my Button, is this possible? When I add both events to the button and then long press, my click event gets fired (on click I navigate to a new page), but my long press event never gets fired. Here is my code snippet: ``` button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(xpos, ypos, 120,130); [button setBackgroundImage:[UIImage imageNamed:@"ibook2.png"] forState:UIControlStateNormal]; [button setTitle:[NSString stringWithFormat:@"32"]]; [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown]; LongPress = [[UILongPressGestureRecognizer alloc] init]; [LongPress addTarget:self action:@selector(longPressDetected:)]; LongPress.delegate = (id<UIGestureRecognizerDelegate>)self; [button addGestureRecognizer:LongPress]; [LongPress release]; [self.view addSubview:button]; ``` How do I add both events? Any help will be appreciated.

Original source

Related problems