今天學(xué)了輪播圖的制作 ?原來(lái)在大街上和網(wǎng)上經(jīng)炒鸲瘢看到的東西是這樣做出來(lái)的。恒序。
我認(rèn)為輪播圖最重要的就是定時(shí)這個(gè)東西,因?yàn)檩啿D就像是幻燈片一樣的東西谁撼,還有循環(huán)歧胁,把最后一個(gè)設(shè)置成和第一張圖片一樣的“假圖片”也是很重要的。
//ViewController.m
//輪播圖
//
//Created by lanouhn on 16/7/13.
//Copyright ? 2016年lanouhn. All rights reserved.
//
//
#import"ViewController.h"
@interfaceViewController()
#define screenWidth [UIScreen mainScreen].bounds.size.width
#define screenHeight [UIScreenmainScreen].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)聽(tīng)滑動(dòng)視圖整個(gè)滑動(dòng)過(guò)程的
self.scrollView.delegate=self;
//開(kāi)啟一個(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)畫(huà)結(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是訪問(wèn)到了滑動(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