Commit d85a8446 authored by lufei's avatar lufei

feature:

1.添加pageWillShow回调
2.添加pageShow回调
3.支持外部修改frame,同时调整子view frame
3.优化代码逻辑
parent 21af5c1c
......@@ -8,7 +8,7 @@
Pod::Spec.new do |s|
s.name = 'DZXCarouselView'
s.version = '0.0.1'
s.version = '0.0.2'
s.summary = '轮播组件'
# This description is used to generate tags and improve search results.
......
......@@ -55,6 +55,8 @@ typedef NS_ENUM(NSUInteger, DZXCarouselPageControlTheme) {
@class DZXCarouselConfig;
typedef void(^DZXCarouselConfigBlock)(DZXCarouselConfig *carouselConfig);
typedef void(^DZXCarouselTapBlock)(NSInteger index, id data);
typedef void(^DZXCarouselPageShowBlock)(NSInteger index, id data);
typedef void(^DZXCarouselPageWillShowBlock)(NSInteger index, id data);
@interface DZXCarouselConfig : NSObject
......@@ -101,7 +103,13 @@ typedef void(^DZXCarouselTapBlock)(NSInteger index, id data);
/// @param frame frame
/// @param configBlock 配置回调
/// @param tapBlock 点击回调
- (instancetype)initWithFrame:(CGRect)frame configBlock:(DZXCarouselConfigBlock)configBlock tapBlock:(DZXCarouselTapBlock)tapBlock;
/// @param pageShowBlock 页面将展示回调
/// @param pageShowBlock 页面展示回调
- (instancetype)initWithFrame:(CGRect)frame
configBlock:(DZXCarouselConfigBlock)configBlock
tapBlock:(DZXCarouselTapBlock)tapBlock
pageWillShowBlock:(DZXCarouselPageWillShowBlock)pageWillShowBlock
pageShowBlock:(DZXCarouselPageShowBlock)pageShowBlock;
/// 开始定时器
- (void)timerStart;
......@@ -109,5 +117,8 @@ typedef void(^DZXCarouselTapBlock)(NSInteger index, id data);
/// 结束定时器
- (void)timerStop;
/// 更新当前索引
- (void)updateCurrentIndex:(NSInteger)currentIndex;
@end
......@@ -75,12 +75,18 @@
@property (nonatomic, copy) DZXCarouselConfigBlock configBlock;
@property (nonatomic, copy) DZXCarouselTapBlock tapBlock;
@property(nonatomic, copy) DZXCarouselPageWillShowBlock pageWillShowBlock;
@property (nonatomic, copy) DZXCarouselPageShowBlock pageShowBlock;
@end
@implementation DZXCarouselView
#pragma mark - Initial Methods
- (instancetype)initWithFrame:(CGRect)frame configBlock:(DZXCarouselConfigBlock)configBlock tapBlock:(DZXCarouselTapBlock)tapBlock {
- (instancetype)initWithFrame:(CGRect)frame
configBlock:(DZXCarouselConfigBlock)configBlock
tapBlock:(DZXCarouselTapBlock)tapBlock
pageWillShowBlock:(DZXCarouselPageWillShowBlock)pageWillShowBlock
pageShowBlock:(DZXCarouselPageShowBlock)pageShowBlock{
if (self = [super initWithFrame:frame]) {
DZXCarouselConfig *carouselConfig = DZXCarouselConfig.new;
......@@ -88,9 +94,10 @@
if (configBlock) {
configBlock(carouselConfig);
}
if (tapBlock) {
self.tapBlock = tapBlock;
}
self.pageWillShowBlock = pageWillShowBlock;
self.pageShowBlock = pageShowBlock;
NSAssert(![carouselConfig.subViewClass isEqualToString:@""], @"carouselConfig.subViewClass should not be nil");
NSAssert(![carouselConfig.propertyName isEqualToString:@""], @"carouselConfig.propertyName should not be nil");
if (_carouselConfig.showPageControl) {
......@@ -107,22 +114,29 @@
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.backgroundColor = UIColor.clearColor;
[self addSubview:_scrollView];
if (carouselConfig.theme == DZXCarouselThemeVertical) {
_scrollView.contentOffset = CGPointMake(0, frame.size.height);
_scrollView.contentSize = CGSizeMake(frame.size.width, frame.size.height*3);
}else{
_scrollView.contentOffset = CGPointMake(frame.size.width, 0);
_scrollView.contentSize = CGSizeMake(frame.size.width*3, frame.size.height);
}
}
return self;
}
- (void)willMoveToSuperview:(UIView *)newSuperview {
[super willMoveToSuperview:newSuperview];
if (!newSuperview) {
[self timerStop];
}
}
- (void)layoutSubviews {
self.scrollView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
[self updateSubViewFrames];
if (_carouselConfig.theme == DZXCarouselThemeVertical) {
_scrollView.contentOffset = CGPointMake(0, self.frame.size.height);
_scrollView.contentSize = CGSizeMake(self.frame.size.width, self.frame.size.height*3);
}else{
_scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
_scrollView.contentSize = CGSizeMake(self.frame.size.width*3, self.frame.size.height);
}
}
#pragma mark - Public Methods
- (void)timerStop {
if (_timer) {
......@@ -130,13 +144,29 @@
self.timer = nil;
}
}
- (void)timerStart {
if (_dataArr.count > 1) {
if (_carouselConfig.autoScroll && _dataArr.count > 1) {
[self timerStop];
self.timer = [NSTimer scheduledTimerWithTimeInterval:_carouselConfig.timeInterval target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
}
}
- (void)updateCurrentIndex:(NSInteger)currentIndex {
if (currentIndex >= _dataArr.count || currentIndex < 0) {
return;
}
[self timerStop];
if (_pageWillShowBlock) {
_pageWillShowBlock(currentIndex,_dataArr[currentIndex]);
}
self.currentIndex = currentIndex;
[self updateViewData];
[self timerStart];
}
#pragma mark - Override Methods
#pragma mark - Privater Methods
......@@ -144,16 +174,18 @@
if (_lastV) return;
Class className = NSClassFromString(_carouselConfig.subViewClass);
self.lastV = [[className alloc] initWithFrame:CGRectMake(_carouselConfig.padding.left, _carouselConfig.padding.top,self.frame.size.width-_carouselConfig.padding.left-_carouselConfig.padding.right, self.frame.size.height-_carouselConfig.padding.top-_carouselConfig.padding.bottom)];
[_scrollView addSubview:_lastV];
self.lastV = [[className alloc] init];
if (_carouselConfig.theme == DZXCarouselThemeVertical) {
self.currentV = [[className alloc] initWithFrame:CGRectMake(_carouselConfig.padding.left, self.frame.size.height+_carouselConfig.padding.top,self.frame.size.width-_carouselConfig.padding.left-_carouselConfig.padding.right, self.frame.size.height-_carouselConfig.padding.top-_carouselConfig.padding.bottom)];
self.nextV = [[className alloc] initWithFrame:CGRectMake(_carouselConfig.padding.left, self.frame.size.height*2+_carouselConfig.padding.top,self.frame.size.width-_carouselConfig.padding.left-_carouselConfig.padding.right, self.frame.size.height-_carouselConfig.padding.top-_carouselConfig.padding.bottom)];
self.currentV = [[className alloc] init];
self.nextV = [[className alloc] init];
}else{
self.currentV = [[className alloc] initWithFrame:CGRectMake(self.frame.size.width+_carouselConfig.padding.left, _carouselConfig.padding.top,self.frame.size.width-_carouselConfig.padding.left-_carouselConfig.padding.right, self.frame.size.height-_carouselConfig.padding.top-_carouselConfig.padding.bottom)];
self.nextV = [[className alloc] initWithFrame:CGRectMake(self.frame.size.width*2+_carouselConfig.padding.left, _carouselConfig.padding.top,self.frame.size.width-_carouselConfig.padding.left-_carouselConfig.padding.right, self.frame.size.height-_carouselConfig.padding.top-_carouselConfig.padding.bottom)];
self.currentV = [[className alloc] init];
self.nextV = [[className alloc] init];
}
[self updateSubViewFrames];
[_scrollView addSubview:_lastV];
[_scrollView addSubview:_currentV];
UITapGestureRecognizer *tapGes = [UITapGestureRecognizer.alloc initWithTarget:self action:@selector(tapCurrentView)];
......@@ -195,9 +227,14 @@
}
}
- (void)tapCurrentView {
if (self.tapBlock) {
self.tapBlock(self.currentIndex, self.dataArr[self.currentIndex]);
- (void)updateSubViewFrames {
_lastV.frame = CGRectMake(_carouselConfig.padding.left, _carouselConfig.padding.top,self.frame.size.width-_carouselConfig.padding.left-_carouselConfig.padding.right, self.frame.size.height-_carouselConfig.padding.top-_carouselConfig.padding.bottom);
if (_carouselConfig.theme == DZXCarouselThemeVertical) {
_currentV.frame = CGRectMake(_carouselConfig.padding.left, self.frame.size.height+_carouselConfig.padding.top,self.frame.size.width-_carouselConfig.padding.left-_carouselConfig.padding.right, self.frame.size.height-_carouselConfig.padding.top-_carouselConfig.padding.bottom);
_nextV.frame = CGRectMake(_carouselConfig.padding.left, self.frame.size.height*2+_carouselConfig.padding.top,self.frame.size.width-_carouselConfig.padding.left-_carouselConfig.padding.right, self.frame.size.height-_carouselConfig.padding.top-_carouselConfig.padding.bottom);
}else{
_currentV.frame = CGRectMake(self.frame.size.width+_carouselConfig.padding.left, _carouselConfig.padding.top,self.frame.size.width-_carouselConfig.padding.left-_carouselConfig.padding.right, self.frame.size.height-_carouselConfig.padding.top-_carouselConfig.padding.bottom);
_nextV.frame = CGRectMake(self.frame.size.width*2+_carouselConfig.padding.left, _carouselConfig.padding.top,self.frame.size.width-_carouselConfig.padding.left-_carouselConfig.padding.right, self.frame.size.height-_carouselConfig.padding.top-_carouselConfig.padding.bottom);
}
}
......@@ -210,18 +247,24 @@
}
_isLast = NO;//重新置回NO,向后滚
[self updateViewData];
}
}
//更新内容
- (void)updateViewData {
[self addProperty:_dataArr[(_currentIndex-1)%_dataArr.count] toObj:_lastV];
[self addProperty:_dataArr[_currentIndex] toObj:_currentV];
[self addProperty:_dataArr[(_currentIndex+1)%_dataArr.count] toObj:_nextV];
if (_carouselConfig.theme == DZXCarouselThemeVertical) {
[_scrollView setContentOffset:CGPointMake(0, self.frame.size.height)];
}else{
[_scrollView setContentOffset:CGPointMake(self.frame.size.width, 0)];
}
_pageControl.currentPage = _currentIndex;
}
}
[self pageShow];
}
- (void)addProperty:(id)data toObj:(id)obj {
SEL sel=NSSelectorFromString(_carouselConfig.propertyName);
if ([obj respondsToSelector:sel]) {
......@@ -266,6 +309,8 @@
#pragma mark - Target Methods
-(void)timerAction:(BOOL)isLast {
[self pageWillShow:isLast];
__weak __typeof(self)weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
__strong __typeof(weakSelf)strongSelf = weakSelf;
......@@ -286,6 +331,32 @@
//动画过后重置位置
[NSTimer scheduledTimerWithTimeInterval:.3 target:self selector:@selector(scroll) userInfo:nil repeats:NO];
}
#pragma mark - Action
//将要显示
- (void)pageWillShow:(BOOL)isLast {
NSInteger willShowIndex;
if (_isLast) {
willShowIndex = (_currentIndex - 1)%_dataArr.count;
}else{
willShowIndex = (_currentIndex + 1)%_dataArr.count;
}
if (_pageWillShowBlock) {
_pageWillShowBlock(willShowIndex,_dataArr[willShowIndex]);
}
}
//已显示
- (void)pageShow {
if (_pageShowBlock) {
_pageShowBlock(_currentIndex,_dataArr[_currentIndex]);
}
}
///点击
- (void)tapCurrentView {
if (self.tapBlock) {
self.tapBlock(_currentIndex, self.dataArr[_currentIndex]);
}
}
#pragma mark - Setter Getter Methods
-(void)setDataArr:(NSArray *)dataArr {
_dataArr=dataArr;
......@@ -311,12 +382,15 @@
[self addProperty:_dataArr[0] toObj:_currentV];
if (dataArr.count>1) {
[self addProperty:_dataArr[1] toObj:_nextV];
if (_carouselConfig.autoScroll) {
[self timerStart];
}
}else{
[self addProperty:_dataArr[0] toObj:_nextV];
}
//页面展示block(第一次进来)
_pageWillShowBlock(0, _dataArr[0]);
_pageShowBlock(0, _dataArr[0]);
}
@end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment