#import"ViewController.h"
@interfaceViewController()
//滑動視圖UIScrollView,自帶了可滑動功能
@property(nonatomic,strong)
UIScrollView*scrollView;
@end
@implementationViewController
#define screenWidth[UIScreen mainScreen].bounds.size.width
#define screenHeight[UIScreen mainScreen].bounds.size.height
- (void)viewDidLoad {
[superviewDidLoad];
//創(chuàng)建初始化滑動視圖
self.scrollView= [[UIScrollViewalloc]initWithFrame:[UIScreenmainScreen].bounds];
for(NSIntegeri =0; i <6; i++) {
//根據(jù)i循環(huán)創(chuàng)建UIImageView口叙,在添加到滑動視圖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添加到滑動視圖上面
[self.scrollViewaddSubview:imageView];
}
//添加滑動視圖到屏幕上
[self.viewaddSubview:self.scrollView];
//設(shè)置滑動試圖的滑動區(qū)域contenSize
self.scrollView.contentSize=CGSizeMake(6*screenWidth,screenHeight);
//整屏翻轉(zhuǎn)
self.scrollView.pagingEnabled=YES;
//邊界回彈
self.scrollView.bounces=NO;
//設(shè)置代理,代理是負(fù)責(zé)監(jiān)聽滑動試圖整個滑動過程的
self.scrollView.delegate=self;
}
//滑動視圖滑動的時候調(diào)用
- (void)scrollViewDidScroll:(UIScrollView*)scrollView;{
//contene)ffset是訪問到了滑動視圖的偏移量,包含了X和Y軸的偏移量
//sentConteneOffset:animated:
//{NSLog(@"偏移量,x = %f,y = %f",scrollView.contentOffset.x,scrollView.contentOffset.y);
}
//scrollView結(jié)束減速(停止)
- (void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView
{
//判斷是否為最后一張
NSIntegerindex = scrollView.contentOffset.x/screenWidth;
//==5表示最后一張
if(index==5) {
[scrollViewsetContentOffset:CGPointMake(0,0)animated:NO];
}
//開啟一個計時器
//TimeInterval:時間間隔
//每隔一段時間間隔镐依,target會去執(zhí)行selector這個方法
[NSTimer
scheduledTimerWithTimeInterval:2target:selfselector:@selector(turnToNextImage)userInfo:nilrepeats:YES];
}
//定時器觸發(fā)的方法拷肌;跳轉(zhuǎn)到下一張
-(void)turnToNextImage
{
//獲取當(dāng)前第幾張圖片
NSIntegerindex =self.scrollView.contentOffset.x/screenWidth;
//跳轉(zhuǎn)到下一張(設(shè)置偏移量)
[self.scrollViewsetContentOffset:CGPointMake((index+1)*screenWidth,0)animated:YES];
}
//滑動結(jié)束時候調(diào)用
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView*)scrollView;
{
//現(xiàn)獲取當(dāng)前下標(biāo)
NSIntegerindex =self.scrollView.contentOffset.x/screenWidth;if(index==5) {
[scrollViewsetContentOffset:CGPointMake(0,0)animated:NO];
}
}
- (void)didReceiveMemoryWarning{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be
recreated.
}
@end
cB