轮播图封装。可以实现自动定时翻页、手动翻页;垂直和水平滚动等。支持纯文本、本地图片、网络图片以及其他view试图。
底层采用UICollectionView当控制器。给原数据源下标0位添加原数据源末尾数据,给元数据源下标末尾添加原数据源0位的数据。以此形成一个新的数据源。可以参考下图
typedef NS_ENUM(NSInteger, ECAutoScrollBannerScrollDirection){
ECAutoScrollBannerScrollDirectionVertical = 1, // 竖向滚动
ECAutoScrollBannerScrollDirectionHorizontal = 0, // 横向滚动
};
/**
* delegate,非必
*/
@property (nonatomic, weak) id delegate;
/**
* 是否自动翻页,默认NO,非必
*/
@property (nonatomic, assign) BOOL isAutoPaging;
/**
* 是否展示PageControl,默认YES,非必
*/
@property (nonatomic, assign) BOOL isHavePageControl;
/**
* 是否无限循环,默认NO,非必
*/
@property (nonatomic, assign) BOOL isInfinitePaging;
/**
* 自动翻页的时候知否支持手动滑动,必须在isAutoPaging=YES时候设置才有效果,if isAutoPaging=YES,则这个必须设置
*/
@property (nonatomic, assign) BOOL isEnabledPanGestureRecognizer;
/**
* collectionView backgroundColor,默认lightGrayColor,非必
*/
@property (nonatomic, strong) UIColor *collectionViewBgColor;
/**
* 可以自定义pageControl的frame(相对于self),必须isHavePageControl=YES,如果没有设置新坐标,则取默认坐标,非必
*/
@property (nonatomic, assign) CGRect pageControlFrame;
/**
* 自动翻页间隔时间。需isAutoPaging=YES才需要设置,否则设置什么效果。默认3.0f
*/
@property (nonatomic, assign) CGFloat autoPageInterval;
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (self.isAutoPaging) {
scrollView.panGestureRecognizer.enabled = self.isEnabledPanGestureRecognizer;
}
NSInteger page = 0;
// 是否无限循环
if (!self.isInfinitePaging) {
if (self.bannerScrollDirection == ECAutoScrollBannerScrollDirectionHorizontal) {
page = scrollView.contentOffset.x / scrollView.frame.size.width;
} else {
page = scrollView.contentOffset.y / scrollView.frame.size.height;
}
self.pageControl.currentPage = page;
} else {
if (self.bannerScrollDirection == ECAutoScrollBannerScrollDirectionHorizontal) {
page = scrollView.contentOffset.x / scrollView.frame.size.width;
// 第一张
if ((page == 0) && (scrollView.contentOffset.x <= 30)) {
page = self.mainDataSource.count - 2;
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:page inSection:0];
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
} else if ((page == self.mainDataSource.count - 1) && (scrollView.contentOffset.x >= scrollView.contentSize.width - scrollView.frame.size.width - 30)) {
// 最后一张
page = 0 + 1;
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:page inSection:0];
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
}
} else {
page = scrollView.contentOffset.y / scrollView.frame.size.height;
// 第一张
if ((page == 0) && (scrollView.contentOffset.y <= 30)) {
page = self.mainDataSource.count - 2;
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:page inSection:0];
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
} else if ((page == self.mainDataSource.count - 1) && (scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height - 30)) {
// 最后一张
page = 0 + 1;
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:page inSection:0];
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
}
}
self.pageControl.currentPage = page - 1;
}
}
- (ECAutoScrollBanner *)textBannerView {
if (_textBannerView == nil) {
_textBannerView = [ECAutoScrollBanner initTextBannerWithFrame:self.topView.bounds withTextDataSource:self.textDataArray withBannerScrollDirection:ECAutoScrollBannerScrollDirectionVertical];
_textBannerView.delegate = self;
_textBannerView.isAutoPaging = YES;
_textBannerView.isHavePageControl = NO;
_textBannerView.isInfinitePaging = YES;
_textBannerView.isEnabledPanGestureRecognizer = NO;
_textBannerView.autoPageInterval = 4.0f;
}
return _textBannerView;
}
- (void)tapScrollBannerItem:(NSInteger)itemTag withObject:(id)object {
NSLog(@"文本banner。点击了第%ld个子item,下标%ld", (long)itemTag + 1, (long)itemTag);
}
- (ECAutoScrollBanner *)imageBannerView {
if (_imageBannerView == nil) {
_imageBannerView = [ECAutoScrollBanner initLocalImageBannerWithFrame:CGRectMake(0, 30, self.bottomView.frame.size.width, 180) withImageDataSource:self.imageDataArray withBannerScrollDirection:ECAutoScrollBannerScrollDirectionHorizontal];
_imageBannerView.isAutoPaging = NO;
}
return _imageBannerView;
}
- (ECAutoScrollBanner *)urlImageBannerVeiw {
if (_urlImageBannerVeiw == nil) {
UIImageView *placehodelImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 50 + 50 + 10 + 180 + 10, self.view.frame.size.width, 180)];
placehodelImage.backgroundColor = [UIColor lightGrayColor];
_urlImageBannerVeiw = [ECAutoScrollBanner initOnlineImageBannerWithFrame:CGRectMake(0, 50 + 50 + 10 + 180 + 10, self.view.frame.size.width, 180) withImageUrlDataSource:self.imageUrlDataArray withPlaceholderImage:placehodelImage.image withBannerScrollDirection:ECAutoScrollBannerScrollDirectionHorizontal];
_urlImageBannerVeiw.isAutoPaging = YES;
_urlImageBannerVeiw.isEnabledPanGestureRecognizer = YES;
_urlImageBannerVeiw.isInfinitePaging = YES;
}
return _urlImageBannerVeiw;
}
- (ECAutoScrollBanner *)viewBannerView {
if (_viewBannerView == nil) {
_viewBannerView = [[ECAutoScrollBanner alloc] initViewBannerWithFrame:CGRectMake(0, 20 + 180 + 20 + 180 + 20, self.view.frame.size.width, 180) withViewsDataSouce:self.viewDataArray withBannerScrollDirection:ECAutoScrollBannerScrollDirectionHorizontal];
_viewBannerView.isAutoPaging = NO;
}
return _viewBannerView;
}