【问题标题】:UIbutton with longpress and Touchup insideUIbutton 长按和 Touch Up 里面
【发布时间】:2011-07-12 06:24:50
【问题描述】:

如何创建具有两个操作的 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");
 }

【问题讨论】:

    标签: cocoa-touch uibutton


    【解决方案1】:

    对于点击,您可以使用 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 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 个方法:

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

    这段代码绝对有效。

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

    【讨论】:

    • joern 我试过这个,- (void)userLongPressed:(id)sender 没有在这里打电话。如果我长按或单击 - (void)userTapped:(id)sender 每次都在打电话
    • 这很奇怪。我在一个测试项目中尝试了上面的代码,它工作正常。愚蠢的问题,但您确定您已将 GestureRecognizer 添加到按钮中吗?
    • 不,为了发布我的问题,我只是这样输入。谢谢
    • 是的,我试过了,它适用于布尔条件。再次感谢
    • 这个事件触发了两次。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-23
    • 1970-01-01
    • 2015-07-11
    • 1970-01-01
    • 2023-04-02
    相关资源
    最近更新 更多