iOS實現(xiàn)圖片輪播器
有時候肯能會用到圖片輪播器,做廣告的效果。下面詳細介紹iOS如何實現(xiàn)圖片輪播器
1.新建一個項目,導入5張圖片(為了代碼方便,我把圖片命名規(guī)范了,其實無所謂)

2.在mainstoryboard中拖入ScrollView和Page Control(也可以代碼寫,或者是自定義xib)
設置page control的 Current Page屬性,決定輪播的當前頁顯示的顏色

3.接下來就是正式代碼了
//(1)將需要展⽰的內容添加到UIScrollView中
//(2)設置UIScrollView的contentSize屬性,告訴UIScrollView所有內容的尺寸,也就是告訴 它滾動的范圍(能滾多遠,滾到哪⾥是盡頭)
#define imageCount 5
#import "ViewController.h"
@interface ViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property(nonatomic,strong)NSTimer *timer;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//1.添加5張圖片到scrollView中
//設置圖片frame,尺寸與scrollView一樣高
CGFloat imageW=self.scrollView.frame.size.width;
CGFloat imageH=self.scrollView.frame.size.height;
//圖片的具體位置需要動態(tài)計算
CGFloat imageY=0;
for (int i=0; i<imageCount; i++)
{
UIImageView *imageView=[[UIImageView alloc]init];
CGFloat imageX=i*imageW;
imageView.frame=CGRectMake(imageX, imageY, imageW, imageH);
//設置圖片
NSString *name=[NSString stringWithFormat:@"img_0%d",i+1];
imageView.image=[UIImage imageNamed:name];
[self.scrollView addSubview:imageView];
}
//2.設置滾動內容的尺寸
CGFloat contentW=5*imageW;
self.scrollView.contentSize=CGSizeMake(contentW, 0);
//3.隱藏水平的滾動條
self.scrollView.showsHorizontalScrollIndicator=NO;
//4.分頁
self.pageControl.enabled=YES;
//5.設置代理
self.scrollView.delegate=self;
//6.設置pageControl的總頁數(shù)
self.pageControl.numberOfPages=imageCount;
//7.添加定時器
self.timer=[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
}
-(void)nextImage
{
int page=0;
if(self.pageControl.currentPage==imageCount-1)
{
//如果滾動到最后一頁了,那下一頁就是第一頁
page=0;
}
else
{
//否則就是下一頁
page=(int)self.pageControl.currentPage+1;
}
//2.計算scrollView滾動的位置
CGFloat offsetX=page*self.scrollView.frame.size.width;
CGPoint offset=CGPointMake(offsetX, 0);
[self.scrollView setContentOffset:offset animated:YES];
}
//開始拖拽的時候調用
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
//停止定時器
[self.timer invalidate];
self.timer=nil;
}
//停止拖拽的時候調用
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
//再次開啟定時器
self.timer=[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
}
//當scrollView正在滾動就會調用
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//根據(jù)scrollView的滾動位置決定pageControl顯示第幾頁
int page=(scrollView.contentOffset.x+self.scrollView.frame.size.width*0.5)/scrollView.frame.size.width;
self.pageControl.currentPage=page;
}
4.效果圖:

5.本圖片輪播器解決了兩個比較關鍵的問題:
(1)當用戶拖拽的時候,定時器是停止的,用戶松開的時候,定時器又起來了。防止用戶長時間拽著某圖片不放,突然松開后,瞬間往后跳過去。
(2)判斷了當前顯示頁。當后面的圖片出現(xiàn)在scrollView超過1/2的距離時,就表明是下一頁了,綠點就跑到下一頁去。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
iOS App開發(fā)中的UIPageControl分頁控件使用小結
UIPageControl分頁控件的例子簡單來說即是我們平時翻動多個桌面頁時及底部帶有的圓點頁碼標注,這里我們來看一下iOS App開發(fā)中的UIPageControl分頁控件使用小結,需要的朋友可以參考下2016-06-06
IOS設備上給body綁定click事件不生效的原因及解決辦法
最近在做移動端的項目,在ios上對body綁定click事件實現(xiàn)事件代理冒泡至某些元素上不生效,怎么回事,如何解決呢?今天小編給大家?guī)砹薎OS設備上給body綁定click事件不生效的原因及解決辦法,一起看看吧2016-09-09
iOS中創(chuàng)建表格類視圖WBDataGridView的實例代碼
這篇文章主要介紹了iOS中創(chuàng)建表格類視圖WBDataGridView的實例代碼,需要的朋友可以參考下2017-02-02

