-->

如何防止UIScrollView的生涩滚动,如果我有EndDecelerating,EndDragg

2019-10-28 22:48发布

我有我每次滚动的UIScrollView时间更新标签的文字...或者每次我滚动并把它停在它自己的一个点。 标签的文本此更新用基于滚动视图的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;
}

现在,在此之后,我的滚动视图变得太干,我怎么能防止滚动型的这种生涩滚动? 是否有一个共同的代表,而不是这三种方法,甚至前两名的方法呢? 在此先感谢您的帮助。

Answer 1:

这里的计算应该不会弄乱你的滚动。 麻烦的是由顶部的两种方法之间的冲突引起的:

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

您需要检查willDecelarate在第一种方法的说法。 如果这是真的,什么都不做- scrollViewDidEndDecelerating他们最终都会被调用。 如果是假的,在这里做了计算。 当willDecelarate是真的,你在呼唤从两种方法你的计算,这打乱了滚动。

由于calulations在两种情况下是相同的,你也可以将它们剔除的常用方法。

    - (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"];
}


文章来源: How can I prevent jerky scrolling of UIScrollView if I have calculations in EndDecelerating,EndDragging and DidScroll?