2

我有一个问题,我已经搜索过但找不到明确的答案。这是我的布局:

UIView - ViewController   
   |_UIScrollView - added programatically
      | |_UIView to hold a backgound/perimeter  - added programmatically
      |_UIView 1 - added programmatically
      |_UIView 2 - added programmatically
       and so on

我的问题是当我在触摸时移动说 UIView 2 时,ViewController 为何只调用一次“touchesMoved”?

现在 UIView 有它自己的 touchesMoved 方法,但我需要控制器的 touchesMoved 被调用,因为我需要它与 ScrollView 对话以更新其位置。比如当那个 UIView 2 靠近拐角时,让 ScrollView 稍微移动一点以完全显示 UIView 2。

如果没有办法解决这个问题,有没有办法从 UIView 2 更新 ScrollView 以在靠近角落时滚动?

编辑:

我想我可能已经找到了解决办法。不确定这是否会被 Apple 接受,但是:

我刚刚调用了一个实例变量,即 = self.superview,然后我可以在 UIView 的 touchesMoved 中与我的 ScrollView 对话

因为我可以调用方法 [ScrollView setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated] 所以当子视图(UIView2)靠近 UIWindow 的边缘时,我的 ScrollView 会得到更新。

感谢您的建议。

4

1 回答 1

3

您描述的行为是UIScrollView劫持触摸移动事件的结果。换句话说,一旦UIScrollView检测到触摸移动事件落在其框架内,它就会控制它。我在尝试创建一个特殊的滑动处理程序时遇到了同样的行为,并且每次 aUIScrollView也对滑动感兴趣时它都会失败。

就我而言,我通过sendEvent:在我的 custom中拦截事件来解决这个问题UIWindow,但我不知道你是否想这样做。无论如何,这对我有用:

- (void)sendEvent:(UIEvent*)event {
NSSet* allTouches = [event allTouches];
UITouch* touch = [allTouches anyObject];
UIView* touchView = [touch view];

//-- UIScrollViews will make touchView be nil after a few UITouchPhaseMoved events;
//-- by storing the initialView getting the touch, we can overcome this problem
if (!touchView && _initialView && touch.phase != UITouchPhaseBegan)
    touchView = _initialView;

    //-- do your own management of the event

    //-- let the event propagate if you want also the default event management
[super sendEvent:event];

}

您可能会研究的另一种方法是将手势识别器附加到您的视图 - 它们具有相当高的优先级,因此 UIScrollView 可能不会与它们混淆,它可能对您更有效。

如果没有办法解决这个问题,有没有办法从 UIView 2 更新 ScrollView 以在靠近角落时滚动?

您是否尝试UIScrollView通过调用来滚动:

- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated
于 2012-09-14T16:04:20.480 回答