博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ScrollviewView自动滚动广告栏
阅读量:6375 次
发布时间:2019-06-23

本文共 9147 字,大约阅读时间需要 30 分钟。

hot3.png

//model#import 
@interface AdDataModel : NSObject@property (retain,nonatomic,readonly) NSArray * imageNameArray;@property (retain,nonatomic,readonly) NSArray * adTitleArray;- (instancetype)initWithImageName;- (instancetype)initWithImageNameAndAdTitleArray;+ (id)adDataModelWithImageName;+ (id)adDataModelWithImageNameAndAdTitleArray;@end#import "AdDataModel.h"#define PLISTFILENAME @"AdDataPlist.plist"#define PATH  [[NSBundle mainBundle]pathForResource:PLISTFILENAME ofType:nil]@implementation AdDataModel- (instancetype)initWithImageName{    self = [super init];    if (self)    {//        _imageNameArray = [NSArray arrayWithContentsOfFile:PATH][0];        _imageNameArray = [NSArray arrayWithObjects:@"weather.png",@"videoeditor.png", @"notes.png",@"monitor.png",@"mms.png",nil];    }    return self;}- (instancetype)initWithImageNameAndAdTitleArray{    _adTitleArray = [NSArray arrayWithContentsOfFile:PATH][1];        return [self initWithImageName];}+ (id)adDataModelWithImageName{    return [[self alloc]initWithImageName];}+ (id)adDataModelWithImageNameAndAdTitleArray{    return [[self alloc]initWithImageNameAndAdTitleArray];}@end

//视图

#import 
typedef NS_ENUM(NSUInteger, UIPageControlShowStyle){    UIPageControlShowStyleNone,//default    UIPageControlShowStyleLeft,    UIPageControlShowStyleCenter,    UIPageControlShowStyleRight,};typedef NS_ENUM(NSUInteger, AdTitleShowStyle){    AdTitleShowStyleNone,    AdTitleShowStyleLeft,    AdTitleShowStyleCenter,    AdTitleShowStyleRight,};@interface AdScrollView : UIScrollView
@property (retain,nonatomic,readonly) UIPageControl * pageControl;@property (retain,nonatomic,readwrite) NSArray * imageNameArray;@property (retain,nonatomic,readonly) NSArray * adTitleArray;@property (assign,nonatomic,readwrite) UIPageControlShowStyle  PageControlShowStyle;@property (assign,nonatomic,readonly) AdTitleShowStyle  adTitleStyle;- (void)setAdTitleArray:(NSArray *)adTitleArray withShowStyle:(AdTitleShowStyle)adTitleStyle;@end#import "AdScrollView.h"#define UISCREENWIDTH  self.bounds.size.width//广告的宽度#define UISCREENHEIGHT  self.bounds.size.height//广告的高度#define HIGHT self.bounds.origin.y //由于_pageControl是添加进父视图的,所以实际位置要参考,滚动视图的y坐标static CGFloat const chageImageTime = 3.0;static NSUInteger currentImage = 1;//记录中间图片的下标,开始总是为1@interface AdScrollView (){    //广告的label    UILabel * _adLabel;    //循环滚动的三个视图    UIImageView * _leftImageView;    UIImageView * _centerImageView;    UIImageView * _rightImageView;        //循环滚动的周期时间    NSTimer * _moveTime;        //用于确定滚动式由人导致的还是计时器到了,系统帮我们滚动的,YES,则为系统滚动,NO则为客户滚动(ps.在客户端中客户滚动一个广告后,这个广告的计时器要归0并重新计时)    BOOL _isTimeUp;        //为每一个图片添加一个广告语(可选)    UILabel * _leftAdLabel;    UILabel * _centerAdLabel;    UILabel * _rightAdLabel;}@property (retain,nonatomic,readonly) UIImageView * leftImageView;@property (retain,nonatomic,readonly) UIImageView * centerImageView;@property (retain,nonatomic,readonly) UIImageView * rightImageView;@end@implementation AdScrollView#pragma mark - 自由指定广告所占的frame- (instancetype)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        self.bounces = NO;                self.showsHorizontalScrollIndicator = NO;        self.showsVerticalScrollIndicator = NO;        self.pagingEnabled = YES;        self.contentOffset = CGPointMake(UISCREENWIDTH, 0);        self.contentSize = CGSizeMake(UISCREENWIDTH * 3, UISCREENHEIGHT);        self.backgroundColor = [UIColor whiteColor];        self.delegate = self;                                _leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, UISCREENWIDTH, UISCREENHEIGHT)];        [self addSubview:_leftImageView];        _centerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(UISCREENWIDTH, 0, UISCREENWIDTH, UISCREENHEIGHT)];        [self addSubview:_centerImageView];        _rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(UISCREENWIDTH*2, 0, UISCREENWIDTH, UISCREENHEIGHT)];        [self addSubview:_rightImageView];                _moveTime = [NSTimer scheduledTimerWithTimeInterval:chageImageTime target:self selector:@selector(animalMoveImage) userInfo:nil repeats:YES];        _isTimeUp = NO;            }    return self;}#pragma mark - 设置广告所使用的图片(名字)- (void)setImageNameArray:(NSArray *)imageNameArray{    _imageNameArray = imageNameArray;//    _leftImageView.image = [UIImage imageNamed:_imageNameArray[0]];    _leftImageView.image = [UIImage imageNamed:@"weather.png"];//    _centerImageView.image = [UIImage imageNamed:_imageNameArray[1]];    _centerImageView.image = [UIImage imageNamed:@"videoeditor.png"];//    _rightImageView.image = [UIImage imageNamed:_imageNameArray[2]];    _rightImageView.image = [UIImage imageNamed:@"notes.png"];}#pragma mark - 设置每个对应广告对应的广告语- (void)setAdTitleArray:(NSArray *)adTitleArray withShowStyle:(AdTitleShowStyle)adTitleStyle{    _adTitleArray = adTitleArray;        if(adTitleStyle == AdTitleShowStyleNone)    {        return;    }        _leftAdLabel = [[UILabel alloc]init];    _centerAdLabel = [[UILabel alloc]init];    _rightAdLabel = [[UILabel alloc]init];            _leftAdLabel.frame = CGRectMake(10, UISCREENHEIGHT - 40, UISCREENWIDTH, 20);    [_leftImageView addSubview:_leftAdLabel];    _centerAdLabel.frame = CGRectMake(10, UISCREENHEIGHT - 40, UISCREENWIDTH, 20);    [_centerImageView addSubview:_centerAdLabel];    _rightAdLabel.frame = CGRectMake(10, UISCREENHEIGHT - 40, UISCREENWIDTH, 20);    [_rightImageView addSubview:_rightAdLabel];        if (adTitleStyle == AdTitleShowStyleLeft) {        _leftAdLabel.textAlignment = NSTextAlignmentLeft;        _centerAdLabel.textAlignment = NSTextAlignmentLeft;        _rightAdLabel.textAlignment = NSTextAlignmentLeft;    }    else if (adTitleStyle == AdTitleShowStyleCenter)    {        _leftAdLabel.textAlignment = NSTextAlignmentCenter;        _centerAdLabel.textAlignment = NSTextAlignmentCenter;        _rightAdLabel.textAlignment = NSTextAlignmentCenter;    }    else    {        _leftAdLabel.textAlignment = NSTextAlignmentRight;        _centerAdLabel.textAlignment = NSTextAlignmentRight;        _rightAdLabel.textAlignment = NSTextAlignmentRight;    }            _leftAdLabel.text = _adTitleArray[0];    _centerAdLabel.text = _adTitleArray[1];    _rightAdLabel.text = _adTitleArray[2];    }#pragma mark - 创建pageControl,指定其显示样式- (void)setPageControlShowStyle:(UIPageControlShowStyle)PageControlShowStyle{    if (PageControlShowStyle == UIPageControlShowStyleNone) {        return;    }    _pageControl = [[UIPageControl alloc]init];    _pageControl.numberOfPages = _imageNameArray.count;        if (PageControlShowStyle == UIPageControlShowStyleLeft)    {        _pageControl.frame = CGRectMake(10, HIGHT+UISCREENHEIGHT - 20, 20*_pageControl.numberOfPages, 20);    }    else if (PageControlShowStyle == UIPageControlShowStyleCenter)    {        _pageControl.frame = CGRectMake(0, 0, 20*_pageControl.numberOfPages, 20);        _pageControl.center = CGPointMake(UISCREENWIDTH/2.0, HIGHT+UISCREENHEIGHT - 10);    }    else    {        _pageControl.frame = CGRectMake( UISCREENWIDTH - 20*_pageControl.numberOfPages, HIGHT+UISCREENHEIGHT - 20, 20*_pageControl.numberOfPages, 20);    }    _pageControl.currentPage = 0;        _pageControl.enabled = NO;        [self performSelector:@selector(addPageControl) withObject:nil afterDelay:0.1f];}//由于PageControl这个空间必须要添加在滚动视图的父视图上(添加在滚动视图上的话会随着图片滚动,而达不到效果)- (void)addPageControl{    [[self superview] addSubview:_pageControl];}#pragma mark - 计时器到时,系统滚动图片- (void)animalMoveImage{        [self setContentOffset:CGPointMake(UISCREENWIDTH * 2, 0) animated:YES];    _isTimeUp = YES;    [NSTimer scheduledTimerWithTimeInterval:0.4f target:self selector:@selector(scrollViewDidEndDecelerating:) userInfo:nil repeats:NO];}#pragma mark - 图片停止时,调用该函数使得滚动视图复用- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{    if (self.contentOffset.x == 0)    {        currentImage = (currentImage-1)%_imageNameArray.count;        _pageControl.currentPage = (_pageControl.currentPage - 1)%_imageNameArray.count;    }    else if(self.contentOffset.x == UISCREENWIDTH * 2)    {               currentImage = (currentImage+1)%_imageNameArray.count;       _pageControl.currentPage = (_pageControl.currentPage + 1)%_imageNameArray.count;    }    else    {        return;    }        _leftImageView.image = [UIImage imageNamed:_imageNameArray[(currentImage-1)%_imageNameArray.count]];        _leftAdLabel.text = _adTitleArray[(currentImage-1)%_imageNameArray.count];        _centerImageView.image = [UIImage imageNamed:_imageNameArray[currentImage%_imageNameArray.count]];        _centerAdLabel.text = _adTitleArray[currentImage%_imageNameArray.count];        _rightImageView.image = [UIImage imageNamed:_imageNameArray[(currentImage+1)%_imageNameArray.count]];        _rightAdLabel.text = _adTitleArray[(currentImage+1)%_imageNameArray.count];        self.contentOffset = CGPointMake(UISCREENWIDTH, 0);        //手动控制图片滚动应该取消那个三秒的计时器    if (!_isTimeUp) {        [_moveTime setFireDate:[NSDate dateWithTimeIntervalSinceNow:chageImageTime]];    }    _isTimeUp = NO;}@end

转载于:https://my.oschina.net/u/2319073/blog/596969

你可能感兴趣的文章
java-jmx使用
查看>>
Win8Metro(C#)数字图像处理--2.15图像霓虹效果
查看>>
Expo大作战(十七)--expo结合哨兵(sentry)进行错误异常记录
查看>>
vue.js入门学习
查看>>
第8件事 3步打造产品的独特气质
查看>>
debug-stripped.ap_' specified for property 'resourceFile' does not exist
查看>>
利用MapReduce计算平均数
查看>>
scala-05-map映射
查看>>
Spring Boot - how to configure port
查看>>
右键添加复制路径选项
查看>>
DocFetcher 本机文件搜索工具
查看>>
ambassador 学习三 限速处理
查看>>
HTTP传输编码增加了传输量,只为解决这一个问题 | 实用 HTTP
查看>>
家里蹲大学数学杂志期刊模式目录
查看>>
数据结构:最小生成树--Kruskal算法
查看>>
Swift_1_基本数据类型
查看>>
POJ 1849 Two(遍历树)
查看>>
Recurrent Neural Network[CTC]
查看>>
VS注释与取消注释快捷键
查看>>
深入解析Vuex实战总结
查看>>