| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- //
- // ZLImageScrollView.m
- // ZLImageScrollViewDemo
- //
- // Created by Mr.LuDashi on 15/8/20.
- // Copyright (c) 2015年 zeluli. All rights reserved.
- //
- #import "ZLImageScrollView.h"
- @interface ZLImageScrollView()<UIScrollViewDelegate>
- @property (nonatomic, strong) UIScrollView *mainScrollView;
- @property (nonatomic, strong) UIPageControl *mainPageControl;
- @property (nonatomic, assign) CGFloat widthOfView;
- @property (nonatomic, assign) CGFloat heightView;
- @property (nonatomic, strong) NSArray *imagesNameArray;
- @property (nonatomic, strong) NSMutableArray *imageViewsArray;
- @property (nonatomic, assign) NSInteger currentPage;
- @property (nonatomic, strong) NSTimer *timer;
- @property (nonatomic, assign) UIViewContentMode imageViewcontentModel;
- @property (nonatomic, strong) UIPageControl *imageViewPageControl;
- @property (nonatomic, strong) TapImageViewButtonBlock block;
- @property (nonatomic, assign) BOOL isRight;
- @end
- @implementation ZLImageScrollView
- #pragma -- 遍历构造器
- + (instancetype) zlImageScrollViewWithFrame: (CGRect) frame
- WithImages: (NSArray *) images{
- ZLImageScrollView *instance = [[ZLImageScrollView alloc] initWithFrame:frame WithImages:images];
- return instance;
- }
- #pragma -- mark 遍历初始化方法
- - (instancetype)initWithFrame: (CGRect)frame
- WithImages: (NSArray *) images
- {
- self = [super initWithFrame:frame];
- if (self) {
- //获取滚动视图的宽度
- _widthOfView = frame.size.width;
-
- //获取滚动视图的高度
- _heightView = frame.size.height;
-
- _scrollInterval = 3;
-
- _animationInterVale = 0.7;
-
- _isRight = YES;
-
- //当前显示页面
- _currentPage = 0;
-
- _imageViewcontentModel = UIViewContentModeScaleAspectFill;
-
- self.clipsToBounds = YES;
-
- _imagesNameArray = images;
-
- //初始化滚动视图
- [self initMainScrollView];
-
- //添加ImageView
- [self addImageviewsForMainScrollWithImageView];
-
- //添加timer
- [self addTimerLoop];
-
- [self addPageControl];
-
- }
- return self;
- }
- #pragma 添加PageControl
- - (void) addPageControl{
- _imageViewPageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, _heightView - 20, _widthOfView, 20)];
-
- _imageViewPageControl.numberOfPages = _imagesNameArray.count;
-
- _imageViewPageControl.currentPage = _currentPage - 1;
-
- _imageViewPageControl.currentPageIndicatorTintColor = [UIColor blackColor];
-
- _imageViewPageControl.pageIndicatorTintColor = [UIColor whiteColor];
-
-
-
- [self addSubview:_imageViewPageControl];
- }
- - (void) addTapEventForImageWithBlock: (TapImageViewButtonBlock) block{
- if (_block == nil) {
- if (block != nil) {
- _block = block;
-
- [self initImageViewButton];
-
- }
- }
- }
- #pragma -- mark 初始化按钮
- - (void) initImageViewButton{
-
- for ( int i = 0; i < _imageViewsArray.count + 1; i ++) {
-
- CGRect currentFrame = CGRectMake(_widthOfView * i, 0, _widthOfView, _heightView);
-
- UIButton *tempButton = [[UIButton alloc] initWithFrame:currentFrame];
- [tempButton addTarget:self action:@selector(tapImageButton:) forControlEvents:UIControlEventTouchUpInside];
- if (i == 0) {
- tempButton.tag = _imageViewsArray.count;
- } else {
- tempButton.tag = i;
- }
-
- [_mainScrollView addSubview:tempButton];
- }
-
- }
- - (void) tapImageButton: (UIButton *) sender{
- if (_block) {
- _block(_currentPage + 1);
- }
- }
- #pragma -- mark 初始化ScrollView
- - (void) initMainScrollView{
-
- _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, _widthOfView, _heightView)];
-
- _mainScrollView.contentSize = CGSizeMake(_widthOfView, _heightView);
-
- _mainScrollView.pagingEnabled = YES;
-
- _mainScrollView.showsHorizontalScrollIndicator = NO;
-
- _mainScrollView.showsVerticalScrollIndicator = NO;
-
- _mainScrollView.delegate = self;
-
- [self addSubview:_mainScrollView];
- }
- #pragma -- mark 给ScrollView添加ImageView 3个ImageView
- -(void) addImageviewsForMainScrollWithImageView{
- //设置ContentSize
- _mainScrollView.contentSize = CGSizeMake(_widthOfView * 2, _heightView);
-
- _imageViewsArray = [[NSMutableArray alloc] initWithCapacity:2];
-
-
- for ( int i = 0; i < 2; i ++) {
-
- CGRect currentFrame = CGRectMake(_widthOfView * i, 0, _widthOfView, _heightView);
-
- UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:currentFrame];
-
- tempImageView.contentMode = _imageViewcontentModel;
-
- tempImageView.clipsToBounds = YES;
-
- [_mainScrollView addSubview:tempImageView];
-
- [_imageViewsArray addObject:tempImageView];
- }
-
- UIImageView *tempImageView = _imageViewsArray[0];
- [tempImageView setImage:[UIImage imageNamed:_imagesNameArray[0]]];
-
- }
- - (void) addTimerLoop{
-
- self.translatesAutoresizingMaskIntoConstraints = NO;
-
- if (_timer == nil) {
- _timer = [NSTimer scheduledTimerWithTimeInterval:_scrollInterval target:self selector:@selector(changeOffset) userInfo:nil repeats:YES];
- }
- }
- -(void) changeOffset{
-
- //获取ScrollView的offset.x
-
- _currentPage ++;
-
- //如果是最后一个图片,让其成为第一个
- if (_currentPage >= _imagesNameArray.count) {
- _currentPage = 0;
- }
-
- //将要显示的视图
- if(_currentPage < _imagesNameArray.count){
- UIImageView *tempImageView = _imageViewsArray[1] ;
- [tempImageView setImage:[UIImage imageNamed:_imagesNameArray[_currentPage]]];
- }
-
-
-
- [UIView animateWithDuration:_animationInterVale animations:^{
-
- if(_isRight){
- _mainScrollView.contentOffset = CGPointMake(_widthOfView, 0);
- } else {
- _mainScrollView.contentOffset = CGPointMake(-_widthOfView, 0);
- }
-
- } completion:^(BOOL finished) {
- //说明是用的第二个ImageView
- if (_currentPage < _imagesNameArray.count) {
-
- _mainScrollView.contentOffset = CGPointMake(0, 0);
- UIImageView *tempImageView = _imageViewsArray[0] ;
- [tempImageView setImage:[UIImage imageNamed:_imagesNameArray[_currentPage]]];
-
- }
-
-
- }];
-
- _imageViewPageControl.currentPage = _currentPage;
-
- }
- -(void) scrollViewDidScroll:(UIScrollView *)scrollView{
- CGFloat offsetx = scrollView.contentOffset.x - 0;
- if (offsetx > 3) {
- [self LoopRightWithBool:YES];
- return;
- }
-
- if (offsetx < -3) {
- [self LoopRightWithBool:NO];
- return;
- }
- }
- -(void) scrollViewWillBeginDragging:(UIScrollView *)scrollView{
- _currentPage ++;
-
- //如果是最后一个图片,让其成为第一个
- if (_currentPage >= _imagesNameArray.count) {
- _currentPage = 0;
- }
-
- //将要显示的视图
- if(_currentPage < _imagesNameArray.count){
- UIImageView *tempImageView = _imageViewsArray[1] ;
- [tempImageView setImage:[UIImage imageNamed:_imagesNameArray[_currentPage]]];
- }
-
- }
- - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
- //说明是用的第二个ImageView
- if (_currentPage < _imagesNameArray.count) {
-
- _mainScrollView.contentOffset = CGPointMake(0, 0);
- UIImageView *tempImageView = _imageViewsArray[0] ;
- [tempImageView setImage:[UIImage imageNamed:_imagesNameArray[_currentPage]]];
-
- }
-
-
- _imageViewPageControl.currentPage = _currentPage;
- [self resumeTimer];
-
-
- }
- #pragma 暂停定时器
- -(void)resumeTimer{
-
- if (![_timer isValid]) {
- return ;
- }
-
- [_timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:_scrollInterval-_animationInterVale]];
-
- }
- #pragma 改变方向
- - (void) LoopRightWithBool: (BOOL) isRight{
- _isRight =isRight;
- UIImageView *secondImageView = _imageViewsArray[1];
-
- if (isRight) {
- secondImageView.frame = CGRectMake(_widthOfView, 0, _widthOfView, _heightView);
- } else {
- secondImageView.frame = CGRectMake(-_widthOfView, 0, _widthOfView, _heightView);
- }
- }
- @end
|