轮播图2
@interface ViewController ()
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
//滑动视图UIScrollView,自带了可滑动功能
@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)UIPageControl *pageControl;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//创建初始化滑动视图
//[UIScreen mainScreen].bounds包含了屏幕的尺寸
self.scrollView=[[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
for (NSInteger i=0; i<6; i++) {
//根据i循环创建UIImageView,再添加到滑动视图scrollView上面
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(i*ScreenWidth, 0, ScreenWidth, ScreenHeight)];
NSString *imageName=nil;
if (i==5) {
imageName=@"1.jpg";
}else{
imageName=[NSString stringWithFormat:@"%ld.jpg",i+1];
}
// NSString *imageName=[NSString stringWithFormat:@"%ld.jpg",i+1];
//加载响应图片
UIImage *image=[UIImage imageNamed:imageName];
//设置图片
imageView.image=image;
//将imageView添加到滑动视图上面
[self.scrollView addSubview:imageView];
//设置滑动视图的滑动区域contentSize
self.scrollView.contentSize=CGSizeMake(6*ScreenWidth, ScreenHeight);
//整屏翻转
self.scrollView.pagingEnabled=YES;
//边界回弹
self.scrollView.bounces=NO;
//设置代理,代理是负责监听滑动视图整个滑动过程
self.scrollView.delegate=self;
//偏移量
// 开启一个定时器
//TimeInterval:时间间隔
//每隔一定的时间间隔,target会去执行selector这个方法
// [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(turnToNextImage) userInfo:nil repeats:YES];
self.pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(150, 700, 150, 20)];
self.pageControl.numberOfPages=5;
// [self.view addSubview:self.pageControl];
}
//添加滑动视图到屏幕上
[self.view addSubview:self.scrollView];
[self.view addSubview:self.pageControl];
}
//定时器触发的方法:跳转到下一张图片
-(void)turnToNextImage{
//先获取当前图片是第几张
NSInteger index=self.scrollView.contentOffset.x/ScreenWidth;
//跳转到下一张(设置偏移量)
[self.scrollView setContentOffset:CGPointMake((index+1)*ScreenWidth, 0) animated:YES];
}
//滑动视图开始滑动时调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//contentOffset 是访问到了滑动视图的偏移量,包含了x轴和y轴的偏移量
//setContentOffset:animated:
// NSLog(@"偏移量.x=%f,偏移量.y=%f",scrollView.contentOffset.x,scrollView.contentOffset.y);
//判断是否为最后一张
NSInteger index=scrollView.contentOffset.x/ScreenWidth;
//==5表示最后一张图片,如果是最后一张图片就切换到第0张图片(设置偏移量为(0,0))
if(index==5){
[scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
self.pageControl.currentPage=0;
}else{
self.pageControl.currentPage=index;
}
}
//scrollView结束减速(停止)
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//判断是否为最后一张
NSInteger index=scrollView.contentOffset.x/ScreenWidth;
//==5表示最后一张图片,如果是最后一张图片就切换到第0张图片(设置偏移量为(0,0))
if(index==5){
[scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
}
}
//滑动动画结束的时候调用
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
//判断是否为最后一张
NSInteger index=scrollView.contentOffset.x/ScreenWidth;
//==5表示最后一张图片,如果是最后一张图片就切换到第0张图片(设置偏移量为(0,0))
if(index==5){
[scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
self.pageControl.currentPage=0;
}else{
self.pageControl.currentPage=index;
}
}