2

いくつかの UIImageViews を追加する必要があるアプリケーションに取り組んでいます。モーダル ビュー コントローラーには、さまざまなボタンが表示されます。モーダル ビュー コントローラーを閉じると、追加する UIImageView を決定するのに役立つボタンのタグが送信されます。

最初に UIImageView を追加すると、すべてのジェスチャが機能します。しかし、2 番目のものを追加すると、最初のものはタッチに対する反応を失います。

UIImageView (Body:UIImageView) を追加するコードは次のとおりです。

-(void) addBodyToStage:(int)index {
NSString * imageString = [NSString stringWithFormat:@"Body%i.png",index];
UIImage * image = [UIImage imageNamed:imageString];
Body * body = [[Body alloc] initWithImage:image];

//For Pan Gestures
[body setUserInteractionEnabled:YES];
[body addGestureRecognizer:panGesture];
[panGesture addTarget:body action:@selector(handlePan:)];

//For Pinch Gestures
[pinchGesture addTarget:body action:@selector(handlePinch:)];
[body addGestureRecognizer:pinchGesture];


//Adding to the view
[self.view addSubview:body];

}
4

3 に答える 3

0

同じ panGestureRecognizer と pinchGestureRecognizer (インスタンス変数) がすべての imageView に追加されました。それ自体は問題ありませんが、imageView ごとに異なるパン/ピンチ ジェスチャ レコグナイザーが必要だと思います。ビューはジェスチャ ジェスチャ レコグナイザを保持するため、このメソッド自体にコードを追加できます。

行け

  -(void) addBodyToStage:(int)index {
     NSString * imageString = [NSString stringWithFormat:@"Body%i.png",index];
    UIImage * image = [UIImage imageNamed:imageString];
    Body * body = [[Body alloc] initWithImage:image];

   //Alloc the pan/pinch gesture recognizers here
   //remember to alloc/init/autorelease (if not using ARC) 
   //else just alloc init
   //Remove the instantiation of those gesture recognizers in any other 
   //part of the  code.

   //For Pan Gestures
   [body setUserInteractionEnabled:YES];
   [body addGestureRecognizer:panGesture];
   [panGesture addTarget:body action:@selector(handlePan:)];

   //For Pinch Gestures
   [pinchGesture addTarget:body action:@selector(handlePinch:)];
   [body addGestureRecognizer:pinchGesture];


   //Adding to the view
   [self.view addSubview:body];

   }
于 2012-07-12T18:23:23.723 に答える
0

ジェスチャ レコグナイザーは、一度に 1 つのビューにのみ割り当てることができます。それらを別のビューに割り当てると、最初から暗黙的に割り当てが解除されます。ビューごとにジェスチャ レコグナイザーを初期化するか、ジェスチャ レコグナイザーを階層の下位のビューに配置し、ジェスチャ レコグナイザーのviewプロパティを使用して、どのビューがタッチされたかを判断する必要があります。

于 2012-07-12T18:23:47.463 に答える
0

ジェスチャーレコグナイザーをどこでも初期化しますか? 同じジェスチャ認識エンジンを別の imageView に転用しているようです。imageView ごとに新しいジェスチャ認識エンジンをインスタンス化してみてください

于 2012-07-12T18:16:20.653 に答える