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

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

问题描述

如何使用两个动作创建UIButton。



我知道使用UILongPressGestureRecognizer可以执行Longpress。



但是我的要求是,当我长按UIButton时,它必须执行一个动作,而当触摸其中的

时,它必须执行另一个动作。

p>

谢谢。



下面是我的代码。

  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动作:@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版本];

-(void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer {

if(longpressGesture.state == UIGestureRecognizerStateBegan)
{
NSlog(@长按);
}

}


-(void)redbottonmethod
{

NSlog(@ );
}


解决方案

对于水龙头,您可以使用UIButton的 addTarget:...方法,对于长按,您可以添加一个手势识别器:

  UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
btn.frame = CGRectMake(100.0,100.0,100.0,20.0);
[btn setTitle:@ Test forState:UIControlStateNormal];
[btn addTarget:self操作:@selector(userTapped :) forControlEvents:UIControlEventTouchUpInside];

UILongPressGestureRecognizer * gr = [[UILongPressGestureRecognizer alloc] init];
[gr addTarget:self操作:@selector(userLongPressed :)];
[btn addGestureRecognizer:gr];
[gr版本];

[self.view addSubview:btn];

当然,您需要实现以下两种方法:

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

-(void)userLongPressed:(id)sender {
NSLog(@用户长按);
}

希望有帮助。



=========



编辑:似乎您将按钮用作UIToolbar中的BarButtonItem。因此,我更改了代码以执行相同的操作:

 -(void)viewDidLoad {
[super viewDidLoad];

//设置按钮
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);

//设置带有按钮作为其视图的条形按钮项
UIBarButtonItem * redTab = [[UIBarButtonItem alloc] initWithCustomView:tabRedbutton];

//设置工具栏并将按钮添加为条形按钮项
UIToolbar *工具栏= [[UIToolbar alloc] initWithFrame:CGRectMake(0.0,100.0,768.0,40.0)];
toolbar.barStyle = UIBarStyleBlack;
NSArray * items = [NSArray arrayWithObject:redTab];
[工具栏setItems:items];
[self.view addSubview:toolbar];
[工具栏发布];

//将点击处理程序添加到用于点击
的按钮中[tabRedbutton addTarget:self action:@selector(redbottonmethod)forControlEvents:UIControlEventTouchUpInside];

//向长按
的按钮添加手势识别器UILongPressGestureRecognizer * longpressGesture1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler :)];
longpressGesture1.minimumPressDuration = 0.1;
[tabRedbutton addGestureRecognizer:longpressGesture1];
[longpressGesture1版本];
}

以及被称为的两种方法:

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


-(void)redbottonmethod {
NSLog(@ single taped);
}

此代码绝对有效。



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


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?

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

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