關(guān)于無限輪播廣告頁,相信大家已經(jīng)再熟悉不過了,今天給大家?guī)硪粋€我自己寫的SGAdvertScrollView;輕松搞定廣告頁顯示和點擊跳轉(zhuǎn)功能,廢話不多述直接貼代碼:
原理:利用 scrollView 只創(chuàng)建三頁內(nèi)容展示(第一頁left ,第二頁center ,第三頁right)默認(rèn)顯示第二頁 center,然后向左或者右滑動結(jié)束后自動跳回第二頁 center 頁,更改 center 頁顯示的內(nèi)容為第三頁或者第一頁內(nèi)容來實現(xiàn)換頁,具體看代碼和源碼:
SGAdvertScrollView.h文件:
#import <UIKit/UIKit.h>
@interface SGAdvertScrollView : UIView
/*!
* @abstract return instance
* @param imagesArray 圖片URL數(shù)組
* @param timeInterval 自動輪播時間(秒) 默認(rèn)為5秒
* @param advertSelectBlock 點擊圖片回調(diào)block
*/
+ (instancetype)advertScrollViewFrame:(CGRect)frame imagesArray:(NSArray *)imagesArray timeInterval:(NSTimeInterval)timeInterval advertSelectBlock:(void(^)(int selectIndex))advertSelectBlock;
/*!
* @abstract stop timer
*/
- (void)stopTimer;
@end
</pre>
SGAdvertScrollView.m文件:
<pre>
#import "SGAdvertScrollView.h"
#define UIImageWithName(name) [UIImage imageNamed:name]
@interface SGAdvertScrollView()<UIScrollViewDelegate>{
int _centerIndex;
NSArray *_imageNameArray;
NSTimeInterval _timeInterval;
}
@property (nonatomic ,strong) UIScrollView *adScrollView;
@property (nonatomic ,strong) UIImageView *leftImgView;
@property (nonatomic ,strong) UIImageView *centerImgView;
@property (nonatomic ,strong) UIImageView *rightImgView;
@property (nonatomic ,strong) NSTimer *timer;
@property (nonatomic ,strong) UIPageControl *pageControl;
@property (nonatomic ,copy) void (^advertClickBlock) (int selectIndex);
@end
@implementation SGAdvertScrollView
+ (instancetype)advertScrollViewFrame:(CGRect)frame imagesArray:(NSArray *)imagesArray timeInterval:(NSTimeInterval)timeInterval advertSelectBlock:(void(^)(int selectIndex))advertSelectBlock{
return [[self alloc]initWithFrame:frame imagesArray:imagesArray timeInterval:timeInterval advertSelectBlock:advertSelectBlock];
}
-(instancetype)initWithFrame:(CGRect)frame imagesArray:(NSArray *)imagesArray timeInterval:(NSTimeInterval)timeInterval advertSelectBlock:(void(^)(int selectIndex))advertSelectBlock{
self = [super initWithFrame:frame];
if (self) {
_imageNameArray = [imagesArray copy];
self.advertClickBlock = advertSelectBlock;
_timeInterval = timeInterval;
if (_timeInterval == 0) {
_timeInterval = 5;
}
[self setSubviews];
[self startTimer];
}
return self;
}
#pragma mark -----lazy
- (UIScrollView *)adScrollView{
if (!_adScrollView) {
_adScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))];
_adScrollView.showsVerticalScrollIndicator = NO;
_adScrollView.showsHorizontalScrollIndicator = NO;
_adScrollView.pagingEnabled = YES;
_adScrollView.backgroundColor = [UIColor whiteColor];
_adScrollView.contentSize = CGSizeMake(3 * CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));
_adScrollView.contentOffset = CGPointMake(CGRectGetWidth(self.bounds), 0);
_adScrollView.delegate = self;
_adScrollView.bounces = NO;
UITapGestureRecognizer *panGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(jumpToAdvertDetail)];
[_adScrollView addGestureRecognizer:panGesture];
[self addSubview:_adScrollView];
}
return _adScrollView;
}
- (UIImageView *)leftImgView{
if (!_leftImgView) {
_leftImgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))];
[self.adScrollView addSubview:_leftImgView];
}
return _leftImgView;
}
- (UIImageView *)centerImgView{
if (!_centerImgView) {
_centerImgView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.bounds), 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))];
[self.adScrollView addSubview:_centerImgView];
}
return _centerImgView;
}
- (UIImageView *)rightImgView{
if (!_rightImgView) {
_rightImgView = [[UIImageView alloc]initWithFrame:CGRectMake(2 * CGRectGetWidth(self.bounds), 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))];
[self.adScrollView addSubview:_rightImgView];
}
return _rightImgView;
}
- (UIPageControl *)pageControl{
if (!_pageControl) {
_pageControl = [[UIPageControl alloc]init];
CGSize size = [_pageControl sizeForNumberOfPages:_imageNameArray.count];
_pageControl.frame = CGRectMake((CGRectGetWidth(self.bounds) - size.width) / 2, CGRectGetHeight(self.bounds) - size.height, size.width, size.height);
_pageControl.center = CGPointMake(self.adScrollView.center.x, _pageControl.center.y);
_pageControl.numberOfPages = _imageNameArray.count;
_pageControl.hidesForSinglePage = YES;
_pageControl.pageIndicatorTintColor = [UIColor grayColor];
_pageControl.currentPageIndicatorTintColor = [UIColor yellowColor];
[self insertSubview:_pageControl aboveSubview:self.adScrollView];
}
return _pageControl;
}
#pragma mark -----operation methods
- (void)setSubviews{
if (_centerIndex < 0) {
_centerIndex = (int)_imageNameArray.count - 1;
}
if (_centerIndex >= _imageNameArray.count) {
_centerIndex = 0;
}
int leftIndex = _centerIndex - 1;
if (leftIndex < 0){
leftIndex = (int)_imageNameArray.count - 1;
}
int rightIndex = _centerIndex + 1;
if (rightIndex >= _imageNameArray.count) {
rightIndex = 0;
}
NSLog(@"leftIndex:%d;_centerIndex:%d;rightIndex:%d",leftIndex,_centerIndex,rightIndex);
if ((leftIndex >= _imageNameArray.count || _centerIndex >= _imageNameArray.count || rightIndex >= _imageNameArray.count)) {
NSLog(@"數(shù)組越界");
return;
}
NSString *leftImgName = [_imageNameArray objectAtIndex:leftIndex];
NSString *centerImgName = [_imageNameArray objectAtIndex:_centerIndex];
NSString *rightImgName = [_imageNameArray objectAtIndex:rightIndex];
//MARK:local images
self.leftImgView.image = UIImageWithName(leftImgName);
self.centerImgView.image = UIImageWithName(centerImgName);
self.rightImgView.image = UIImageWithName(rightImgName);
//MARK:url images
/*
[self.leftImgView sd_setImageWithURL:[NSURL URLWithString:leftImgName] placeholderImage:UIImageWithName(leftImgName)];
[self.centerImgView sd_setImageWithURL:[NSURL URLWithString:centerImgName] placeholderImage:UIImageWithName(centerImgName)];
[self.rightImgView sd_setImageWithURL:[NSURL URLWithString:rightImgName] placeholderImage:UIImageWithName(rightImgName)];
*/
self.pageControl.currentPage = _centerIndex;
}
- (void)jumpToAdvertDetail{
if (self.advertClickBlock) {
self.advertClickBlock(_centerIndex);
}
}
#pragma mark -----NSTimer
- (void)startTimer{
[self stopTimer];
_timer = [NSTimer scheduledTimerWithTimeInterval:_timeInterval target:self selector:@selector(timerChanged) userInfo:nil repeats:YES];
}
- (void)timerChanged{
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
[self.adScrollView scrollRectToVisible:CGRectMake(2 * CGRectGetWidth(self.bounds), 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds)) animated:NO];
} completion:^(BOOL finished) {
[self.adScrollView scrollRectToVisible:CGRectMake(CGRectGetWidth(self.bounds), 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds)) animated:NO];
_centerIndex++;
[self setSubviews];
}];
}
- (void)stopTimer{
if (_timer) {
[_timer invalidate];
_timer = nil;
}
}
#pragma mark -----UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
[self stopTimer];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
[self startTimer];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
CGFloat offsetX = scrollView.contentOffset.x;
if (offsetX != CGRectGetWidth(self.bounds)) {
[scrollView scrollRectToVisible:CGRectMake(CGRectGetWidth(self.bounds), 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds)) animated:NO];
if (offsetX <= CGRectGetWidth(self.bounds)) {
_centerIndex--;
}else if (offsetX >= CGRectGetWidth(self.bounds) * 2){
_centerIndex++;
}
[self setSubviews];
}
}
@end
</pre>
注意:需要自己導(dǎo)入 SDWebImage并且改為網(wǎng)絡(luò)地址:
SGAdvertScrollView.m
網(wǎng)絡(luò)圖片格式如下:注釋掉本地圖片的代碼,打開網(wǎng)絡(luò)圖片代碼,還有使用我的 demo加載網(wǎng)絡(luò)圖片不出來的檢查一下 info.plist 文件,看看有沒有允許訪問網(wǎng)絡(luò)(兼容 https)
<pre>
//MARK:local images
// self.leftImgView.image = UIImageWithName(leftImgName);
// self.centerImgView.image = UIImageWithName(centerImgName);
// self.rightImgView.image = UIImageWithName(rightImgName);
//MARK:url images
[self.leftImgView sd_setImageWithURL:[NSURL URLWithString:leftImgName] placeholderImage:UIImageWithName(leftImgName)];
[self.centerImgView sd_setImageWithURL:[NSURL URLWithString:centerImgName] placeholderImage:UIImageWithName(centerImgName)];
[self.rightImgView sd_setImageWithURL:[NSURL URLWithString:rightImgName] placeholderImage:UIImageWithName(rightImgName)];
喜歡的朋友可以給我點贊哦...