将触摸转移到父视图而不禁用子视图的用户交互 [英] Transfer touch to the parent view without disabling the user interaction of child view

查看:62
本文介绍了将触摸转移到父视图而不禁用子视图的用户交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UIView on the view controller's view. A pan gesture is added on the UIView. Now I want to transfer the touch to the parent view (view controller's view) so that the touches delegate methods also gets called of parent view as well as the UIView is also panned.

推荐答案

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

Depends on what you want to do. If you want to let the view controller know that something happened in the UIView child, you should pass a delegate of the main view controller to the child view (the Object-Oriented-Programming way). Something like this:

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

但如果您希望两个视图都对手势做出反应,您应该使用 shouldRecognizeSimultaneouslyWithGestureRecognizer 手势委托方法,像这样:

But if you want both views to react to the gesture you should use the shouldRecognizeSimultaneouslyWithGestureRecognizer gesture delegate method, like this:

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

我刚刚读到您还希望在父级中调用 touchesBegan(和类似的)方法.这不是任何 UI 的标准行为.请阅读 iOS 事件响应链.如果您真的希望调用链中的下一个视图,您可以覆盖孩子的方法并调用下一个响应者.像这样:

I've just read you want the touchesBegan (and similar) method called in the parent also. That is not standard behaviour for any UI. Please read up on iOS event responder chain. If you really want the next view in chain to be called you can override the child's methods and call the next responder. Like this:

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

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

What I would do is something similar but using a delegate:

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

这篇关于将触摸转移到父视图而不禁用子视图的用户交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆