在UIButton上添加Longpress手势和Click事件

Kru*_*nal 2 iphone uibutton ipad uigesturerecognizer long-press

我是iPhone新手.

我想在我的按钮上添加长按手势和点击事件,这可能吗?

当我将两个事件添加到按钮然后长按时,我的点击事件被触发(点击我导航到新页面),但我的长按事件永远不会被触发.

这是我的代码片段:

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];
Run Code Online (Sandbox Code Playgroud)

如何添加这两个事件?

任何帮助将不胜感激.

Vig*_*esh 7

更改行如下,

     [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)

如果使用触地事件,只要触摸按钮,就会触发点击事件.TouchupInside在触摸时触发动作.

获得头衔,

 - (void)longPressDetected:(UIGestureRecognizer *)gestureRecognizer
{
    UIButton *button = (UIButton *)[gestureRecognizer view];
    NSLog(@"%@",[button titleForState:UIControlStateNormal]);
}
Run Code Online (Sandbox Code Playgroud)