如果我在EndDecelerating,EndDragging和DidScroll中进行计算,如何防止UIScrollView的不稳定滚动?

时间:2013-07-05 06:47:26

标签: objective-c uiscrollview smooth-scrolling contentoffset

每次滚动UIScrollView时,我都必须更新标签文本...或者每当我滚动并让它停在它自己的某个点上时。标签文本的这种更新是基于scrollview的contentoffset完成的。所以现在我正在对每种方法进行检查:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
     int one = scrollView.contentOffset.x/21;
     int two = (21*one)+14;
     CGPoint point = CGPointMake(two, scrollView.contentOffset.y);
     [scrollView setContentOffset:point animated:YES];
     [self setLabelText:@"scroll"];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    int one = scrollView.contentOffset.x/21;
    int two = (21*one)+14;
    CGPoint point = CGPointMake(two, scrollView.contentOffset.y);
    [scrollView setContentOffset:point animated:YES];
    [self setLabelText:@"scroll"];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGPoint offset = scrollView.contentOffset;
    if (offset.x < minuteScrollMinX) offset.x = minuteScrollMinX;
    if (offset.x > minuteScrollMaxX) offset.x = minuteScrollMaxX;
    scrollView.contentOffset = offset;
}

此后,我的滚动视图变得太生涩,我怎么能阻止滚动视图的这种不稳定的滚动?是否有共同的代表而不是这三种方法甚至是前两种方法? 提前感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

这里的计算不应该弄乱你的滚动。麻烦是由前两种方法之间的冲突造成的:

 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;

您需要在第一种方法中检查willDecelarate参数。如果是真的,什么都不做 - scrollViewDidEndDecelerating最终会被调用。如果是假,请在此处进行计算。当willDecelarate为真时,你从两种方法调用计算,这会弄乱滚动。

由于两种情况下的计算方法相同,您也可以将它们分解为常用方法。

    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
         [self calculateScrollOffset];
    }

    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView 
                      willDecelerate:(BOOL)decelerate
    {
        if (!decelerate) {
             [self calculateScrollOffset];
             }
    }


- (void) calculateScrollOffset
{
    int one = scrollView.contentOffset.x/21;
     int two = (21*one)+14;
     CGPoint point = CGPointMake(two, scrollView.contentOffset.y);
     [scrollView setContentOffset:point animated:YES];
     [self setLabelText:@"scroll"];
}