【问题标题】:Multiple UIImageViews does not respond to touches.多个 UIImageViews 不响应触摸。
【发布时间】:2012-07-12 13:38:15
【问题描述】:

我正在开发一个需要添加多个 UIImageView 的应用程序。 模态视图控制器显示有不同的按钮。 当关闭模态视图控制器时,它会发送按钮标签,帮助我们决定添加哪个 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];

}

【问题讨论】:

  • 为每个视图添加一个新的平移手势实例,或者在它们共享的超级视图上使用单个手势识别器,并为每个帧使用CGRectContainsPoint,以查看手势是否为@ 987654323@ 包含在其中。
  • 上述问题解决了吗?我面临同样的问题...

标签: iphone objective-c ios ios4


【解决方案1】:

您是否在任何地方初始化手势识别器?看起来您可能会将相同的手势识别器重新用于不同的图像视图。尝试为每个 imageView 实例化新的手势识别器

【讨论】:

    【解决方案2】:

    相同的 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];
    
       }
    

    【讨论】:

    • 什么是身体?它是子类还是什么?
    • Body 是 UIView 的子类
    【解决方案3】:

    手势识别器一次只能分配给一个视图。如果您将它们分配给另一个视图,它们会从第一个视图中隐式取消分配。您需要为每个视图初始化手势识别器,或者将手势识别器放置在层次结构中较低的视图上,并使用手势识别器的view 属性来判断触摸的是哪个视图。

    【讨论】:

      猜你喜欢
      • 2021-10-31
      • 2016-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多