scrollView三张图片循环滑动

思想:总是让显示的图片在中间位置

#import "ViewController.h"

@interface ViewController ()
{
    CGFloat _screenWidth;
    UIScrollView *_scrollView;
    UIPageControl *_pageControl;
    NSArray *_images;
    NSMutableArray *_views;
    
    NSInteger _lastIndex;
    NSInteger _currentIndex;
    NSInteger _nextIndex;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _views = [NSMutableArray array];
    _screenWidth = self.view.bounds.size.width;
    _images = @[[UIImage imageNamed:@"image1.jpg"],[UIImage imageNamed:@"image2.jpg"],[UIImage imageNamed:@"image3.jpg"]];
    _currentIndex = 0;
    [self changeIndex];
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, _screenWidth, 200)];
    _scrollView.contentSize = CGSizeMake(_screenWidth * _images.count, 0);
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    _scrollView.showsHorizontalScrollIndicator = NO;
    [_scrollView setContentOffset:CGPointMake(_screenWidth, 0) animated:NO];
    [self.view addSubview:_scrollView];
    
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 170, _screenWidth, 20)];
    _pageControl.numberOfPages = _images.count;
    _pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
    _pageControl.pageIndicatorTintColor = [UIColor grayColor];
    [self.view addSubview:_pageControl];
    
    for (int i = 0; i < _images.count; i++) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(i * _screenWidth, 0, _screenWidth, 200)];
        [_scrollView addSubview:view];
        [_views addObject:view];
    }
    UIView *lastView = _views[0];
    UIView *currentView = _views[1];
    UIView *nextView = _views[2];
    [self changeImageWithLastView:lastView andCurrentView:currentView andNextView:nextView];
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    UIView *lastView = _views[0];
    UIView *currentView = _views[1];
    UIView *nextView = _views[2];
    //向左滑
    if (scrollView.contentOffset.x >= 2 *_screenWidth) {
        _currentIndex += 1;
        if (_currentIndex > _images.count - 1) {
            _currentIndex = 0;
        }
        [self changeIndex];
        [self changeImageWithLastView:lastView andCurrentView:currentView andNextView:nextView];
    }
    //向右滑
    if (scrollView.contentOffset.x <= 0) {
        _currentIndex -= 1;
        if (_currentIndex < 0) {
            _currentIndex = _images.count - 1;
        }
        [self changeIndex];
        [self changeImageWithLastView:lastView andCurrentView:currentView andNextView:nextView];
        
    }
    //改变pageControlD的当前小点
    _pageControl.currentPage = _currentIndex;
    [scrollView setContentOffset:CGPointMake(_screenWidth, 0) animated:NO];
    
}

- (void)changeIndex {
    _lastIndex = _currentIndex - 1;
    _nextIndex = _currentIndex + 1;
    if (_lastIndex < 0) {
        _lastIndex = _images.count - 1;
    }
    if (_nextIndex >= _images.count) {
        _nextIndex = 0;
    }
}
//改变view的图片
- (void)changeImageWithLastView:(UIView *)lastView andCurrentView:(UIView*)currentView andNextView:(UIView*)nextView {

    UIImage *lastImg = _images[_lastIndex];
    UIImage *currentImg = _images[_currentIndex];
    UIImage *nextImg = _images[_nextIndex];
    lastView.layer.contents = (__bridge id) lastImg.CGImage;
    currentView.layer.contents = (__bridge id) currentImg.CGImage;
    nextView.layer.contents = (__bridge id) nextImg.CGImage;
}

@end

你可能感兴趣的:(scrollView三张图片循环滑动)