Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于CurrentPage的状态更新优化 #13

Open
szmichaelyb opened this issue Jun 20, 2018 · 0 comments
Open

关于CurrentPage的状态更新优化 #13

szmichaelyb opened this issue Jun 20, 2018 · 0 comments

Comments

@szmichaelyb
Copy link

szmichaelyb commented Jun 20, 2018

有一个问题,就是ScrollView滑动比较快的时候,其CurrentPage的状态更新比较迟缓。

建考虑优化一下:

#pragma mark - ------------UIScrollView代理方方法------------
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 不在这里设置currentPage --- 因为当滑动比较快速的时候,就出现currentPage更新比较慢
    // [self.imagePageControl setCurrentPage:page];
    // 这里就不用四舍五入了,因为在scroll滚动减速完成时才执行(相当于scroolView滚动静止的那一刻),四舍五入和不四舍五入,效果都一样
    int img_count = (int)self.imageArray.count;
    int page = (int)(scrollView.contentOffset.x / scrollView.frame.size.width);
    if (self.imageArray && page == img_count - 1 && self.slideInto == NO) {
        [self buttonClick:nil];
    }
    if (self.imageArray && page < img_count - 1 && self.slideInto == YES) {
        self.slideIntoNumber = 1;
    }
    if (self.imageArray && page == img_count - 1 && self.slideInto == YES) {
        UISwipeGestureRecognizer * swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:nil action:nil];
        if (swipeGestureRecognizer.direction == UISwipeGestureRecognizerDirectionRight) {
            self.slideIntoNumber++;
            if (self.slideIntoNumber == 3) {
                [self buttonClick:nil];
                // [self handleSingleTapFrom];
            }
        }
    }
}

// 这里也不能设置currentPage,状态无法及时更新
//- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
//{
//    int page = (int)(scrollView.contentOffset.x / scrollView.frame.size.width);
//    [self.imagePageControl setCurrentPage:page];
//}

// 2018-06-20 修改:新增一个代理方法,专门用来设置currentPage
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 四舍五入,保证pageControl状态跟随手指滑动刷新
    [self.imagePageControl setCurrentPage:(int)((scrollView.contentOffset.x / scrollView.frame.size.width) + 0.5f)];
}

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant