Initial commit
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// LXAdScrollView.h
|
||||
// LXAdScrollview
|
||||
//
|
||||
// Created by imac on 16/11/8.
|
||||
// Copyright © 2016年 xiang. All rights reserved.
|
||||
//
|
||||
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
@interface LXAdScrollView : UIView
|
||||
/*
|
||||
*显示广告的数据源,可以有三种类型 1:NSString,图片的网络地址,采用SDWebImage加载
|
||||
2:NSString,本地图片文件名
|
||||
* 3:使用UIImage
|
||||
* 此属性赋值后会立即开启定时器,需要在下面两个属性设置完后再设置
|
||||
*/
|
||||
@property (nonatomic, copy) NSArray *adList;
|
||||
@property (nonatomic, assign, readonly) NSInteger currentPage;
|
||||
@property (nonatomic, assign) NSTimeInterval animationDuration;//自动滚动动画时间,默认2s
|
||||
@property (nonatomic, strong) UIPageControl *pageControl;
|
||||
@property (nonatomic , copy) void (^tapActionBlock)(LXAdScrollView *adScrollView);
|
||||
@property (nonatomic, strong) UIImageView *currentImageView;
|
||||
@property (nonatomic, strong) UIImageView *leftImageView;
|
||||
@property (nonatomic, strong) UIImageView *rightImageView;
|
||||
@property (nonatomic, strong) UIScrollView *scrollView;
|
||||
@end
|
||||
@@ -0,0 +1,226 @@
|
||||
//
|
||||
// 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 ()<UIScrollViewDelegate>
|
||||
|
||||
@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
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// NSTimer+Addition.h
|
||||
// SDAdScrollView
|
||||
//
|
||||
// Created by DHsong on 16/4/20.
|
||||
// Copyright © 2016年 DHsong. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface NSTimer (Addition)
|
||||
- (void)pauseTimer;
|
||||
- (void)resumeTimer;
|
||||
- (void)resumeTimerAfterTimeInterval:(NSTimeInterval)interval;
|
||||
- (void)stopTimer;
|
||||
@end
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
//
|
||||
// NSTimer+Addition.m
|
||||
// SDAdScrollView
|
||||
//
|
||||
// Created by DHsong on 16/4/20.
|
||||
// Copyright © 2016年 DHsong. All rights reserved.
|
||||
//
|
||||
|
||||
#import "NSTimer+Addition.h"
|
||||
|
||||
@implementation NSTimer (Addition)
|
||||
-(void)pauseTimer
|
||||
{
|
||||
if (![self isValid]) {
|
||||
return ;
|
||||
}
|
||||
[self setFireDate:[NSDate distantFuture]];
|
||||
}
|
||||
|
||||
|
||||
-(void)resumeTimer
|
||||
{
|
||||
if (![self isValid]) {
|
||||
return ;
|
||||
}
|
||||
[self setFireDate:[NSDate date]];
|
||||
}
|
||||
|
||||
- (void)resumeTimerAfterTimeInterval:(NSTimeInterval)interval
|
||||
{
|
||||
if (![self isValid]) {
|
||||
return ;
|
||||
}
|
||||
[self setFireDate:[NSDate dateWithTimeIntervalSinceNow:interval]];
|
||||
}
|
||||
|
||||
- (void)stopTimer
|
||||
{
|
||||
if ([self isValid]) {
|
||||
[self invalidate];
|
||||
}
|
||||
}
|
||||
@end
|
||||
Reference in New Issue
Block a user