-1

重複の可能性:
uiview と uibutton で Uitableview セルのタップを検出する方法は?
UIButton 長押しイベント

テーブルのカスタム セルからボタンを読み込んでいます。ユーザーのシングル クリックまたはボタンのロング プレス イベントを特定するにはどうすればよいですか?

4

3 に答える 3

1

私はそれをグーグルで検索し、スタックオーバーフローから最良の回答を得まし

- (void)viewDidLoad
{
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [self.button addGestureRecognizer:longPress];
    [longPress release];

 [super viewDidLoad];


}

とイベント:-

- (void)longPress:(UILongPressGestureRecognizer*)gesture {
    if ( gesture.state == UIGestureRecognizerStateEnded ) {
         NSLog(@"Long Press");
    }
}
于 2013-01-18T06:35:15.963 に答える
0

UILongPressGestureRecognizer インスタンスを作成してボタンにアタッチすることから始めることができます。

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.button addGestureRecognizer:longPress];
[longPress release];

そして、ジェスチャーを処理するメソッドを実装します

- (void)longPress:(UILongPressGestureRecognizer*)gesture {
    if ( gesture.state == UIGestureRecognizerStateEnded ) {
         NSLog(@"Long Press");
    }
}

これが基本的なアプローチになります。また、プレスの最小時間と、許容されるエラーの量を設定することもできます。また、ジェスチャーを認識した後にメソッドが数回呼び出されることにも注意してください。そのため、ジェスチャーの最後に何かをしたい場合は、その状態を確認して処理する必要があります。

参照

于 2013-01-18T06:36:42.810 に答える
0
- (void)setLongTouchAction:(SEL)newValue
{
    if (newValue == NULL)
    {
        [self removeGestureRecognizer:longPressGestureRecognizer];
        [longPressGestureRecognizer release];
        longPressGestureRecognizer = nil;
    }
    else
    {
        [longPressGestureRecognizer release];
        longPressGestureRecognizer = nil;

        longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:[[self allTargets] anyObject] action:newValue];
        [self addGestureRecognizer:longPressGestureRecognizer];
    }
}


[undoButton addTarget:self action:@selector(performUndo:) forControlEvents:UIControlEventTouchUpInside];
[undoButton setLongTouchAction:@selector(showUndoOptions:)];
于 2013-01-18T06:36:50.403 に答える