ios PageControl and UIScrollView
//
// AlbumViewController.m
// HwangKop08.18
//
// Created by rimi on 15/8/20.
// Copyright (c) 2015年 rimi. All rights reserved.
//
#import "AlbumViewController.h"
#define IMAGE_COUNT 3
@interface AlbumViewController () <UIScrollViewDelegate>
{
NSTimer *_timer;
}
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) NSMutableArray *imageNameArray;
@property (nonatomic, strong) NSMutableArray *imageViewArray;
@property (nonatomic, assign) NSInteger currentIndex;
- (void)initDataSource;
- (void)initUserInterface;
@end
@implementation AlbumViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initDataSource];
[self initUserInterface];
}
- (void)initDataSource {
self.currentIndex = 0;
self.imageNameArray = [[NSMutableArray alloc] init];
self.imageViewArray = [[NSMutableArray alloc] init];
for (int i = 0; i < 8; i ++) {
NSString *name = [NSString stringWithFormat:@"%d.png", i];
[self.imageNameArray addObject:name];
}
}
- (void)initUserInterface {
//关闭自适应scrollView边界
self.automaticallyAdjustsScrollViewInsets = NO;
UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
scrollView.delegate = self;
scrollView.backgroundColor = [UIColor orangeColor];
//设置内容大小
scrollView.contentSize = CGSizeMake(IMAGE_COUNT * CGRectGetWidth(scrollView.bounds), CGRectGetHeight(scrollView.bounds));
//设置偏移量
scrollView.contentOffset = CGPointMake(CGRectGetWidth(self.view.bounds), 0);
//开启分页
scrollView.pagingEnabled = YES;
[self.view addSubview:scrollView];
self.scrollView = scrollView;
//添加子视图
for (int i = 0; i < IMAGE_COUNT; i ++) {
CGFloat width = CGRectGetWidth(self.view.bounds);
CGFloat height = CGRectGetHeight(self.view.bounds);
CGFloat x = i * width;
//创建图片视图
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, 0, width, height)];
[scrollView addSubview:imageView];
[self.imageViewArray addObject:imageView];
}
[self dynamicLoadImage];
#pragma mark -- UIPageControl
//创建pageControl
UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 200, 375, 50)];
//设置pageControl的页数(是个小圆点)
pageControl.numberOfPages = 8;
//配置颜色 当前选中颜色和没有选中颜色
pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
pageControl.pageIndicatorTintColor = [UIColor colorWithRed:0.443 green:0.447 blue:0.435 alpha:1.000];
pageControl.currentPage = 0;
//添加点击事件
[pageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged];
self.pageControl = pageControl;
//添加到视图
[self.view insertSubview:self.pageControl atIndex:3];
}
#pragma mark -- PageContorl method - (void)pageTurn:(UIPageControl *)pageControl { NSInteger whichPage = pageControl.currentPage; [UIView setAnimationDuration:0.3]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; self.scrollView.contentOffset = CGPointMake(CGRectGetWidth(self.view.bounds) * whichPage, 0.0); }
#pragma mark -- scrollView methods
- (void)dynamicLoadImage {
for (int i = 0; i < IMAGE_COUNT; i ++) {
NSInteger index = (i - 1 + self.currentIndex + self.imageNameArray.count) % self.imageNameArray.count;
NSString *path = [[NSBundle mainBundle] pathForAuxiliaryExecutable:self.imageNameArray[index]];
UIImage *image = [UIImage imageWithContentsOfFile:path];
[self.imageViewArray[i] setImage:image];
self.scrollView.contentOffset = CGPointMake(CGRectGetWidth(self.view.bounds), 0);
}
}
- (void)pageLeft {
self.currentIndex = (--self.currentIndex + self.imageNameArray.count) % self.imageNameArray.count;
[self dynamicLoadImage];
}
- (void)pageRight {
self.currentIndex = (++self.currentIndex + self.imageNameArray.count) % self.imageNameArray.count;
[self dynamicLoadImage];
}
#pragma mark - UIScrollViewDelegate Method - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView.contentOffset.x <=0) { [self pageLeft]; } else if (scrollView.contentOffset.x >= CGRectGetWidth(scrollView.bounds) * 2) { [self pageRight]; } self.pageControl.currentPage = self.currentIndex; }
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[self pauseTimer];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[self startTimer];
}
#pragma mark -- timer methods
- (void)startTimer {
if (!_timer) {
_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(handleTimer:) userInfo:nil repeats:YES];
}
_timer.fireDate = [NSDate dateWithTimeIntervalSinceNow:2];
}
- (void)pauseTimer {
_timer.fireDate = [NSDate distantFuture];
}
- (void)stopTimer {
[_timer invalidate];
_timer = nil;
}
- (void)handleTimer:(NSTimer *)timer {
[self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self startTimer];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[self stopTimer];
}
@end
附上demo
链接:http://pan.baidu.com/s/1nt5VzRj 密码:xmr9