3

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

両方のイベントを追加するにはどうすればよいですか?

どんな助けでも大歓迎です。

4

1 に答える 1

8

次のように行を変更します。

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

タッチダウンイベントを使用すると、ボタンに触れるとすぐにクリックイベントが発生します。TouchupInside は、タッチアップ時にアクションを起動します。

タイトルを取得するには、

 - (void)longPressDetected:(UIGestureRecognizer *)gestureRecognizer
{
    UIButton *button = (UIButton *)[gestureRecognizer view];
    NSLog(@"%@",[button titleForState:UIControlStateNormal]);
}
于 2012-07-24T06:08:28.843 に答える