#import <UIKit/UIKit.h>
@protocol AdvertisementCardWidgetDelegate <NSObject>
@optional
- (void)protocolAdvertisementCardWidgetMoreInfo:(id)object;
@end
#define KEYADVERTISEMENTDATAID @"KEYADVERTISEMENTDATAID"// 每一个信息ID(雷同数据库ID)
#define KEYADVERTISEMENTFROM @"KEYADVERTISEMENTFROM"// 谁在调用广告卡
#define KEYADVERTISEMENTDETAILINFOID @"KEYADVERTISEMENTDETAILINFOID"// 每一个广告的查询ID(点击进入下一级页面的需求)
#define KEYADVERTISEMENTIMAGEURL @"KEYADVERTISEMENTIMAGEURL"//图片url
#define KEYADVERTISEMENTOBJECT @"KEYADVERTISEMENTOBJECT"//单个ADVERTISEMENT对象
#define KEYADVERTISEMENTTAG 100 //tag值的基数
@interface LZAdvertisementCardWidget :UIView
<UIScrollViewDelegate>
{
NSMutableArray *imageArray;
// CGFloat width;
// CGFloat height;
UIScrollView *imageScroll;
UIPageControl *pageControl;
NSTimer *timer;
}
@property (nonatomic,weak) id<AdvertisementCardWidgetDelegate> delegate;
@property (nonatomic,assign)int currentIndex;
@property (nonatomic,strong)NSDictionary *currentDic;
//- (id)initWithFrame:(CGRect)frame data:(NSArray *)array;
//- (id)initWithFrame:(CGRect)frame data:(NSDictionary *)dictionary;
//-(void)loadData:(NSArray *)array;
- (void)loadImage:(int)count;
- (void)loadDynamicAdvertisement:(NSArray *)array;
- (void)loadAdvertisementData:(NSDictionary *)dictionary;
- (void)refreshImage:(UIImage *)image index:(NSNumber *)tagNumber;
- (void)removeTimer;
@end
// Created by LvZhao on 15/5/15.
#import "LZAdvertisementCardWidget.h"
@implementation LZAdvertisementCardWidget
- (void)dealloc
{
if (timer) {
[selfremoveTimer];
}
imageScroll.delegate =nil;
}
- (id)initWithFrame:(CGRect)frame
{
self = [superinitWithFrame:frame];
if (self) {
CGFloat width =CGRectGetWidth(self.frame);
CGFloat height =CGRectGetHeight(self.frame);
UIImageView *bgd = [[UIImageViewalloc]initWithFrame:CGRectMake(0, 0, width, height)];
bgd.backgroundColor = [UIColorclearColor];
bgd.image = [UIImageimageNamed:@"topAD_defaut"];
[selfaddSubview:bgd];
imageScroll = [[UIScrollViewalloc]initWithFrame:CGRectMake(0, 0, width, height)];
imageScroll.backgroundColor = [UIColorclearColor];
imageScroll.delegate =self;
imageScroll.showsHorizontalScrollIndicator =NO;
imageScroll.pagingEnabled =YES;
imageScroll.scrollsToTop =NO;
[selfaddSubview:imageScroll];
pageControl = [[UIPageControlalloc] initWithFrame:CGRectMake(0, height - 20,width, 20)];
pageControl.backgroundColor = [UIColorclearColor];
pageControl.userInteractionEnabled =NO;
pageControl.currentPageIndicatorTintColor = [UIColorredColor];
pageControl.pageIndicatorTintColor = [UIColorwhiteColor];
pageControl.currentPage = 0;
[selfaddSubview:pageControl];
}
returnself;
}
- (void)loadImage:(int)count
{
int imageCount = count + 2;
pageControl.numberOfPages = count;
CGFloat width =CGRectGetWidth(self.frame);
CGFloat height =CGRectGetHeight(self.frame);
for (int i = 0 ; i < imageCount ; i++) {
CGRect rect =CGRectMake(width * i , 0, width, height);
UIImageView *adImageView = [[UIImageViewalloc]initWithFrame:rect];
adImageView.tag =KEYADVERTISEMENTTAG + i;
adImageView.backgroundColor = [UIColorclearColor];
[imageScrolladdSubview:adImageView];
adImageView.userInteractionEnabled =YES;
UITapGestureRecognizer *tapGestureRecognize = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(singleTapGestureRecognizer:)];
tapGestureRecognize.numberOfTapsRequired = 1;
[adImageView addGestureRecognizer:tapGestureRecognize];
}
imageScroll.contentSize =CGSizeMake(width * imageCount, height);
imageScroll.contentOffset =CGPointMake(width, 0);
}
#pragma mark - 动态轮播广告调用
- (void)loadDynamicAdvertisement:(NSArray *)array {
// 配置数据
if (imageArray) {
[imageArrayremoveAllObjects];
}else{
imageArray = [[NSMutableArrayalloc]init];
}
[imageArrayaddObjectsFromArray:array];
for (UIImageView *imageViewin imageScroll.subviews) {
[imageView removeFromSuperview];
}
if (array.count > 1) {
NSDictionary *insertFirstDic = array.lastObject;
NSDictionary *insertLastDic = array.firstObject;
[imageArrayinsertObject:insertFirstDic atIndex:0];
[imageArrayaddObject:insertLastDic];
self.currentDic =imageArray[1];
self.currentIndex = 1;
pageControl.hidden =NO;
pageControl.numberOfPages = array.count;
} else {
self.currentDic =imageArray.firstObject;
self.currentIndex = 0;
pageControl.hidden =YES;
}
CGFloat width =CGRectGetWidth(self.frame);
CGFloat height =CGRectGetHeight(self.frame);
for (int i = 0 ; i <imageArray.count ; i++) {
CGRect rect =CGRectMake(width * i , 0, width, height);
UIImageView *adImageView = [[UIImageViewalloc]initWithFrame:rect];
adImageView.tag =KEYADVERTISEMENTTAG + i;
adImageView.backgroundColor = [UIColorclearColor];
[imageScrolladdSubview:adImageView];
adImageView.userInteractionEnabled =YES;
UITapGestureRecognizer *tapGestureRecognize = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(singleTapGestureRecognizer:)];
tapGestureRecognize.numberOfTapsRequired = 1;
[adImageView addGestureRecognizer:tapGestureRecognize];
NSDictionary *dic =imageArray[i];
adImageView.image = [UIImageimageNamed:@"1"];
}
imageScroll.contentSize =CGSizeMake(width * imageArray.count, height);
if (array.count > 1) {
if (timer==nil) {
timer = [NSTimerscheduledTimerWithTimeInterval:2.0ftarget:selfselector:@selector(scroll)userInfo:nilrepeats:YES];
}
imageScroll.contentOffset =CGPointMake(width, 0);
} else {
imageScroll.contentOffset =CGPointMake(0, 0);
}
}
#pragma mark - 广告卡调用
- (void)loadAdvertisementData:(NSDictionary *)dictionary
{
NSArray *array = dictionary[@"NSArray"];
if (array.count > 0) {
// 配置数据
if (imageArray) {
[imageArrayremoveAllObjects];
}else{
imageArray = [[NSMutableArrayalloc]init];
}
[imageArrayaddObjectsFromArray:array];
NSDictionary *insertFirstDic = array.lastObject;
NSDictionary *insertLastDic = array.firstObject;
[imageArrayinsertObject:insertFirstDic atIndex:0];
[imageArrayaddObject:insertLastDic];
for (UIImageView *imageViewin imageScroll.subviews) {
[imageView removeFromSuperview];
}
CGFloat width =CGRectGetWidth(self.frame);
CGFloat height =CGRectGetHeight(self.frame);
for (int i = 0 ; i <imageArray.count ; i++) {
CGRect rect =CGRectMake(width * i , 0, width, height);
UIImageView *adImageView = [[UIImageViewalloc]initWithFrame:rect];
adImageView.tag =KEYADVERTISEMENTTAG + i;
adImageView.backgroundColor = [UIColorclearColor];
[imageScrolladdSubview:adImageView];
adImageView.userInteractionEnabled =YES;
UITapGestureRecognizer *tapGestureRecognize = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(singleTapGestureRecognizer:)];
tapGestureRecognize.numberOfTapsRequired = 1;
[adImageView addGestureRecognizer:tapGestureRecognize];
NSDictionary *dic =imageArray[i];
// UIImageView *currentImageView = (UIImageView *)[imageScroll viewWithTag:KEYADVERTISEMENTTAG + i];//第 0个
adImageView.image = [UIImageimageNamed:@"1"];
}
if (array.count>0) {
self.currentDic =imageArray[1];
self.currentIndex = 1;
if (array.count > 1) {
if (timer==nil) {
timer = [NSTimerscheduledTimerWithTimeInterval:2.0ftarget:selfselector:@selector(scroll)userInfo:nilrepeats:YES];
}
}
}
pageControl.numberOfPages = array.count;
imageScroll.contentSize =CGSizeMake(width * imageArray.count, height);
imageScroll.contentOffset =CGPointMake(width, 0);
}else{
pageControl.numberOfPages = 0;
imageScroll.contentSize =CGSizeMake(0, 0);
imageScroll.contentOffset =CGPointMake(0, 0);
}
}
#pragma mark 定时器跑马灯(定时器初始化当前索引为 1 )
- (void)scroll
{
self.currentIndex ++;
[imageScrollsetContentOffset:CGPointMake(CGRectGetWidth(self.frame)*self.currentIndex, 0) animated:YES];
if (self.currentIndex >=imageArray.count - 1) {
[selfperformSelector:@selector(restImageScroll)withObject:nilafterDelay:0.4f];
pageControl.currentPage = 0;
self.currentIndex = 1;
}else{
pageControl.currentPage =self.currentIndex - 1;
}
self.currentDic =imageArray[self.currentIndex];
}
- (void)restImageScroll
{
imageScroll.contentOffset =CGPointMake(CGRectGetWidth(self.frame), 0);
}
- (void)removeTimer
{
if (timer) {
[timerinvalidate];
timer =nil;
}
}
- (void)singleTapGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
UIImageView *imageView = (UIImageView *)gestureRecognizer.view;
NSUInteger tagValue = imageView.tag -KEYADVERTISEMENTTAG;
if (imageArray &&imageArray.count > 0) {
NSDictionary *dic =imageArray[tagValue];
if (dic !=nil) {
if (self.delegate && [self.delegaterespondsToSelector:@selector(protocolAdvertisementCardWidgetMoreInfo:)]) {
[self.delegateprotocolAdvertisementCardWidgetMoreInfo:dic];
}
}
}
}
- (void)refreshImage:(UIImage *)image index:(NSNumber *)tagNumber
{
int tag = [tagNumberintValue];
if (tag==KEYADVERTISEMENTTAG) {
UIImageView *firstImageView = (UIImageView *)[imageScrollviewWithTag:tag];//第 0个
UIImageView *reciprocalSecondImageView = (UIImageView *)[imageScrollviewWithTag:imageArray.count-1];//倒数第 2个
firstImageView.image = image;
reciprocalSecondImageView.image = image;
}elseif (tag==KEYADVERTISEMENTTAG + (imageArray.count-1)) {
UIImageView *lastImageView = (UIImageView *)[imageScrollviewWithTag:tag];//最后一个
UIImageView *secondImageView = (UIImageView *)[imageScrollviewWithTag:KEYADVERTISEMENTTAG + 1];//正数第 2个
lastImageView.image = image;
secondImageView.image = image;
}else{
UIImageView *imageView = (UIImageView *)[imageScrollviewWithTag:tag];
imageView.image = image;
}
}
#pragma mark-
#pragma mark-
#pragma mark-
#pragma mark- **********************************************系统协议
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
// // 一般不在此方法处理数据及 操作页面UI,这将时时占用内存(如必须使用可进行数据处理操作,尽可能不在这里操作UI)
//}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
if (timer) {
[selfremoveTimer];
}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if (imageArray.count > 3) {
if (timer==nil) {
timer = [NSTimerscheduledTimerWithTimeInterval:2.0ftarget:selfselector:@selector(scroll)userInfo:nilrepeats:YES];
}
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
self.currentIndex = (int)(scrollView.contentOffset.x / scrollView.frame.size.width);
self.currentDic =imageArray[self.currentIndex];
if (self.currentIndex >=imageArray.count - 1) {
scrollView.contentOffset =CGPointMake(CGRectGetWidth(self.frame), 0);
pageControl.currentPage = 0;
}elseif (self.currentIndex == 0) {
scrollView.contentOffset =CGPointMake((imageArray.count-2)*CGRectGetWidth(self.frame), 0);
pageControl.currentPage =imageArray.count - 1;
}else{
pageControl.currentPage =self.currentIndex - 1;
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
NSMutableArray * array = [[NSMutableArrayalloc]init];
for (int i = 0; i <7 ;i++) {
NSMutableDictionary * dict = [[NSMutableDictionaryalloc]init];
[dict setValue:@"123"forKey:[NSStringstringWithFormat:@"i"]];
[array addObject:dict];
}
CGFloat height = 228.0 / 720.0 *self.view.frame.size.width;
LZAdvertisementCardWidget* advertisementView = [[LZAdvertisementCardWidgetalloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, height)];
advertisementView.backgroundColor = [UIColorgroupTableViewBackgroundColor];
advertisementView.delegate =self;
[advertisementView loadDynamicAdvertisement:array];