12

在 UIScrollview 中水平滚动时,如果我在同一方向快速滑动两次,滚动视图会剧烈跳跃。有没有办法防止这种情况发生?为了详细解释,这是来自滚动视图的事件日志,在大多数委托方法中,我只打印 x 坐标。

scrollViewWillBeginDragging:
    14:55:12.034 Will begin dragging!
    14:55:12.037 - Position -0.000000
scrollViewWillBeginDeceleration:
    14:55:12.129 Deceleration rate 0.998000
    14:55:12.152 + Position 314.000000
scrollViewWillBeginDragging:
    14:55:12.500 Will begin dragging!
    14:55:12.522 - Position 1211.000000
scrollViewWillBeginDeceleration:
    14:55:12.530 Deceleration rate 0.998000
    14:55:12.533 + Position 1389.000000
scrollViewDidScroll: (printing values < 0 && > 6000 (bounds.size.width)
    14:55:12.595 !!! Position 7819.000000
    14:55:12.628 !!! Position 9643.000000
    14:55:12.658 !!! Position 10213.000000
    14:55:12.688 !!! Position 10121.000000
    14:55:12.716 !!! Position 9930.000000
    ... contentoffset.x drops with around 400 each scrollviewdidscroll call ...
    14:55:13.049 !!! Position 6508.000000
scrollViewDidEndDecelerating:
    14:55:13.753 Will end deceleration
    14:55:13.761 * Position 6144.000000

日志中最值得注意的是在 scrollViewWillBeginDeceleration 之后,contentoffset.x 在几毫秒内以约 6000 点跳跃。

执行

uiscrollview 和 uiscrollviewdelegate 属于同一个类,是 uiscrollview 的子类,它也实现了 uiscrollviewdelegate 协议,对 contentoffset 没有什么特别的,在 scrollview 上设置的唯一属性是:

    self.showsHorizontalScrollIndicator = YES;
    self.scrollsToTop = NO;
    self.delegate = self;

滚动视图子视图是通过在承载 uiscrollview 的 uiviewcontroller 中的 viewwillappear 调用添加一次(并且 contentSize 已适当设置)。滚动,等待一会儿,然后再次滚动完美。

4

4 回答 4

1

在您需要的方向上设置滚动视图以及您想要滚动的坐标并使用与 XIB 的正确连接,然后它将正常工作。

于 2012-12-27T06:03:41.447 回答
0

你可以这样试试

- (void)enableScrollView
{
    scrollView.userInteractionEnabled = YES;
}

- (void)delayScroll:(id)sender
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    //delay userInteraction 0.1 second, prevent second swipe
    [NSThread sleepForTimeInterval:0.1];
    [self performSelectorOnMainThread:@selector(enableScrollView) withObject:nil waitUntilDone:NO];
    [pool release];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    scrollView.userInteractionEnabled = NO;
    [NSThread detachNewThreadSelector:@selector(delayScroll:) toTarget:self withObject:nil];
}
于 2012-02-09T06:54:13.193 回答
0

如果您希望 UIScrollView 滚动得慢一点,例如,您可以在用户滚动时添加一个 NSTimer 并使用如下动画将滚动条 contentOffset 设置得稍微靠后一点:

在 .h 文件中声明:

UIScrollView *myScrollView;
NSTimer *scrollCheckTimer;
BOOL delayScroll;

.m 文件:

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (delayScroll == YES) {
        [scrollCheckTimer invalidate];
        delayScroll = NO;
        [myScrollView setContentOffset:CGPointMake(myScrollView.contentOffset.x - 40, contentOffset.y) animated:YES];
    }
    delayScroll = YES;
    scrollCheckTimer = [NSTimer scheduledTimerWithTimeInterval:0.9 target:self selector:@selector(removeDelayBecouseOfTime) userInfo:nil repeats:NO];
}

- (void)removeDelayBecouseOfTime { delayScroll = NO; }
于 2012-05-30T04:10:14.440 回答
0

您可能需要设置bouncesZoom属性吗?

scrollviewobject.bouncesZoom = NO;
于 2011-10-08T11:12:00.647 回答