重複の可能性:
uiview と uibutton で Uitableview セルのタップを検出する方法は?
UIButton 長押しイベント
テーブルのカスタム セルからボタンを読み込んでいます。ユーザーのシングル クリックまたはボタンのロング プレス イベントを特定するにはどうすればよいですか?
重複の可能性:
uiview と uibutton で Uitableview セルのタップを検出する方法は?
UIButton 長押しイベント
テーブルのカスタム セルからボタンを読み込んでいます。ユーザーのシングル クリックまたはボタンのロング プレス イベントを特定するにはどうすればよいですか?
私はそれをグーグルで検索し、スタックオーバーフローから最良の回答を得ました
- (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");
}
}
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");
}
}
これが基本的なアプローチになります。また、プレスの最小時間と、許容されるエラーの量を設定することもできます。また、ジェスチャーを認識した後にメソッドが数回呼び出されることにも注意してください。そのため、ジェスチャーの最後に何かをしたい場合は、その状態を確認して処理する必要があります。
- (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:)];