效果圖
#import "ViewController.h"
@interface ViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;//分頁指示器
@property (assign, nonatomic) int pageNo; //分頁碼 scrollOffset
@property(nonatomic) int pageSum;// 總頁數(shù)
@property (assign, nonatomic) CGFloat scrollMaxX; //scrollView最大x軸寬度
@property (assign, nonatomic) CGFloat scrollVisibleW;//scrollView可視區(qū)寬度
@property (assign, nonatomic) CGPoint scrollOffset;//scrollView的偏移量
@property (strong, nonatomic) NSTimer * timerImgScroll;//圖片輪播定時(shí)器
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *imageTemp;
UIImageView *imgViewText;
self.pageSum = 5;// 總頁數(shù)
CGFloat imgViewY = 0;
CGFloat imgViewW = 375;
CGFloat imgViewH = 260;
for (int i=0; i<5; i++) {
NSString *imgName = [NSString stringWithFormat:@"img_%02d",i+1];
imageTemp = [UIImage imageNamed:imgName];
imgViewText = [[UIImageView alloc]initWithImage:imageTemp];
imgViewText.frame = CGRectMake(i*imgViewW, imgViewY, imgViewW, imgViewH);
[self.scrollView addSubview:imgViewText];
}
//CGFloat imgViewMaxX = CGRectGetMaxX(imgViewText.frame);
self.scrollMaxX = CGRectGetMaxX(imgViewText.frame);
//告訴scrollView內(nèi)容有多大
self.scrollView.contentSize = CGSizeMake(self.scrollMaxX, 0);
//不顯示水平方向的滾動條
self.scrollView.showsHorizontalScrollIndicator = NO;
/*
啟用分頁控件
scrollView是以多少寬度,進(jìn)行分頁得?
答:以scrollView自身的寬度為一頁,進(jìn)行分頁得!
*/
self.scrollView.pagingEnabled = YES;
/*
PageControl
Tint Color (其它顏色) Current Color(現(xiàn)在顏色)
numberOfPages (總共多少頁) currentPage(現(xiàn)在是第幾頁)
*/
self.pageControl.numberOfPages = self.pageSum;
//設(shè)置代理對象
self.scrollView.delegate = self;
//設(shè)置scrollView可視區(qū)域的寬度
CGSize imgViewSize = self.scrollView.frame.size;
self.scrollVisibleW = imgViewSize.width;
/*
第一種方法
NSTimeInterval 每隔多少時(shí)間
target: 調(diào)用一次X的
selector: XX方法
userInfo: 發(fā)送XX消息
repeats: 是否重復(fù)
*/
self.timerImgScroll = [NSTimer scheduledTimerWithTimeInterval: 2.0 target:self selector: @selector(autoImgScoll) userInfo:nil repeats:YES];
//獲取當(dāng)前運(yùn)行的線程,將’計(jì)時(shí)器’添加到當(dāng)前線程,調(diào)整當(dāng)前線程的優(yōu)先等級
NSRunLoop *currentLoop = [NSRunLoop currentRunLoop];
[currentLoop addTimer:self.timerImgScroll forMode:NSRunLoopCommonModes];
}
//自動實(shí)現(xiàn)輪播
-(void)autoImgScoll{
self.pageNo ++;
if(self.pageNo >= self.pageSum){
self.pageNo = 0;
}
CGFloat offsetScroll = (CGFloat)self.pageNo * self.scrollVisibleW;
self.scrollView.contentOffset = CGPointMake(offsetScroll, 0);
}
/*
開始拖動,(計(jì)時(shí)器銷毀)
*/
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
NSLog(@"計(jì)時(shí)器銷毀...");
[self.timerImgScroll invalidate];
}
/*
結(jié)束拖動
*/
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
NSLog(@"開啟timerBlock計(jì)時(shí)器...");
/*
第二種,使用block方式
當(dāng)前的頁數(shù)*scrollView可視寬度 = 需要移動的偏移量
timer - block:滿足block里面的條件,方執(zhí)行timer! 不滿足則不執(zhí)行timer
*/
self.timerImgScroll = [NSTimer scheduledTimerWithTimeInterval:2.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
self.pageNo++;
if (self.pageNo >= self.pageSum) {
self.pageNo = 0;
}
CGFloat offsetScroll = (CGFloat)self.pageNo * self.scrollVisibleW;
self.scrollView.contentOffset = CGPointMake(offsetScroll, 0);
}];
//獲取當(dāng)前運(yùn)行的線程,將’計(jì)時(shí)器’添加到當(dāng)前線程,調(diào)整當(dāng)前線程的優(yōu)先等級
NSRunLoop *currentLoop = [NSRunLoop currentRunLoop];
[currentLoop addTimer:self.timerImgScroll forMode:NSRunLoopCommonModes];
}
/*
監(jiān)聽滾動事件
通過偏移量,來計(jì)算出滾動到某個(gè)寬度,則PageControl跳轉(zhuǎn)到下一個(gè)
計(jì)算公式: (滾動的偏移量 + 每頁寬度*0.5)/每頁的寬度 = 第幾頁
計(jì)算公式: 滾動的偏移量/每頁的寬度 + 0.5 = 第幾頁
*/
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSLog(@"%@",NSStringFromCGPoint(self.scrollView.contentOffset));
//獲取滾動時(shí)的偏移量
self.scrollOffset = self.scrollView.contentOffset;
//self.pageNo = scrollOffset.x/self.scrollVisibleW;
//最初方法沒有*0.5,這樣如果將圖片滾動到中間位置,pageControl不會改變?yōu)橄乱豁?就是那個(gè)點(diǎn),不會改變)!
//self.pageNo = self.scrollOffset.x /self.scrollVisibleW;
self.pageNo = (self.scrollOffset.x + self.scrollVisibleW *0.5)/self.scrollVisibleW;
self.pageControl.currentPage = self.pageNo;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end