Skip to content

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

@szmichaelyb

Description

@szmichaelyb

有一个问题,就是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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions