//
//ViewController.m
//輪播圖
//
//Created by lanouhn on 16/7/13.
//Copyright ? 2016年lanouhn. All rights
reserved.
//
//
#import"ViewController.h"
@interfaceViewController()
#define screenWidth [UIScreenmainScreen].bounds.size.width
#define screenHeight[UIScreen mainScreen].bounds.size.height
/*UIPageControlnumOfPages
currentPage
*/
@property(nonatomic,strong)UIPageControl*pageControl;
//滑動(dòng)視圖UIScrollView,自帶了可滑動(dòng)功能
@property(nonatomic,strong)UIScrollView*scrollView;
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
//創(chuàng)建初始化滑動(dòng)視圖
//[UIScreen
mainScreen].bounds]:包含了屏幕的尺寸
self.scrollView= [[UIScrollViewalloc]initWithFrame:[UIScreenmainScreen].bounds];
for(NSIntegeri =0; i <6; i++) {
//根據(jù)i循環(huán)創(chuàng)建UIImageView臭埋。再添加到滑動(dòng)視圖scrollView上面
UIImageView*imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(i*screenWidth,0,screenWidth,screenHeight)];
NSString*imageName =nil;
if(i ==5) {
imageName =@"1.jpg";
}else{
imageName = [NSStringstringWithFormat:@"%ld.jpg",i+1];
}
//加載響應(yīng)的圖片
UIImage*image = [UIImageimageNamed:imageName];
//設(shè)置圖片
imageView.image= image;
//將imageView添加到滑動(dòng)視圖上去
[self.scrollViewaddSubview:imageView];
}
//添加滑動(dòng)視圖到屏幕上面
[self.viewaddSubview:self.scrollView];
//設(shè)置滑動(dòng)視圖區(qū)域contentSize
self.scrollView.contentSize=CGSizeMake(6*screenWidth,screenHeight);
//整屏翻轉(zhuǎn)
self.scrollView.pagingEnabled=YES;
//邊界回彈
self.scrollView.bounces=NO;
//設(shè)置代理胜榔,代理是負(fù)責(zé)監(jiān)聽滑動(dòng)視圖整個(gè)滑動(dòng)過程的
self.scrollView.delegate=self;
//開啟一個(gè)定時(shí)器
//TimeInterval:時(shí)間間隔
//每隔一定的時(shí)間間隔,target回去執(zhí)行selecto這個(gè)方法
[NSTimerscheduledTimerWithTimeInterval:2target:selfselector:@selector(turnToNextImage)userInfo:nilrepeats:YES];
}
//定時(shí)器觸發(fā)的方法:跳轉(zhuǎn)到下一張圖片
-(void)turnToNextImage
{
//先獲取當(dāng)前圖片是第幾張
NSIntegerindex =self.scrollView.contentOffset.x/screenWidth;
//跳轉(zhuǎn)到下一張
[self.scrollViewsetContentOffset:CGPointMake((index+1)*screenWidth,0)animated:YES];
self.pageControl= [[UIPageControlalloc]initWithFrame:CGRectMake(130,665,150,20)];
self.pageControl.numberOfPages=5;
[self.viewaddSubview:self.pageControl];
}
//滑動(dòng)動(dòng)畫結(jié)束的時(shí)候調(diào)用
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView*)scrollView;
{
//先獲取當(dāng)前下標(biāo)
NSIntegerindex = scrollView.contentOffset.x/screenWidth;
//是最后一張就設(shè)置偏移量為0,0
if(index ==5) {
[scrollViewsetContentOffset:CGPointMake(0,0)animated:NO];
}
}
//滑動(dòng)視圖滑動(dòng)的時(shí)候調(diào)用
- (void)scrollViewDidScroll:(UIScrollView*)scrollView
{
//contentOffset是訪問到了滑動(dòng)視圖的偏移量,包含了x和y軸的偏移量
//setContentOffset:animated:
//NSLog(@"偏移量.x = %f,偏移量.y = %f",scrollView.contentOffset.x,scrollView.contentOffset.y);
}
// scrollView結(jié)束減速(停止)
- (void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView
{
//判斷是否為最后一張
NSIntegerindex = scrollView.contentOffset.x/screenWidth;
//==5表示最后一張圖片,如果是最后一張圖片就切換到第0張圖片(設(shè)置偏移量為0捌省,0)
if(index ==5) {
[scrollViewsetContentOffset:CGPointMake(0,0)animated:NO];
self.pageControl.currentPage=0;
}else{
self.pageControl.currentPage= index;
}
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
}
@end