将触摸转移到父视图,而不禁用子视图的用户交互

我在视图控制器的视图上有一个UIView。 平移手势被添加到UIView上。 现在我想将触摸转移到父视图(视图控制器的视图),以便触摸委托方法也被称为父视图以及UIView也被平移。

取决于你想做什么。 如果你想让视图控制器知道在UIView子中发生了什么,你应该把主视图控制器的委托传递给子视图(面向对象的编程方式)。 像这样的东西:

// in child UIVIew ... id<mainControllerDelegate> _mainControllerDel; // This delegate was passed to the view by the main view controller ... -(void)gestureHappened { [_mainControllerDel gestureHappenedInView]; } 

但是,如果您希望两个视图都对手势做出反应,则应使用shouldRecognizeSimultaneouslyWithGestureRecognizer手势委托方法,如下所示:

 // In class that conforms to your UIGestureRecognizerDelegate - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; } 

编辑:

我刚刚读过你想要touchesBegan (和类似的)在父母中调用的方法。 这不是任何UI的标准行为。 请阅读iOS事件应答链 。 如果你真的想要链中的下一个视图被调用,你可以覆盖子的方法并调用下一个响应者。 喜欢这个:

 // in child view -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesBegan:touches withEvent:event]; [self.nextResponder touchesBegan:touches withEvent:event]; } 

我会做的是类似的,但使用委托:

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [_mainControllerDel touchBeganOnView: self withEvent: event]; }