0

这是设置。

我有一个自定义 UIScrollView 作为 PrimeView 的子视图。

自定义的 UIScrollView 可以看作是覆盖滚动视图,它最初获取所有的触摸事件。

现在,当滚动视图不拖动时,我希望它将触摸事件传递给其他响应者。
下面是我目前的代码,但我不确定 self.nextResponder 和 super 之间的区别。

我不明白为什么 touchesBegan 被正确传递给超级视图,但 touchesMoved 没有被传递给超级视图。

-(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event
{
    if (!self.dragging)
    {
        [self.nextResponder touchesBegan: touches withEvent:event];

    }

    [super touchesBegan: touches withEvent: event];
}

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

1 回答 1

3

苹果的立场是你永远不应该做这样的事情:

    [self.nextResponder touchesBegan: touches withEvent:event];

如果你这样做,你就超出了 UIKit 自己在响应者链上转发消息的范围,结果是未定义的。

Also, in your touchesMoved:withEvent:, you're sending touchesBegan:withEvent: to nextResponder, which seems suspicious.

Also, in your touchesMoved:withEvent:, you're passing on the event twice, which seems like a bad idea.

If you want to handle drags conditionally, your best bet is to use a UIPanGestureRecognizer. If the recognizer doesn't accept the event, it will be forwarded up the responder chain normally.

If you are a registered (paid) iOS developer, you should have access to the WWDC 2012 videos. I strongly recommend you watch the “Enhancing User Experience with Scroll Views” video. I won't say more about it because its contents are still under NDA.

于 2012-06-26T05:52:51.837 回答