如果我在 EndDecelerating、EndDragging 和 DidScroll 中有计算,如何防止 UIScrollView 的滚动? [英] How can I prevent jerky scrolling of UIScrollView if I have calculations in EndDecelerating,EndDragging and DidScroll?

查看:17
本文介绍了如果我在 EndDecelerating、EndDragging 和 DidScroll 中有计算,如何防止 UIScrollView 的滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have to update text of a label every time I scroll the UIScrollView...or everytime I scroll and leave it stop at a point on it's own. This updation of the text of the label is done based on the contentoffset of the scrollview. So now I am putting checks in each method :

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

现在在此之后,我的滚动视图变得太生涩了,我该如何防止滚动视图的生涩滚动?是否有一个通用的委托而不是这三种方法甚至前两种方法?在此先感谢您的帮助.

Now after this, my scrollview becomes too jerky, how can I prevent this jerky scrolling of the scrollview? Is there a common delegate instead of these three methods or even the top two methods? Thanks in advance for any help.

推荐答案

此处的计算不应该弄乱您的滚动.问题是由前两种方法之间的冲突引起的:

The calculations here shouldn't mess up your scrolling. The trouble is caused by a conflict between the top two methods:

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

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

You need to check the willDecelarate argument in the first method. If it is true, do nothing - scrollViewDidEndDecelerating will get called eventually. If it is false, do the calc here. When willDecelarate is true you are calling your calculation from both methods, which messes up the scroll.

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

As the calulations are the same in both cases, you can also factor them out to a common method.

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

这篇关于如果我在 EndDecelerating、EndDragging 和 DidScroll 中有计算,如何防止 UIScrollView 的滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆