//
// YZScro.m
// scrollViewTest
//
// Created by yuzhong.jing on 16/4/27.
// Copyright © 2016年 yuzhong.jing. All rights reserved.
//
#import "YZScro.h"
#define kHieght self.frame.size.height
#define kWidth self.frame.size.width
@implementation YZScro
{
UIScrollView *_myScroll;
UIImageView *_leftImageView;
UIImageView *_centerImageView;
UIImageView *_rightimageView;
UIPageControl *_pageControl;
NSTimer *_myTimer;
NSArray *_myImageArray;//图片arr
NSInteger _currentIndex;//当前
NSInteger _maxImageCount;//最大
}
-(instancetype)initWithFrame:(CGRect)frame withImageArr:(NSArray *)imageArr{//初始化方法,图片数量大于2
if (imageArr.count<2) {
return nil;
}
if (self =[super initWithFrame:frame]) {
//创建scroll
[self createScrollView];
_myImageArray = imageArr;
[self setMaxImageViewCount:_myImageArray.count];
}
return self;
}
- (void)setMaxImageViewCount:(NSInteger)maxCount{
_maxImageCount =maxCount;
//创建imageView
[self createImageViewWithImageName];
//创建pageControl
[self createPageControlWithPahe];
//开启定时器
[self createTimer];
[self changeImageLeft:_maxImageCount-1 center:0 right:1];
}
- (void)createScrollView{
UIScrollView *scrollView =[[UIScrollView alloc]initWithFrame:self.bounds];
[self addSubview:scrollView];
_myScroll =scrollView;
_myScroll.backgroundColor =[UIColor clearColor];
_myScroll.pagingEnabled =YES;
_myScroll.showsHorizontalScrollIndicator =NO;
_myScroll.delegate =self;
_myScroll.contentSize =CGSizeMake(kWidth*3, 0);
_currentIndex =0;
}
- (void)createImageViewWithImageName{
UIImageView *left =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHieght)];
UIImageView *center = [[UIImageView alloc]initWithFrame:CGRectMake(kWidth, 0, kWidth, kHieght)];
center.userInteractionEnabled =YES;
UIImageView *right =[[UIImageView alloc]initWithFrame:CGRectMake(kWidth*2, 0, kWidth, kHieght)];
UITapGestureRecognizer *tapCenter =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapCenter)];
[center addGestureRecognizer:tapCenter];
[_myScroll addSubview:left];
[_myScroll addSubview:center];
[_myScroll addSubview:right];
_leftImageView =left;
_centerImageView =center;
_rightimageView =right;
}
- (void)tapCenter{
NSLog(@"%@",_myImageArray[_currentIndex]);
}
- (void)createPageControlWithPahe{
UIPageControl *control =[[UIPageControl alloc]initWithFrame:CGRectMake(kWidth/2-40, 190, 80, 20)];
// control.backgroundColor =[UIColor yellowColor];
_pageControl =control;
_pageControl.numberOfPages =_maxImageCount;
_pageControl.currentPage =0;
[self addSubview:_pageControl];
}
- (void)createTimer{
_myTimer =[NSTimer timerWithTimeInterval:2 target:self selector:@selector(scroll) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop]addTimer:_myTimer forMode:NSRunLoopCommonModes];
}
- (void)scroll{
NSLog(@"开启");
[_myScroll setContentOffset:CGPointMake(_myScroll.contentOffset.x+kWidth, 0) animated:YES];
}
- (void)endScroll{
if (_myTimer == nil) {
return;
}
[_myTimer invalidate];
_myTimer=nil;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
[self createTimer];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
[self endScroll];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
[self changeImageWithOffset:scrollView.contentOffset.x];
}
- (void)changeImageWithOffset:(CGFloat)offsetX{
if (offsetX >= kWidth * 2) {
_currentIndex++;
if (_currentIndex == _maxImageCount-1) {
[self changeImageLeft:_currentIndex-1 center:_currentIndex right:0];
}else if (_currentIndex == _maxImageCount) {
_currentIndex = 0;
[self changeImageLeft:_maxImageCount-1 center:0 right:1];
}else {
[self changeImageLeft:_currentIndex-1 center:_currentIndex right:_currentIndex+1];
}
_pageControl.currentPage = _currentIndex;
}
if (offsetX <= 0) {
_currentIndex--;
if (_currentIndex == 0) {
[self changeImageLeft:_maxImageCount-1 center:0 right:1];
}else if (_currentIndex == -1) {
_currentIndex = _maxImageCount-1;
[self changeImageLeft:_currentIndex-1 center:_currentIndex right:0];
}else {
[self changeImageLeft:_currentIndex-1 center:_currentIndex right:_currentIndex+1];
}
_pageControl.currentPage = _currentIndex;
}
}
- (void)changeImageLeft:(NSInteger)leftIndex center:(NSInteger)centerIndex right:(NSInteger)rightIndex{
_leftImageView.image = [UIImage imageNamed:_myImageArray[leftIndex]];
_centerImageView.image = [UIImage imageNamed:_myImageArray[centerIndex]];
_rightimageView.image = [UIImage imageNamed:_myImageArray[rightIndex]];
[_myScroll setContentOffset:CGPointMake(kWidth, 0)];
}
@end