iOS之UIScrollView循环滚动

时间:2022-10-08 11:08:44

#import "ViewController.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define IMAGEVIEW_COUNT 3

@interface ViewController () <UIScrollViewDelegate> {

UIScrollView *_scrollView;

UIImageView *_leftImageView;

UIImageView *_centerImageView;

UIImageView *_rightImageView;

UIPageControl *_pageControl;

UILabel *_label;

NSMutableDictionary *_imageData;//图片数据

int _currentImageIndex;//当前图片索引

int _imageCount;//图片总数

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

//加载数据

[self loadImageData];

//添加滚动控件

[self addScrollView];

//添加图片控件

[self addImageViews];

//添加分页控件

[self addPageControl];

//添加图片信息描述控件

[self addLabel];

//加载默认图片

[self setDefaultImage];

}

#pragma mark 加载图片数据

-(void)loadImageData {

//读取程序包路径中的资源文件

NSString *path = [[NSBundle mainBundle] pathForResource:@"imageInfo" ofType:@"plist"];

_imageData = [NSMutableDictionary dictionaryWithContentsOfFile:path];

_imageCount = (int)_imageData.count;

}

#pragma mark 添加控件

-(void)addScrollView {

//    _scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];

_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20+20, SCREEN_WIDTH, 200)];

_scrollView.backgroundColor = [UIColor lightGrayColor];

//设置代理

_scrollView.delegate = self;

//设置滚动范围

_scrollView.contentSize = CGSizeMake(IMAGEVIEW_COUNT*SCREEN_WIDTH, 0);

//设置当前显示的位置为中间图片(设置scrollView偏移量)

[_scrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0)];

//设置分页

_scrollView.pagingEnabled = YES;

//隐藏水平条 竖向条

_scrollView.showsHorizontalScrollIndicator = NO;

_scrollView.showsVerticalScrollIndicator = NO;

//关闭弹簧效果

//    _scrollView.bounces = NO;

[self.view addSubview:_scrollView];

}

#pragma mark 添加图片三个控件

-(void)addImageViews {

_leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, _scrollView.frame.size.height)];

//会保证图片比例不变,而且全部显示在ImageView中,这意味着ImageView会有部分空白

_leftImageView.contentMode=UIViewContentModeScaleAspectFit;

[_scrollView addSubview:_leftImageView];

_centerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, _scrollView.frame.size.height)];

_centerImageView.contentMode = UIViewContentModeScaleAspectFit;

[_scrollView addSubview:_centerImageView];

_rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(2*SCREEN_WIDTH, 0, SCREEN_WIDTH, _scrollView.frame.size.height)];

_rightImageView.contentMode = UIViewContentModeScaleAspectFit;

[_scrollView addSubview:_rightImageView];

}

#pragma mark 设置默认显示图片

-(void)setDefaultImage {

//加载默认图片

_leftImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.jpeg",_imageCount-1]];

_centerImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.jpeg",0]];

_rightImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.jpeg",1]];

_currentImageIndex = 0;

//设置当前页

_pageControl.currentPage = _currentImageIndex;

NSString *imageName = [NSString stringWithFormat:@"%i.jpeg",_currentImageIndex];

_label.text = _imageData[imageName];

}

#pragma mark 添加分页控件

-(void)addPageControl {

_pageControl = [[UIPageControl alloc] init];

//    _pageControl.backgroundColor = [UIColor orangeColor];

//注意此方法可以根据页数返回UIPageControl合适的大小

CGSize size = [_pageControl sizeForNumberOfPages:_imageCount];

_pageControl.bounds = CGRectMake(0, 0, size.width, size.height);

_pageControl.center = CGPointMake(SCREEN_WIDTH*2/3, _scrollView.frame.origin.y+_scrollView.frame.size.height-10);

//设置颜色

_pageControl.pageIndicatorTintColor = [UIColor colorWithRed:193/255.0 green:219/255.0 blue:249/255.0 alpha:1];

//设置当前页颜色

_pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:0 green:150/255.0 blue:1 alpha:1];

//设置总页数

_pageControl.numberOfPages = _imageCount;

[self.view addSubview:_pageControl];

}

#pragma mark 添加信息描述控件

-(void)addLabel {

_label = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 20)];

//    _label.backgroundColor = [UIColor orangeColor];

_label.textAlignment = NSTextAlignmentCenter;

_label.textColor = [UIColor colorWithRed:0 green:150/255.0 blue:1 alpha:1];

[self.view addSubview:_label];

}

#pragma mark 滚动停止事件

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

//重新加载图片

[self reloadImage];

//移动到中间

[_scrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0)];

//设置分页

_pageControl.currentPage = _currentImageIndex;

//设置描述

NSString *imageName = [NSString stringWithFormat:@"%i.jpeg",_currentImageIndex];

_label.text = _imageData[imageName];

}

#pragma mark 重新加载图片

-(void)reloadImage {

int leftImageIndex,rightImageIndex;

CGPoint offset = [_scrollView contentOffset];

if (offset.x > SCREEN_WIDTH) { //向右滑动

_currentImageIndex = (_currentImageIndex+1)%_imageCount;

}else if(offset.x < SCREEN_WIDTH) { //向左滑动

_currentImageIndex = (_currentImageIndex+_imageCount-1)%_imageCount;

}

_centerImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.jpeg",_currentImageIndex]];

//重新设置左右图片

leftImageIndex = (_currentImageIndex+_imageCount-1)%_imageCount;

rightImageIndex = (_currentImageIndex+1)%_imageCount;

_leftImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.jpeg",leftImageIndex]];

_rightImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.jpeg",rightImageIndex]];

}

@end