//
// ViewController.m
// 分页功能1
//
// Created by cdj on 17/9/10.
// Copyright © 2017年 ue. All rights reserved.
//
#import "ViewController.h"
@interface ViewController () <UIScrollViewDelegate>
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1.添加图片
CGFloat scrollViewW = self.scrollView.frame.size.width;
CGFloat scrollViewH = self.scrollView.frame.size.height;
int count = 5;
for (int i=0; i<count; i++) {
UIImageView *imageView = [[UIImageView alloc] init];
NSString *name = [NSString stringWithFormat:@"img_0%d", i+1];
imageView.image = [UIImage imageNamed:name];
imageView.frame = CGRectMake(i*scrollViewW, 0, scrollViewW, scrollViewH);
[self.scrollView addSubview:imageView];
}
// 2.设置contentSize
self.scrollView.contentSize = CGSizeMake(count * scrollViewW, scrollViewH);
// 3分页
self.scrollView.pagingEnabled = YES;
// 4.设置总页数
self.pageControl.numberOfPages = count;
self.pageControl.hidesForSinglePage = YES;
// if (count <= 1) {
//// self.pageControl.alpha = 0;
// self.pageControl.hidden = YES; //单页隐藏
// }
// 5.
[self.pageControl setValue:[UIImage imageNamed:@"current"] forKey:@"_currentPageImage"];
[self.pageControl setValue:[UIImage imageNamed:@"other"] forKey:@"_pageImage"];
}
/**
* 分页小圆点切换
*
* @param scrollView <#scrollView description#>
* @param decelerate <#decelerate description#>
*/
//方式一
/*
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
if (decelerate == NO) {
}
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
int page = scrollView.contentOffset.x / scrollView.frame.size.width;
self.pageControl.currentPage = page;
}
*/
//方式二
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
// 四舍五入 int(小数部分+0.5)
int page = scrollView.contentOffset.x / scrollView.frame.size.width + 0.5;
self.pageControl.currentPage = page;
}
@end
//
// ViewController.m
// 分页功能1
//
// Created by cdj on 17/9/10.
// Copyright © 2017年 ue. All rights reserved.
//
#import "ViewController.h"
@interface ViewController () <UIScrollViewDelegate>
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property (nonatomic, weak) NSTimer *timer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1.添加图片
CGFloat scrollViewW = self.scrollView.frame.size.width;
CGFloat scrollViewH = self.scrollView.frame.size.height;
int count = 5;
for (int i=0; i<count; i++) {
UIImageView *imageView = [[UIImageView alloc] init];
NSString *name = [NSString stringWithFormat:@"img_0%d", i+1];
imageView.image = [UIImage imageNamed:name];
imageView.frame = CGRectMake(i*scrollViewW, 0, scrollViewW, scrollViewH);
[self.scrollView addSubview:imageView];
}
// 2.设置contentSize
self.scrollView.contentSize = CGSizeMake(count * scrollViewW, scrollViewH);
// 3分页
self.scrollView.pagingEnabled = YES;
// 4.设置总页数
self.pageControl.numberOfPages = count;
self.pageControl.hidesForSinglePage = YES;
// if (count <= 1) {
//// self.pageControl.alpha = 0;
// self.pageControl.hidden = YES; //单页隐藏
// }
// 5.
[self.pageControl setValue:[UIImage imageNamed:@"current"] forKey:@"_currentPageImage"];
[self.pageControl setValue:[UIImage imageNamed:@"other"] forKey:@"_pageImage"];
// 6.定时器,自动切换图片
[self startTimer];
}
/**
* 分页小圆点切换
*
* @param scrollView <#scrollView description#>
* @param decelerate <#decelerate description#>
*/
//方式一
/*
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
if (decelerate == NO) {
}
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
int page = scrollView.contentOffset.x / scrollView.frame.size.width;
self.pageControl.currentPage = page;
}
*/
//方式二
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
// 四舍五入 int(小数部分+0.5)
int page = scrollView.contentOffset.x / scrollView.frame.size.width + 0.5;
self.pageControl.currentPage = page;
}
-(void) nextPage:(NSTimer *)timer{
NSLog(@"nextPage=%@", timer.userInfo);
int page = self.pageControl.currentPage + 1;
if (page >= 5) {
page = 0;
}
[self.scrollView setContentOffset:CGPointMake(page * self.scrollView.frame.size.width, 0) animated:YES];
}
/**
* 用户即将开启拖拽scrollView时,停止定时器
*
* @param scrollView <#scrollView description#>
*/
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
[self stopTimer];
}
/**
* 用户已经停止拖拽scrollView时,开启定时器
*
* @param scrollView <#scrollView description#>
* @param decelerate <#decelerate description#>
*/
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
[self startTimer];
}
-(void)startTimer{
self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextPage:) userInfo:@"123" repeats:YES];
}
-(void)stopTimer{
[self.timer invalidate];
self.timer = nil;
}
@end