简体   繁体   English

多个UIImageViews不响应触摸。

[英]Multiple UIImageViews does not respond to touches.

I am working on an application which requires several UIImageViews to be added. 我正在开发一个需要添加多个UIImageViews的应用程序。 A modal view controller is presented with different buttons. 模态视图控制器显示有不同的按钮。 When dismissing the modal view controller it sends tag of the button which helps us deciding which UIImageView to add. 当关闭模式视图控制器时,它发送按钮的标签,这有助于我们确定要添加的UIImageView。

Now when I add first UIImageView, all the gestures work on it. 现在,当我添加第一个UIImageView时,所有手势都可以使用。 But upon adding the second one, the first one loses response to touch. 但是添加第二个后,第一个就失去了对触摸的响应。

The code to add the UIImageView (Body:UIImageView) is : 添加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];

}

do you initialize the gesture recognizers anywhere? 您可以在任何地方初始化手势识别器吗? It looks like you might be repurposing the same gesture recognizer to different imageViews. 看起来您可能正在将相同的手势识别器重新用于不同的imageViews。 Try instantiating new gesture recognizers for each imageView 尝试为每个imageView实例化新的手势识别器

The same panGestureRecognizer, and pinchGestureRecognizer (the instance variable) has been added to all the imageViews. 相同的panGestureRecognizer和pinchGestureRecognizer(实例变量)已添加到所有imageViews中。 There is no problem with that per se, but I think you want different pan/pinch gesture recognizers for each imageView. 这样做本身没有问题,但我认为您需要为每个imageView使用不同的平移/捏合手势识别器。 The view will retain the gesture gesture recognizers so you can add the code in this method itself. 该视图将保留手势手势识别器,因此您可以在此方法本身中添加代码。

Go

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

   }

Gesture recognizers can only be assigned to one view at a time. 手势识别器一次只能分配给一个视图。 If you assign them to another view, they are implicitly unassigned from the first. 如果将它们分配给另一个视图,则它们将从第一个隐式取消分配。 You will need to initialize gesture recognizers for every view, or place the gesture recognizer on a view lower in the hierarchy and use the view property of the gesture recognizer to tell which one was touched. 您将需要为每个视图初始化手势识别器,或者将手势识别器放置在层次结构中较低的视图上,并使用手势识别器的view属性来判断触摸了哪个手势。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM