在UIButton上添加Longpress手势和点击事件

问题描述:

我是iPhone新手,在UIButton上添加Longpress手势和点击事件

我想在我的Button上添加Longpress手势和Click事件,是否有可能?

当我在按钮上添加两个事件,然后当我长按,我的点击事件被激发,点击事件我导航到新的页面,我的长按事件永远不会被解雇。

这里是我的代码片段,

  button = [UIButton buttonWithType:UIButtonTypeCustom]; 
      button.frame = CGRectMake(xpos, ypos, 120,130); 
      [button setBackgroundImage:[UIImage imageNamed:@"ibook2.png"] forState:UIControlStateNormal]; 
      [button setTitle:[NSString stringWithFormat:@"32"]]; 
      [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
      [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown]; 

      LongPress = [[UILongPressGestureRecognizer alloc] init]; 
      [LongPress addTarget:self action:@selector(longPressDetected:)]; 
      LongPress.delegate = (id<UIGestureRecognizerDelegate>)self; 
      [button addGestureRecognizer:LongPress]; 
      [LongPress release]; 

      [self.view addSubview:button]; 

如何添加两个事件?

任何帮助将不胜感激。

+1

可能重复http://*.com/questions/4825199/gesture-recognizer-and-button-actions?rq=1 – 2012-07-24 06:08:22

更改线路如下,

 [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; 

您单击事件会尽快与您轻触按钮,如果你使用的触下事件被解雇。 TouchupInside在touchup上触发动作。

要拿到冠军,

- (void)longPressDetected:(UIGestureRecognizer *)gestureRecognizer 
{ 
    UIButton *button = (UIButton *)[gestureRecognizer view]; 
    NSLog(@"%@",[button titleForState:UIControlStateNormal]); 
} 
+0

感谢名单这是工作,如何获得长按按钮的按钮文本? – Krunal 2012-07-24 06:13:26

+0

@Krunal。欢迎。编辑答案! – Vignesh 2012-07-24 06:19:17

+0

'NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString * documentsDirectory = [paths objectAtIndex:0]; [[NSFileManager defaultManager] removeItemAtPath:documentsDirectory error:NULL]; documentsDirectory = [文档目录stringByAppendingString:@“/%@”,[btn titleForState:UIControlStateNormal]];'我在最后一行得到错误原因?有没有语法错误? – Krunal 2012-07-24 06:30:40