带有长按和点击事件的UIButton

8

如何创建一个具有两个动作的UIButton。

我知道通过使用UILongPressGestureRecognizer可以执行长按操作。

但我的要求是,当我长按UIButton时,它必须执行一个动作,当我在内部触摸时,它必须执行另一个动作。

谢谢。

以下是我的代码。

       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");
 }
1个回答

13

对于轻触(tap)您可以使用UIButton的“addTarget:…”方法,对于长按(longpress)您可以添加一个手势识别器:

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];

当然,您需要实现将被调用的两个方法:

- (void)userTapped:(id)sender {
   NSLog(@"user tapped");
}

- (void)userLongPressed:(id)sender {
   NSLog(@"user long pressed");
}

希望这可以帮助到你。

=========

编辑:看起来你正在将按钮用作UIToolbar中的BarButtonItem。所以我改变了我的代码来做同样的事情:

- (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];
}

而被调用的两种方法:

- (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer {
    NSLog(@"Long press");
}


-(void)redbottonmethod {  
    NSLog(@"single tapped");
}

这段代码肯定可以工作。

顺便说一句:我注意到你的代码中,在被调用的两个方法中有一个拼写错误:必须使用NSLog()而不是NSlog()。这可能是问题所在吗?


joern 我已经尝试过了,-(void)userLongPressed:(id)sender 这个方法没有被调用。如果我长按或单击,-(void)userTapped:(id)sender 方法每次都会被调用。 - Ayesha Fatima
很奇怪,我在一个测试项目中尝试了上面的代码,它运行正常。可能是个愚蠢的问题,但你确定已经将 GestureRecognizer 添加到按钮了吗? - joern
不,我发表问题时只是这样打字的。 谢谢。 - Ayesha Fatima
是的,我尝试过了,使用布尔条件可以正常工作。再次感谢。 - Ayesha Fatima
3
这个会触发两次事件。 - tong

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接