スワイプを実行した方向を認識し、ビュー上のどのオブジェクトがそれを送信したかを知る必要があります。私は今のところ回避策を見つけましたが、それは複雑で巨大に見えるので、最も簡単な解決策はないでしょうか?いくつかの言葉で私の回避策:
- ビュー上のすべてのUIButtonオブジェクトにレコグナイザーをアタッチしました。
- スワイプの実行中にどのボタンがタップされたかを示すインジケーターがあります
- このインジケーターを確認し、方向をスワイプして正しい判断を下す必要があります。どのオブジェクトとどの方向に移動する必要がありますか。
アドバイスありがとうございます。
@synthesize swipeDirection; //スワイプ方向を示すラベル
@synthesize buttonNumber; //タップされているボタンの数を示すラベル
-(void)viewDidLoad
{{
UIButton*ボタン=[UIButtonbuttonWithType:UIButtonTypeRoundedRect]; //作成ボタン
[button setTitle:@ "test1" forState:UIControlStateNormal];
button.frame = CGRectMake(100、100、100、100);
[button addTarget:self//タップされたボタンのインジケーターを更新するセレクターを追加します。タップボタンなしでスワイプする方法はありません。
アクション:@selector(updateButtonNumber :)
forControlEvents:UIControlEventTouchDown];
[ボタンsetTag:1];
[self.view addSubview:button]; //表示するボタンを追加
[自己beginListenToSwipes:button]; //4方向のUISwipeGestureRecognizerを追加するメソッドにボタンを送信します
/*2番目のボタンと同じタスク*/
UIButton * button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setTitle:@ "test2" forState:UIControlStateNormal];
button1.frame = CGRectMake(200、200、100、100);
[button1 addTarget:self
アクション:@selector(updateButtonNumber :)
forControlEvents:UIControlEventTouchDown];
[button1 setTag:2];
[self.view addSubview:button1];
[自己beginListenToSwipes:button1];
}
/ *ボタンをタップするたびに(スワイプを開始すると、このメソッドが呼び出され、buttonNumberは常に現在のボタンを指します* /
-(void)updateButtonNumber:(UIButton *)sender
{{
self.buttonNumber.text = [NSString stringWithFormat:@ "%d"、sender.tag];
}
/ *メソッドはUIButtonを受け取り、それにレコグナイザーを追加します* /
-(void)beginListenToSwipes:(UIButton *)sender
{{
UISwipeGestureRecognizer * recognizer;
認識=[[UISwipeGestureRecognizeralloc] initWithTarget:self action:@selector(handleSwipe :)];
[認識機能setDirection:(UISwipeGestureRecognizerDirectionRight)];
[送信者addGestureRecognizer:recognizer];
認識=[[UISwipeGestureRecognizeralloc] initWithTarget:self action:@selector(handleSwipe :)];
[認識機能setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[送信者addGestureRecognizer:recognizer];
認識=[[UISwipeGestureRecognizeralloc] initWithTarget:self action:@selector(handleSwipe :)];
[認識機能setDirection:(UISwipeGestureRecognizerDirectionUp)];
[送信者addGestureRecognizer:recognizer];
認識=[[UISwipeGestureRecognizeralloc] initWithTarget:self action:@selector(handleSwipe :)];
[認識機能setDirection:(UISwipeGestureRecognizerDirectionDown)];
[送信者addGestureRecognizer:recognizer];
}
/ *スワイプが実行されたときに呼び出され、実行された方向を示すメソッド* /
-(void)handleSwipe:(UISwipeGestureRecognizer *)recognizer
{{
int i = Recognitionr.direction;
self.swipeDirection.text = [NSString stringWithFormat:@ "%d"、i];
}