我正在开发一个需要添加多个UIImageViews的应用程序。
模态视图控制器显示有不同的按钮。
当关闭模式视图控制器时,它发送按钮的标签,这有助于我们确定要添加的UIImageView。
现在,当我添加第一个UIImageView时,所有手势都可以使用它。但是添加第二个后,第一个就失去了对触摸的响应。
添加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];
}
最佳答案
相同的panGestureRecognizer和pinchGestureRecognizer(实例变量)已添加到所有imageViews中。这样做本身没有问题,但我认为您需要为每个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];
}
关于iphone - 多个UIImageViews不响应触摸。,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11458017/