【问题标题】:How can I pass a gesture recognizer from one view to another如何将手势识别器从一个视图传递到另一个视图
【发布时间】:2014-01-02 18:50:16
【问题描述】:

我有一个包含多个子视图的视图,这些子视图是带有多个按钮的复杂控件。 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
    }
}

【问题讨论】:

  • 也许你可以用[singletaprecognizer setCancelsTouchesInView:NO];做点什么

标签: ios objective-c uigesturerecognizer uitouch


【解决方案1】:

好吧,你可以创建一个继承自 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:

【讨论】:

  • 我不确定这是不是最好的方法。苹果类如何处理同一视图上的多个手势?在事先不知道子视图的情况下,它如何知道将事件传递给子视图
  • 你需要让你的大多数superview(带有手势识别器的那个)实现这个方法。它已经有相关的子视图。在某些情况下,您只需处理对 hitTest my call super 的调用,而在其他情况下,您将返回要处理的视图。这样你“模拟”用户正在触摸另一个视图
  • 你怎么知道是什么手势导致了 hitTest
  • 你不知道,当 hitTest 出现时,你需要将从手势中获得的信息与 hittest 结合起来。另见stackoverflow.com/questions/259183/delay-with-touch-events
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-20
  • 1970-01-01
  • 1970-01-01
  • 2021-09-04
相关资源
最近更新 更多