輪播圖

//

// ?ViewController.m

// ?輪播圖

//

// ?Created by lanou on 16/7/13.

// ?Copyright ? 2016年 lanou. All rights reserved.

//

#import "ViewController.h"

@interface ViewController ()

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

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

@property(nonatomic,strong)UIPageControl *pageControl;

//滑動視圖UIScrollView,自帶了可滑動功能

@property(nonatomic,strong)UIScrollView *scrollView;

@end

@implementation ViewController

- (void)viewDidLoad {

? ?[super viewDidLoad];

// ? ?創(chuàng)建初始化滑動視圖

// ? ?[UIScreen mainScreen].bounds包含了屏幕尺寸

? ?self.scrollView=[[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];

? ?for (NSInteger i=0; i<6; i++) {

// ? ?根據(jù)i循環(huán)創(chuàng)建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];

? ? ? ?}

// ? ? ? ?加載響應(yīng)的圖片

? ? ? ?UIImage *image=[UIImage imageNamed:imageName];

// ? ? ? ?設(shè)置圖片

? ? ? ?imageView.image=image;

// ? ? ? ?將imageView添加到滑動視圖上

? ? ? ?[self.scrollView addSubview:imageView];

? ?}

// ? ?添加滑動視圖到屏幕上

? ?[self.view addSubview:self.scrollView];

// ? ?設(shè)置滑動視圖的滑動區(qū)域contentSize

? ?self.scrollView.contentSize=CGSizeMake(6*screenWidth, screenHeight);

// ? ?整屏翻轉(zhuǎn)

? ?self.scrollView.pagingEnabled=YES;

// ? ?邊界回彈

? ?self.scrollView.bounces=YES;

// ? ?設(shè)置代理,代理是負(fù)責(zé)監(jiān)聽滑動視圖整個(gè)滑動過程的

? ?self.scrollView.delegate=self;

// ? ?開啟一個(gè)定時(shí)器

// ? ?TimeInterval:時(shí)間間隔

// ? ?每隔一定的時(shí)間隔,target會去執(zhí)行selector這個(gè)方法

// ? ?[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(turnToNextImage) userInfo:(nil) repeats:YES];

? ?self.pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(80, 500, 150, 20)];

? ?self.pageControl.numberOfPages=5;

? ?[self.view addSubview:self.pageControl];

? ?}

//定時(shí)器觸發(fā)的方法:跳轉(zhuǎn)到下一張圖片

-(void)turnToNextImage

{

// ? ?先獲取當(dāng)前圖片是第幾張

? ?NSInteger index=self.scrollView.contentOffset.x/screenWidth;

// ? ?跳轉(zhuǎn)到下一張(設(shè)置偏移量)

? ?[self.scrollView setContentOffset:CGPointMake((index+1)*screenWidth, 0) animated:YES];

}

//滑動視圖滑動的時(shí)候調(diào)用

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

// ? ?contentOffset是訪問到了滑動視圖的偏移量,包含了x和y軸的偏移量

// ? ?setContentOffset:animated:

// ? ?NSLog(@"offset.x=%f,offset.y=%f",scrollView.contentOffset.x,scrollView.contentOffset.y);

}

//scrollView結(jié)束減速(停止)

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

// 判斷是否為最后一張

? ?NSInteger index=scrollView.contentOffset.x/screenWidth;

// ? ?5表示最后一張圖片,如果是最后一張圖片就切換到第0張圖片(設(shè)置偏移量為0,0)

? ?if (index==5) {

? ? ? ?[scrollView setContentOffset:CGPointMake(0, 0) animated:YES];

? ? ? ?self.pageControl.currentPage=0;

? ?}else{

? ? ? ?self.pageControl.currentPage=index;

? ?}

}

//滑動結(jié)束的時(shí)候調(diào)用

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView

{

// ? ?先獲取當(dāng)前下標(biāo)

? ?NSInteger index=scrollView.contentOffset.x/screenWidth;

// ? ?是最后一張就設(shè)置偏移量為0,0

? ?if (index==5) {

? ? ? ?[scrollView setContentOffset:CGPointMake(0, 0) animated:NO];

? ? ? ?self.pageControl.currentPage=0;

? ?}else{

? ? ? ?self.pageControl.currentPage=index;

? ?}

}

- (void)didReceiveMemoryWarning {

? ?[super didReceiveMemoryWarning];

? ?// Dispose of any resources that can be recreated.

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容