Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

cocoa touch - UIbutton with longpress and Touchup inside

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");
 }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...