// 前提 工程文件中必须有 SDWebImage 第三方文件
使用方法
// 定义一个TopViewController类
@interface TopViewController () <ZLQScrollViewClick>
@property (nonatomic,retain)ZLQScrollView *zlqScrollView;
@end
@implementation TopViewController
- (void)viewDidLoad {
self.zlqScrollView = [[ZLQScrollViewalloc]initWithFrame:CGRectMake(0,94,WIDTH,200)andOpenTimeter:YES];
[self.zlqScrollViewsetZlqScrollViewDelegate:self];
[self.view addSubview:self.zlqScrollView];
[_zlqScrollView release];
[self.zlqScrollViewsetTitleArray:tempTitleArray];
[self.zlqScrollViewsetImageArray:tempImageArray];
}
- (void)zlqScrollViewClick:(NSInteger)index {
NSLog(@"title = %@",[[self.imageArray objectAtIndex:index] objectForKey:@"title"]);
}
@end
ZLQScrollView.h
#import <UIKit/UIKit.h>
@protocol ZLQScrollViewClick <NSObject>
@required
/*
* 轮播图点击事件
*/
- (void)zlqScrollViewClick:(NSInteger)index;
@end
@interface ZLQScrollView :UIView
#warning
/*注意: ********先给标题数组数据 *************/
@property (nonatomic,retain)NSArray *imageArray;
@property (nonatomic,assign)CGRect scrollViewFrame;
@property (nonatomic,retain)NSArray *imageFileArray;
@property (nonatomic,retain)NSArray *titleArray;
// 初始化方法
+ (instancetype)zlqScrollViewWithFrame:(CGRect)frame;
- (instancetype)initWithFrame:(CGRect)frame;
//添加定时器 默认为关
+ (instancetype)zlqScrollViewWitnFrame:(CGRect)frame andOpenTimeter:(BOOL)addTime;
- (instancetype)initWithFrame:(CGRect)frame andOpenTimeter:(BOOL)addTime;
@property (nonatomic,assign)id <ZLQScrollViewClick> zlqScrollViewDelegate;
@end
ZLQScrollView.m
#import "ZLQScrollView.h"
#import "UIImageView+WebCache.h"
#define Screen [[UIScreen mainScreen] bounds]
@interface ZLQScrollView () <UIScrollViewDelegate>
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) NSTimer *timer;
@property (nonatomic, retain) UIPageControl *page;
@property (nonatomic, assign) BOOL addTime;
@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) CGFloat height;
@end
@implementation ZLQScrollView
- (void)dealloc {
[_page release];
[_scrollView release];
[_timer release];
[_imageArray release];
[_imageFileArray release];
[_titleArray release];
[super dealloc];
}
+ (instancetype)zlqScrollViewWithFrame:(CGRect)frame {
ZLQScrollView *zlqScrollView = [[ZLQScrollView alloc] initWithFrame:frame];
return [zlqScrollView autorelease];
}
+ (instancetype)zlqScrollViewWitnFrame:(CGRect)frame andOpenTimeter:(BOOL)addTime {
ZLQScrollView *zlqScrollView = [[ZLQScrollView alloc] initWithFrame:frame andOpenTimeter:addTime];
return [zlqScrollView autorelease];
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.addTime = NO;
self.width = frame.size.width;
[self setHeight:frame.size.height];
[self createSubviews];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame andOpenTimeter:(BOOL)addTime {
self = [super initWithFrame:frame];
if (self) {
self.addTime = addTime;
[self setWidth:frame.size.width];
[self setHeight:frame.size.height];
[self createSubviews];
}
return self;
}
- (void)createSubviews {
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
[self addSubview:self.scrollView];
[_scrollView release];
self.scrollView.delegate = self;
self.page = [[UIPageControl alloc] initWithFrame:CGRectMake([self width] - 200, [self height] - 60, 200, 50)];
[self addSubview:self.page];
[_page release];
}
- (void)setImageArray:(NSArray *)imageArray {
if (_imageArray != imageArray) {
[_imageArray release];
_imageArray = [imageArray retain];
}
[self scrollImage];
if (self.addTime == YES) {
[self addTitmer];
}
self.page.numberOfPages = self.titleArray.count;
}
- (void)setImageFileArray:(NSArray *)imageFileArray {
if (_imageFileArray != imageFileArray) {
[_imageFileArray release];
_imageFileArray = [imageFileArray retain];
}
[self scrollFileImage];
if (self.addTime == YES) {
[self addTitmer];
}
self.page.numberOfPages = self.titleArray.count;
}
/*
//无限循环 轮播图
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
// NSLog(@"%.2f -- %.2f", scrollView.contentOffset.x, scrollView.contentOffset.y);
NSInteger count = 0;
if (self.imageArray.count != 0) {
count = self.imageArray.count;
}
if (self.imageFileArray.count != 0) {
count = self.imageFileArray.count;
}
if (scrollView.contentOffset.x == 0) {
scrollView.contentOffset = CGPointMake(count * self.frame.size.width, 0);
// NSLog(@"%.2f -- %.2f", scrollView.contentOffset.x, scrollView.contentOffset.y);
} else if ((scrollView.contentOffset.x / self.frame.size.width) == (count + 1)){
scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
} else {
self.page.currentPage = scrollView.contentOffset.x / self.frame.size.width - 1;
}
}
*/
- (void)scrollFileImage {
self.scrollView.contentSize = CGSizeMake([self width] * (self.imageFileArray.count + 2), 0);
for (int i = 0; i < self.imageFileArray.count; i++) {
NSString *string = [self.imageFileArray objectAtIndex:i];
NSString *title = [self.titleArray objectAtIndex:i];
UIScrollView *smallScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake((i + 1) * self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
[self.scrollView addSubview:smallScrollView];
UIImageView *MyImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
MyImageView.userInteractionEnabled = YES;
MyImageView.tag = 10000 + i;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[MyImageView addGestureRecognizer:tap];
[tap release];
[smallScrollView addSubview:MyImageView];
[MyImageView release];
[MyImageView setImage:[UIImage imageNamed:string]];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, MyImageView.frame.size.height - 30, self.scrollView.frame.size.width, 30)];
titleLabel.text = title;
[titleLabel setBackgroundColor:[UIColor whiteColor]];
titleLabel.alpha = 0.5;
[smallScrollView addSubview:titleLabel];
[titleLabel release];
[smallScrollView release];
}
UIScrollView *smallScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
[self.scrollView addSubview:smallScrollView];
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
NSString *string = [self.imageFileArray lastObject];
NSString *title = [self.titleArray lastObject];
[myImageView setImage:[UIImage imageNamed:string]];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, myImageView.frame.size.height - 30, self.scrollView.frame.size.width, 30)];
titleLabel.text = title;
[titleLabel setBackgroundColor:[UIColor whiteColor]];
titleLabel.alpha = 0.5;
[smallScrollView addSubview:myImageView];
[smallScrollView addSubview:titleLabel];
[myImageView release];
[titleLabel release];
UIScrollView *smallScrollView2 = [[UIScrollView alloc] initWithFrame:CGRectMake((self.imageFileArray.count + 1) * self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
[self.scrollView addSubview:smallScrollView2];
NSString *string2 = [self.imageFileArray firstObject];
NSString *title2 = [self.titleArray firstObject];
UIImageView *myImageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
self.scrollView.contentOffset = CGPointMake([self width], 0);
[myImageView2 setImage:[UIImage imageNamed:string2]];
UILabel *titleLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(0, myImageView2.frame.size.height - 30, self.scrollView.frame.size.width, 30)];
titleLabel2.text = title2;
[titleLabel2 setBackgroundColor:[UIColor whiteColor]];
titleLabel2.alpha = 0.5;
[smallScrollView2 addSubview:myImageView2];
[smallScrollView2 addSubview:titleLabel2];
[myImageView2 release];
[titleLabel2 release];
[smallScrollView2 release];
}
//轮播图
- (void)scrollImage {
self.scrollView.contentSize = CGSizeMake([self width] * (self.imageArray.count + 2), 0);
for (int i = 0; i < self.imageArray.count; i++) {
NSString *urlString = [self.imageArray objectAtIndex:i];
NSURL *url = [NSURL URLWithString:urlString];
UIScrollView *smallScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake((i + 1) * self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
[self.scrollView addSubview:smallScrollView];
UIImageView *MyImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
MyImageView.userInteractionEnabled = YES;
MyImageView.tag = 10000 + i;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[MyImageView addGestureRecognizer:tap];
[tap release];
[smallScrollView addSubview:MyImageView];
[MyImageView release];
[MyImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
NSString *title = [self.titleArray objectAtIndex:i];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, MyImageView.frame.size.height - 30, self.scrollView.frame.size.width, 30)];
titleLabel.text = title;
titleLabel.textAlignment = NSTextAlignmentCenter;
[titleLabel setBackgroundColor:[UIColor whiteColor]];
titleLabel.alpha = 0.5;
[smallScrollView addSubview:titleLabel];
[titleLabel release];
[smallScrollView release];
}
UIScrollView *smallScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
[self.scrollView addSubview:smallScrollView];
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
NSString *urlString = [self.imageArray lastObject];
NSURL *url = [NSURL URLWithString:urlString];
[myImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, myImageView.frame.size.height - 30, self.scrollView.frame.size.width, 30)];
NSString *title = [self.titleArray lastObject];
titleLabel.text = title;
titleLabel.textAlignment = NSTextAlignmentCenter;
[titleLabel setBackgroundColor:[UIColor whiteColor]];
titleLabel.alpha = 0.5;
[smallScrollView addSubview:myImageView];
[smallScrollView addSubview:titleLabel];
[myImageView release];
[titleLabel release];
UIScrollView *smallScrollView2 = [[UIScrollView alloc] initWithFrame:CGRectMake((self.imageArray.count + 1) * self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
[self.scrollView addSubview:smallScrollView2];
NSString *urlString2 = [self.imageArray firstObject];
NSURL *url2 = [NSURL URLWithString:urlString2];
NSString *title2 = [self.titleArray firstObject];
UIImageView *myImageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
self.scrollView.contentOffset = CGPointMake([self width], 0);
[myImageView2 sd_setImageWithURL:url2 placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
UILabel *titleLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(0, myImageView2.frame.size.height - 30, self.scrollView.frame.size.width, 30)];
titleLabel2.text = title2;
titleLabel2.textAlignment = NSTextAlignmentCenter;
[titleLabel2 setBackgroundColor:[UIColor whiteColor]];
titleLabel2.alpha = 0.5;
[smallScrollView2 addSubview:myImageView2];
[smallScrollView2 addSubview:titleLabel2];
[myImageView2 release];
[titleLabel2 release];
[smallScrollView2 release];
}
//滚动时一直触发
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// NSLog(@"%.2f -- %.2f", scrollView.contentOffset.x, scrollView.contentOffset.y);
NSInteger count = 0;
if (self.imageArray.count != 0) {
count = self.imageArray.count;
}
if (self.imageFileArray.count != 0) {
count = self.imageFileArray.count;
}
if (scrollView.contentOffset.x == 0) {
scrollView.contentOffset = CGPointMake(count * self.frame.size.width, 0);
// NSLog(@"%.2f -- %.2f", scrollView.contentOffset.x, scrollView.contentOffset.y);
} else if ((scrollView.contentOffset.x / self.frame.size.width) == (count + 1)){
scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
} else {
self.page.currentPage = scrollView.contentOffset.x / self.frame.size.width - 1;
}
}
//将要开始拖拽时触发
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
if (self.addTime == YES) {
[self removeTimer];
}
}
//拖拽结束时被触发
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
if (self.addTime == YES) {
[self addTitmer];
}
}
- (void)tapAction:(UITapGestureRecognizer *)tap {
NSInteger index = tap.view.tag - 10000;
NSLog(@"index = %ld",index);
if ([self.zlqScrollViewDelegate respondsToSelector:@selector(zlqScrollViewClick:)]) {
[self.zlqScrollViewDelegate zlqScrollViewClick:index];
return;
}
[self.zlqScrollViewDelegate zlqScrollViewClick:index];
}
- (void)pageAction:(UIPageControl *)page {
[self.scrollView setContentOffset:CGPointMake(([self width] * page.currentPage + 1), 0) animated:YES];
}
- (void)addTitmer {
self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
}
- (void)removeTimer {
[self.timer invalidate];
}
- (void)nextImage {
int page = (int) self.page.currentPage;
if (page == self.titleArray.count - 1) {
page = 0;
} else {
page++;
}
//滚动Scrollview
CGFloat x = ((page * self.scrollView.frame.size.width) + self.frame.size.width) / self.frame.size.width;
self.scrollView.contentOffset = CGPointMake(x * self.frame.size.width, 0);
}
@end