如何将手势识别器从一个视图传递到另一个视图

时间:2014-01-02 18:50:17

标签: ios objective-c uigesturerecognizer uitouch

我有一个包含多个子视图的视图,这些子视图是带有多个按钮的复杂控件。 superview具有水龙头,滑动等手势识别器。

在某些情况下,当接受单次或双次触摸时,我希望超级视图将手势识别器传递给子视图进行治疗。

例如:

singletaprecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSingleTapGestureRecognizerDetected:)];

- (void)onSingleTapGestureRecognizerDetected:(UITapGestureRecognizer*)theTapGestureRecognizer
{
    if (someCaseHappens) {
        // do something with the gesture - for instance - move a control from one place to another or some other UI action in the superview
    }
    else {
        // the subview will need to get the gesture and do something with it (imagine the touch was inside a button in a subview - the top superview should disregard the gesture and the button should be pressed) - THIS ISN"T HAPPENING NOW
    }
}

1 个答案:

答案 0 :(得分:7)

好吧,您可以创建一个继承自UIView的自定义视图,然后覆盖:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

通过此方法,您可以返回要处理事件的视图。

看看:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/hitTest:withEvent