scrollView 滑动滚广告

本文介绍了一个用于iOS应用的轮播广告视图组件,该组件基于UIView实现,支持动态加载广告数据并自动轮播。组件通过UIScrollView展示图片,并利用UIPageControl指示当前播放的广告位置。


#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];


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值