实现图片的循环滚动播放 - iOS开发

前言

相信大家在项目中很多都遇到过这样的需求,要求实现图片的循环滚动播放,我这里使用scrollview实现,先来说大体思路:首先创建一个scrollview,将其等分为三等份,然后根据定时器触发滚动,当滚动到最后或者第一个区域时,将当前的contentoffset手动设置到中间2号区域,相当于每次的滚动完之后当前位置都在2号区域;接下来,就看代码了

实现图片的循环滚动播放 - iOS开发_第1张图片
image

代码实现

首先,设置好scrollview,并且拿到需要显示的images,我这快使用代理模式,拿到需要显示的图片总数和当前三个区域需要显示的图片,然后设置当前contenoffset为二号区域,最后开启定时器,进行滚动播放,这里的imageviews实现了复用,只需要创建三个imageview:

- (void)reloadData
{
    _currentPage = 0;
    _totalPageCount = 0;
    if (self.delegate && [self.delegate respondsToSelector:@selector(numberOfTotalScrollCount)]) {
        _totalPageCount = [self.delegate numberOfTotalScrollCount];
        if (_totalPageCount <= 1) {
            [_timer pause];
        }
    }else{
    }
    [self resetScrollView];
}
- (void)resetScrollView
{
    [_scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    NSInteger preIndex = [self getPreIndex:_currentPage];
    NSInteger curIndex = _currentPage;
    NSInteger nextIndex = [self getNextIndex:_currentPage];
    if (self.delegate && [self.delegate respondsToSelector:@selector(imageForScrollInIndex:)]) {
        UIImage *preImage = [self.delegate imageForScrollInIndex:preIndex];
        UIImage *curImage = [self.delegate imageForScrollInIndex:curIndex];
        UIImage *nextImage = [self.delegate imageForScrollInIndex:nextIndex];
        NSArray *images = @[preImage, curImage, nextImage];
        for (int i = 0; i < images.count; i++) {
            UIImageView *imageView = _imageViews[i];
            switch (_direction) {
                case ScrollPictureDirectionRight:
                case ScrollPictureDirectionLeft:
                    [imageView setFrame:CGRectMake(i * OwnWidth_LCY, 0, OwnWidth_LCY, OwnHeight_LCY)];
                    break;
                case ScrollPictureDirectionTop:
                case ScrollPictureDirectionBottom:
                    [imageView setFrame:CGRectMake(0, i * OwnHeight_LCY, OwnWidth_LCY, OwnHeight_LCY)];
                    break;
                default:
                    break;
            }
            [imageView setImage:images[i]];
            [_scrollView addSubview:imageView];
        }
        [_scrollView setContentOffset:(_direction == ScrollPictureDirectionRight || _direction == ScrollPictureDirectionLeft) ? CGPointMake(OwnWidth_LCY, 0) : CGPointMake(0, OwnHeight_LCY)];
    }
}

在定时器中,我们需要实现scrollview的滚动,即,根据方向设置其contentoffset代码如下:

- (void)start:(NSTimer *)timer
{
    CGFloat contentOffsetX = _scrollView.contentOffset.x;
    CGFloat contentOffSetY = _scrollView.contentOffset.y;
    switch (_direction) {
        case ScrollPictureDirectionLeft:
            contentOffsetX += OwnWidth_LCY;
            break;
        case ScrollPictureDirectionRight:
            contentOffsetX -= OwnWidth_LCY;
            break;
        case ScrollPictureDirectionTop:
            contentOffSetY += OwnHeight_LCY;
            break;
        case ScrollPictureDirectionBottom:
            contentOffSetY -= OwnHeight_LCY;
            break;
        default:
            break;
    }
    CGPoint contentOffset = CGPointMake(contentOffsetX, contentOffSetY);
    [_scrollView setContentOffset:contentOffset animated:YES];
}

边界处理,当当前显示的图片是数组中最后一个或者第一个图片时,需要进行处理,刷新当前需要显示的image;
那么我们在什么时候得知试图当前的滚动位置呢,无疑我们应该在其代理函数里面获取,并且进行处理,这里我实现了上下左右四个方向的滚动,可以根据参数选取不同的方向,原理是一样的:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint contentSize = _scrollView.contentOffset;
    switch (_direction) {
        case ScrollPictureDirectionRight:
        {
            if (contentSize.x <= 0){
                _currentPage = [self getPreIndex:_currentPage];
                [self resetScrollView];
                if (self.delegate && [self.delegate respondsToSelector:@selector(scrollAtTheIndex:)]) {
                    [self.delegate scrollAtTheIndex:_currentPage];
                }
            }
        }
            break;
        case ScrollPictureDirectionBottom:
        {
            if (contentSize.y <= 0){
                _currentPage = [self getPreIndex:_currentPage];
                [self resetScrollView];
                if (self.delegate && [self.delegate respondsToSelector:@selector(scrollAtTheIndex:)]) {
                    [self.delegate scrollAtTheIndex:_currentPage];
                }
            }
        }
            break;
        case ScrollPictureDirectionLeft:
        {
            
            if (contentSize.x >= OwnWidth_LCY * 2) {
                _currentPage = [self getNextIndex:_currentPage];
                [self resetScrollView];
                if (self.delegate && [self.delegate respondsToSelector:@selector(scrollAtTheIndex:)]) {
                    [self.delegate scrollAtTheIndex:_currentPage];
                }
            }
        }
            break;
        case ScrollPictureDirectionTop:
        {
            
            if (contentSize.y >= OwnHeight_LCY * 2) {
                _currentPage = [self getNextIndex:_currentPage];
                [self resetScrollView];
                if (self.delegate && [self.delegate respondsToSelector:@selector(scrollAtTheIndex:)]) {
                    [self.delegate scrollAtTheIndex:_currentPage];
                }
            }
        }
            break;
        default:
            break;
    }
}
- (NSInteger)getPreIndex:(NSInteger)currentIndex
{
    if (currentIndex == 0) {
        return _totalPageCount - 1;
    }
    return currentIndex - 1;
}

- (NSInteger)getNextIndex:(NSInteger)currentIndex
{
    if (currentIndex == _totalPageCount - 1) {
        return 0;
    }
    return currentIndex + 1;
}

至此,所有的的关键代码就结束了,这里的关键点是要理解当scrollview滚动到1号或者3号区域时,需要重新设置当前的contentoffset为2号区域,以此来实现无限滚动;

ps:这里是代码链接和使用~~

你可能感兴趣的:(实现图片的循环滚动播放 - iOS开发)