// // LXAdScrollView.m // LXAdScrollview // // Created by imac on 16/11/8. // Copyright © 2016年 xiang. All rights reserved. // #import "LXAdScrollView.h" #import "NSTimer+Addition.h" #import "UIImageView+WebCache.h" @interface LXAdScrollView () @property (nonatomic, strong) NSTimer *timer; @property (nonatomic) CGRect rect; @end @implementation LXAdScrollView -(instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { _rect = self.bounds; [self creatSubviews]; } return self; } -(void)awakeFromNib{ [super awakeFromNib]; _rect = CGRectMake(0,0,kScreenSize.width,CGRectGetHeight(self.frame)); [self creatSubviews]; } -(void)creatSubviews{ _animationDuration = 3.0f; _leftImageView = [[UIImageView alloc]init]; [self.scrollView addSubview:_leftImageView]; _currentImageView = [[UIImageView alloc]init]; [self.scrollView addSubview:_currentImageView]; _rightImageView = [[UIImageView alloc]init]; [self.scrollView addSubview:_rightImageView]; _timer = [NSTimer scheduledTimerWithTimeInterval:_animationDuration target:self selector:@selector(onTimer:) userInfo:nil repeats:YES]; [_timer pauseTimer]; UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onGesture:)]; [self.scrollView addGestureRecognizer:tapGesture]; self.translatesAutoresizingMaskIntoConstraints = NO; } -(void)layoutSubviews { [super layoutSubviews]; //xib 时 self。frame 改变 _scrollView.frame = self.bounds; _leftImageView.frame = CGRectMake(0,0,CGRectGetWidth(self.frame), CGRectGetHeight(self.frame)); _currentImageView.frame = CGRectOffset(_leftImageView.frame, CGRectGetWidth(_leftImageView.frame), 0); _rightImageView.frame = CGRectOffset(_currentImageView.frame, CGRectGetWidth(_currentImageView.frame), 0); _pageControl.frame = CGRectMake(40, CGRectGetHeight(self.frame)-20, CGRectGetWidth(self.frame)-40*2, 10); } #pragma mark - Action -(void)onTimer:(NSTimer*)timer { [self.scrollView setContentOffset:CGPointMake(CGRectGetWidth(_rect)*2, 0) animated:YES]; } -(void)onGesture:(UITapGestureRecognizer*)tapGesture { if (self.tapActionBlock) { self.tapActionBlock(self); } } #pragma mark - setter -(void)setAdList:(NSArray *)adList { _adList = adList; self.pageControl.numberOfPages = _adList.count; self.pageControl.currentPage = 0; _currentPage = 0; if (_adList.count <= 1) { _scrollView.scrollEnabled = NO; _scrollView.contentSize = CGSizeMake(CGRectGetWidth(_rect)*3, 0); //self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(_rect), 0); }else{ _scrollView.scrollEnabled = YES; _scrollView.contentSize = CGSizeMake(CGRectGetWidth(_rect)*3, 0); [_timer resumeTimerAfterTimeInterval:_animationDuration]; } [self refreshImageView]; } //根据当前的index,重置当前显示的图片,并且使currentImageView永远保持在中间 -(void)refreshImageView { NSInteger index = _currentPage; [self formatImageView:_currentImageView imageData:self.adList[index]]; if (self.adList.count > 1) { index = _currentPage-1<0?self.adList.count-1:_currentPage-1; [self formatImageView:_leftImageView imageData:self.adList[index]]; index = _currentPage+1>=self.adList.count?0:_currentPage+1; [self formatImageView:_rightImageView imageData:self.adList[index]]; } self.scrollView.contentOffset = CGPointMake(CGRectGetWidth(_rect), 0); } -(void)formatImageView:(UIImageView*)imageView imageData:(id)data { if ([data isKindOfClass:[UIImage class]]) { imageView.image = (UIImage*)data; }else if ([data isKindOfClass:[NSString class]]) { NSString *imageName = (NSString*)data; if ([imageName hasPrefix:@"http"]) { //网络图片live_upload_banner [imageView sd_setImageWithURL:[NSURL URLWithString:imageName] placeholderImage:[UIImage imageNamed:@"live_upload_banner.png"]]; }else{ //本地图片 dispatch_async(dispatch_get_main_queue(), ^{ imageView.image = [UIImage imageNamed:imageName]; }); } } } -(void)refreshCurrentPage { if (self.scrollView.contentOffset.x >= CGRectGetWidth(_rect)*1.5) { _currentPage ++; if (_currentPage > self.adList.count-1) { _currentPage = 0; } }else if (self.scrollView.contentOffset.x <= CGRectGetWidth(_rect)/2) { _currentPage--; if (_currentPage < 0) { _currentPage = self.adList.count-1; } } } #pragma mark - UI -(UIPageControl*)pageControl { if (!_pageControl) { CGRect rect = CGRectMake(40, CGRectGetHeight(_rect)-20, CGRectGetWidth(_rect)-40*2, 10); _pageControl = [[UIPageControl alloc]initWithFrame:rect]; _pageControl.hidesForSinglePage = YES; _pageControl.pageIndicatorTintColor = [UIColor grayColor]; _pageControl.currentPageIndicatorTintColor = RGB(0, 184, 238); [self addSubview:_pageControl]; } return _pageControl; } -(UIScrollView*)scrollView { if (!_scrollView) { _scrollView = [[UIScrollView alloc]initWithFrame:_rect]; _scrollView.delegate = self; _scrollView.bounces = NO; _scrollView.showsHorizontalScrollIndicator = NO; _scrollView.showsVerticalScrollIndicator = NO; _scrollView.pagingEnabled = YES; _scrollView.scrollsToTop = NO; _scrollView.contentSize = CGSizeMake(CGRectGetWidth(_rect)*3, 0); //_scrollView.backgroundColor = [UIColor redColor]; [self addSubview:_scrollView]; } return _scrollView; } #pragma mark - UIScrollViewDelegate - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { //开始滚动时暂停定时器 [self.timer pauseTimer]; } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { [self refreshCurrentPage]; if (self.pageControl.currentPage != _currentPage) { self.pageControl.currentPage = _currentPage; [self refreshImageView]; } //滚动结束回复定时器 [self.timer resumeTimerAfterTimeInterval:_animationDuration]; } - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { [self refreshCurrentPage]; if (self.pageControl.currentPage != _currentPage) { self.pageControl.currentPage = _currentPage; [self refreshImageView]; } } -(void)dealloc{ [_timer invalidate]; _timer = nil; } @end