现今很多项目中都有轮播图的这个功能,这两天我们的项目中也处处要用到轮播图,所以我写了一个简易的轮播图封装,轻松的解决了所遇到的问题。
由于我这个轮播图传入的参数是存放图片URL的数组,所以在用的时候需先引入SDWebImage的第三方,当然,用我上一篇写的图片下载的两种方法也可行。
@interface WheelImageTool : UIView
- (instancetype )initWithFrame:(CGRect)frame imageUrlArr:(NSArray *)arr timeInterval:(CGFloat)timeInterval;
@end
#import "WheelImageTool.h"
#import "UIImageView+WebCache.h"
@interface WheelImageTool()<UIScrollViewDelegate>
{
NSUInteger count;
NSUInteger currentNum;
}
@property (nonatomic,strong)UIScrollView *mainScroller;
@property (nonatomic,strong)UIPageControl *page;
@property (nonatomic,strong)NSTimer *timer;
@property (nonatomic,assign)CGFloat timeInterval;
@end
@implementation WheelImageTool
- (instancetype )initWithFrame:(CGRect)frame imageUrlArr:(NSArray *)arr timeInterval:(CGFloat)timeInterval
{
self = [super initWithFrame:frame];
if (self) {
self.timeInterval = timeInterval;
count = arr.count;
currentNum = 1;
if (arr.count != 0)
{
[self setViewWithImageUrlArr:arr];
if(timeInterval )
{
[self.timer invalidate];
self.timer = nil;
self.timer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
}
}
}
return self;
}
- (void)timerAction{
if (count == 1) {
[self.mainScroller setContentOffset:CGPointMake(0, 0) animated:NO];
}else{
currentNum ++;
if (currentNum == count+1)
{
self.page.currentPage = 0;
[self.mainScroller setContentOffset:CGPointMake(0, 0) animated:NO];
[self.mainScroller setContentOffset:CGPointMake(self.frame.size.width, 0) animated:YES];
currentNum = 1;
return;
}
[self.mainScroller setContentOffset:CGPointMake(currentNum *self.frame.size.width, 0) animated:YES];
self.page.currentPage = currentNum - 1;
}
}
#pragma mark -scroll的代理方法 ==== 结束减速时的操作
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
currentNum = self.mainScroller.contentOffset.x / self.frame.size.width;
if (currentNum == count + 1) {
self.page.currentPage = 0 ;
currentNum = 1;
[self.mainScroller setContentOffset:CGPointMake(currentNum * self.frame.size.width, 0) animated:NO];
}else{
self.page.currentPage = currentNum - 1 ;
}
}
- (void)setViewWithImageUrlArr:(NSArray *)arr{
self.mainScroller = [[UIScrollView alloc]initWithFrame:self.frame];
self.mainScroller.contentSize = CGSizeMake((arr.count+2)*self.frame.size.width, 0);
self.mainScroller.delegate = self;
self.mainScroller.pagingEnabled = YES;
self.mainScroller.bounces = NO;
self.mainScroller.showsHorizontalScrollIndicator = NO;
self.mainScroller.contentOffset = CGPointMake(self.frame.size.width, 0);
[self addSubview:self.mainScroller];
UIImageView *imV1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
imV1.userInteractionEnabled = YES;
[imV1 sd_setImageWithURL:arr.lastObject];
[self.mainScroller addSubview:imV1];
for (int i = 0 ; i < arr.count ; i++) {
UIImageView *imV = [[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width * (i+1), 0, self.frame.size.width, self.frame.size.height)];
imV.userInteractionEnabled = YES;
[imV sd_setImageWithURL:arr[i]];
[self.mainScroller addSubview:imV];
}
UIImageView *imV2 = [[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width * ( arr.count + 1 ), 0, self.frame.size.width, self.frame.size.height)];
imV2.userInteractionEnabled = YES;
[imV2 sd_setImageWithURL:arr.firstObject];
[self.mainScroller addSubview:imV2];
self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(self.frame.size.width - 80, self.frame.size.height - 30, 80, 30)];
self.page.enabled = NO;
self.page.numberOfPages = arr.count;
[self addSubview:self.page];
}
@end
实验结果是没有问题的,由于轮播图片的结果是动态的,我暂时还不知道怎么展示出来,所以没在此处给大家展示,但是这个一定是可以用的,代码为个人所写,有任何不足或可以改良之处望大家提出,谢谢。