How to create a UIButton With two actions.
I know by using UILongPressGestureRecognizer we can perform Longpress.
But my requirement is,When I Long Press UIButton,it has to perform one action and when touch
up inside it, it has to perform another action.
Thanks.
Below is my code.
UIImage *redImage = [UIImage imageNamed:@"TabFav2.png"]; tabRedbutton = [UIButton buttonWithType:UIButtonTypeCustom]; [tabRedbutton setImage:redImage forState:UIControlStateNormal]; tabRedbutton.frame = CGRectMake(0.0, 0.0, 50,35); redTAb = [[UIBarButtonItem alloc] initWithCustomView:tabRedbutton]; [tabRedbutton addTarget:self action:@selector(redbottonmethod) forControlEvents:UIControlEventTouchUpInside]; longpressGesture1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)]; longpressGesture1.minimumPressDuration =0.1; [longpressGesture1 setDelegate:self]; longpressGesture1.cancelsTouchesInView = NO; [tabRedbutton addGestureRecognizer:longpressGesture1]; [longpressGesture1 release]; - (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer { if (longpressGesture.state == UIGestureRecognizerStateBegan) { NSlog(@"Long press"); } } -(void)redbottonmethod { NSlog(@"single tapped"); }
For the tap you can use UIButton's "addTarget:..." method and for the longpress you can add a gesture recognizer:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame = CGRectMake(100.0, 100.0, 100.0, 20.0); [btn setTitle:@"Test" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(userTapped:) forControlEvents:UIControlEventTouchUpInside]; UILongPressGestureRecognizer *gr = [[UILongPressGestureRecognizer alloc] init]; [gr addTarget:self action:@selector(userLongPressed:)]; [btn addGestureRecognizer:gr]; [gr release]; [self.view addSubview:btn];
Of course you need to implement the 2 methods that will be called:
- (void)userTapped:(id)sender { NSLog(@"user tapped"); } - (void)userLongPressed:(id)sender { NSLog(@"user long pressed"); }
Hope that helps.
=========
EDIT: It seems that you are using your button as a BarButtonItem inside a UIToolbar. So I changed my code to do the same:
- (void)viewDidLoad { [super viewDidLoad]; // set up the button UIImage *redImage = [UIImage imageNamed:@"TabFav2.png"]; UIButton *tabRedbutton = [UIButton buttonWithType:UIButtonTypeCustom]; tabRedbutton.backgroundColor = [UIColor redColor]; [tabRedbutton setImage:redImage forState:UIControlStateNormal]; tabRedbutton.frame = CGRectMake(0.0, 0.0, 50,35); // set up a bar button item with the button as its view UIBarButtonItem *redTab = [[UIBarButtonItem alloc] initWithCustomView:tabRedbutton]; // set up toolbar and add the button as a bar button item UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, 100.0, 768.0, 40.0)]; toolbar.barStyle = UIBarStyleBlack; NSArray *items = [NSArray arrayWithObject:redTab]; [toolbar setItems:items]; [self.view addSubview:toolbar]; [toolbar release]; // add tap handler to button for tap [tabRedbutton addTarget:self action:@selector(redbottonmethod) forControlEvents:UIControlEventTouchUpInside]; // add gesture recognizer to button for longpress UILongPressGestureRecognizer *longpressGesture1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)]; longpressGesture1.minimumPressDuration =0.1; [tabRedbutton addGestureRecognizer:longpressGesture1]; [longpressGesture1 release]; }
And the two methods that get called:
- (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer { NSLog(@"Long press"); } -(void)redbottonmethod { NSLog(@"single tapped"); }
This code definitely works.
By the way: I noticed that in your code in the 2 methods that get called you have typo: You must use NSLog() and not NSlog(). Could that be the problem?