将触摸传输到父视图而不禁用子视图的用户交互

时间:2014-12-08 08:01:07

标签: ios objective-c

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

1 个答案:

答案 0 :(得分:4)

取决于你想做什么。如果你想让视图控制器知道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 event responder chain。如果你真的想要调用链中的下一个视图,你可以覆盖子方法并调用下一个响应者。像这样:

// 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];
}