带有长按和 Touchup 的 UIbutton [英] UIbutton with longpress and Touchup inside

查看:31
本文介绍了带有长按和 Touchup 的 UIbutton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建具有两个操作的 UIButton.

How to create a UIButton With two actions.

我知道通过使用 UILongPressGestureRecognizer 我们可以执行长按.

I know by using UILongPressGestureRecognizer we can perform Longpress.

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

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.

谢谢.

下面是我的代码.

       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");
 }

推荐答案

点击可以使用 UIButton 的addTarget:..."方法,长按可以添加手势识别器:

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

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

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");
}

希望对您有所帮助.

=========

您似乎将按钮用作 UIToolbar 中的 BarButtonItem.所以我改变了我的代码来做同样的事情:

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");
}

这段代码绝对有效.

顺便说一句:我注意到在您调用的 2 个方法中的代码中有错字:您必须使用 NSLog() 而不是 NSlog().这可能是问题吗?

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?

这篇关于带有长按和 Touchup 的 UIbutton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆