【问题标题】:touchesEnded:withEvent: not called when a drag event moved out of the screen top/bottomtouchesEnded:withEvent: 当拖动事件移出屏幕顶部/底部时不调用
【发布时间】:2012-07-23 08:06:04
【问题描述】:

我的问题很奇怪但很简单。 我继承了我的客户 UIScrollView:MyScrollView,我在其中禁用了滚动:

 self.scrollEnabled = NO; 

这意味着除了

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView 

不会调用所有其他 UIScrollViewDelegate 方法 在 MyScrollView 中,我通过检测屏幕上的用户触摸移动来进行内容滚动,也就是说没有翻转,没有反弹,我的实现是在 touchesMoved:withEvent: 方法中

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject];
   // NSLog(@"touch.phase:%d",touch.phase);
    CGPoint currentPosition = [touch locationInView:self];
    CGPoint lastTouchPosition = [touch previousLocationInView:self];
    CGFloat deltaY = lastTouchPosition.y - currentPosition.y;
    CGFloat contentYOffset = self.contentOffset.y + deltaY;
    [self setContentOffset:CGPointMake(0,contentYOffset) animated:NO];

}

用户拖拽完成后,我根据touchesEnded:withEvent:中MyScrollView的内容偏移量做我自己的方法

 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
 {
     //some implementation according to current content offset
 }

用户在屏幕上移动手指,只要手指离开屏幕表面,就会调用 touchesEnded:withEvent: 并且我可以正确实现自己的方法。

但是,当用户在屏幕表面上将手指从屏幕内部移动到顶部或底部外部,然后抬起手指时,touchesEnded:withEvent: 方法从未被调用,似乎 ios 不处理移出边界(顶部或底部)事件作为触摸结束事件,当触摸超出屏幕边界时 ios 也不知道发生了什么

有人可能会建议我检测 touchesMoved:withEvent: 中的当前位置以检查它是否是入站。当运动非常缓慢时,这可能会起作用,但是当您移动非常快时,系统无法检测到每个点的位置,似乎是在某个时间间隔内检测到运动。 任何人都可以帮助我如何检测用户手指是否移出边界

【问题讨论】:

    标签: iphone uiscrollview uitouch


    【解决方案1】:

    我认为touchesCancelled:withEvent:方法会被调用!

    【讨论】:

    • 不,touchesCancelled:withEvent: 也没有被调用
    • 谢谢这对我有用,但是在触摸拖动(或平移)时,即使平移未完成,它也会触发。
    【解决方案2】:

    我已经解决了这个问题 bcs UIScrollView 做了太多的工作,我们无法自己处理一些事件,幸运的是 UIView 可以检测到 touche move out of bounds 并会调用touchesEnd:withEvent: 方法。 考虑到用 UIView 替换 MyScrollView 的超类有太多的工作要做,所以我想出了一个简单的解决方法: 我添加了一个从 UIView 子类化的 TouchActionDetectView,它的工作是检测所有用户触摸事件并将这些事件传递给 MyScrollView。当然我必须清除 TouchActionDetectView 的背景颜色以避免阻塞其他视图内容。

    【讨论】:

    • 是的,在这种情况下调用了 touchesEnd。
    猜你喜欢
    • 2016-12-01
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多