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 

 

转载于:https://www.cnblogs.com/HwangKop/p/4745422.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值