iOS实现三屏复用循环广告[从服务器请求的广告]
循环广告我们在开发中已经是熟得不能再熟了,今天整理这篇scrollview三屏复用广告
原理使用scrollview里的三个imageview分别去加载不同的图片,用少量的资源来显示大量或不确定的广告数量,不然如果用普通方法实现广告,难道10个广告用12个scrollview的contentsize去做,岂不是太浪费资源了
代码如下,实现所有数量的循环广告,当广告只有一个时,仅采用单图显示,>=2个广告时,自动采用三屏复用
这里添加图片的方式是通过网络请求,更新服务器上的广告,如果仅使用本地广告,可以将.m文件里的全部图片的添加方式
如:
self.endImageView.image = self.imageArray[endImageCount];
修改为
self.endImageView.image = [UIImage imageNamed:self.imageArray[endImageCount]];
然后在使用该类时,直接将本地图片的名字用数组传过去就行了,如
cview.imageArray = [[NSMutableArray alloc]initWithObjects:@"图片1",@"图片2",@"图片3", nil]; 或者不改则使用方法如
NSArray *imageArr = [[NSArray alloc]initWithObjects:@"banner_理财.jpg",@"banner_惠普",@"banner_炒股", nil];
for (int i=0; i<3; i++) {
UIImage *cirImage1 = [UIImage imageNamed:imageArr[i]];
[cirScrollView.imageArray addObject:cirImage1];
}
如果图片给的是地址那可以用imageWithURL这个方法来获取图片
下面讲从服务器获取的广告方式,请求服务器图片及解析这里就不讲了,仅从获取到的data数据后开始
先新建一个类继承UIView,
.h里
#import <UIKit/UIKit.h> @interface CirculateScrollview : UIView @property (nonatomic,strong)NSMutableArray *imageArray;//图片数组
@property (nonatomic,strong)UIScrollView *circulateScrollView;//广告 /*
三屏复用广告
适用范围:网络请求或固定本地的广告图片
适用所有数量广告,广告>=2时自动采用三屏复用技术
使用方法:例
在需要添加广告的控制器里面 CirculateScrollview *cview = [[CirculateScrollview alloc]initWithFrame:CGRectMake(0, 20, 320, 200)];
for (int i=0; i<3; i++) {
UIImage *image = [UIImage imageNamed:@"旅行图1"];//传进图片名字方式
//UIImage *image = UIImage imageWithData:data];//传进data数据图片方式将服务器请求到的data数据图片转换成image形式再传输
[cview.imageArray addObject:image];
}
[self.view addSubview:cview]; */ /*
图片转换NSData方法
测试可用
NSData * data = UIImageJPEGRepresentation(image, 1);
*/ @end
.m文件里
实现方法是这三个成员变量,用来读取传输过来的图片在数组中的位置,三屏复用里,我们显示的位置是scrollview的中间位置,左边广告是全部广告的最后一个,中间显示第一个,右边的显示第二个,然后根据左滑成员变量递增,当变量递增到超过广告总数时,重新赋值第一个广告,而右滑递减,递减至-1时,即不在数组范围时,重新赋值广告数组的最后一个
#import "CirculateScrollview.h" #define ViewWidth self.frame.size.width
#define ViewHeight self.frame.size.height
#define AllImageCount self.imageArray.count-1 @interface CirculateScrollview()<UIScrollViewDelegate>
{
NSInteger endImageCount;//左边图片
NSInteger oneImageCount;//中间图片[当前看到的图片]
NSInteger secondImageCount;//右边图片
}
@property (nonatomic,strong)UIImageView *endImageView;
@property (nonatomic,strong)UIImageView *oneImageView;
@property (nonatomic,strong)UIImageView *secondImageView;
@property (nonatomic,strong)UIPageControl *pageCtl; @end @implementation CirculateScrollview -(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) { }
return self;
} -(NSMutableArray *)imageArray
{
if (!_imageArray) {
_imageArray = [[NSMutableArray alloc]init];
}
return _imageArray;
} - (void)drawRect:(CGRect)rect {
self.circulateScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(, , ViewWidth, ViewHeight)]; endImageCount = self.imageArray.count-;
oneImageCount = ;
secondImageCount = ; self.circulateScrollView.showsHorizontalScrollIndicator = NO;
self.circulateScrollView.pagingEnabled = YES;
self.circulateScrollView.delegate = self;
self.circulateScrollView.bounces = NO; self.circulateScrollView.contentOffset = CGPointMake(ViewWidth, ); self.backgroundColor = [UIColor whiteColor]; if (!self.imageArray.count) {
NSLog(@"图片数组为空");
return;
} //若广告数量少于2张则不采用三屏复用技术
if (self.imageArray.count<=){
self.circulateScrollView.contentSize = CGSizeMake(ViewWidth, ViewHeight); self.endImageView = [[UIImageView alloc]initWithFrame:CGRectMake(, , ViewWidth, ViewHeight)];
self.endImageView.image = self.imageArray[endImageCount];
[self.circulateScrollView addSubview:self.endImageView];
[self addSubview:self.circulateScrollView]; }else{
self.circulateScrollView.contentSize = CGSizeMake(ViewWidth*, ViewHeight); //左
self.endImageView = [[UIImageView alloc]initWithFrame:CGRectMake(, , ViewWidth, ViewHeight)];
self.endImageView.image = self.imageArray[endImageCount];
[self.circulateScrollView addSubview:self.endImageView];
//中
self.oneImageView = [[UIImageView alloc]initWithFrame:CGRectMake(ViewWidth, , ViewWidth, ViewHeight)];
self.oneImageView.image = self.imageArray[oneImageCount];
[self.circulateScrollView addSubview:self.oneImageView];
//右
self.secondImageView = [[UIImageView alloc]initWithFrame:CGRectMake(ViewWidth*, , ViewWidth, ViewHeight)];
self.secondImageView.image = self.imageArray[secondImageCount];
[self.circulateScrollView addSubview:self.secondImageView]; [self addSubview:self.circulateScrollView];
[self pageNumControl];
} }
//添加页符
-(void)pageNumControl
{
self.pageCtl = [[UIPageControl alloc]initWithFrame:CGRectMake(, ViewHeight-, ViewWidth, )];
self.pageCtl.backgroundColor = [UIColor lightGrayColor];
self.pageCtl.currentPageIndicatorTintColor = [UIColor greenColor];
self.pageCtl.pageIndicatorTintColor = [UIColor whiteColor];
self.pageCtl.alpha = 0.7;
self.pageCtl.numberOfPages = AllImageCount+;
[self addSubview:self.pageCtl];
} -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.x == ) {
endImageCount--;
oneImageCount--;
secondImageCount--;
if (endImageCount<) {
endImageCount = AllImageCount;
}else if (oneImageCount<){
oneImageCount = AllImageCount;
}
//适配2张图片
if (secondImageCount<){
secondImageCount = AllImageCount;
}
//NSLog(@"endImageCount=%ld oneImageCount=%ld secondImageCount=%ld",endImageCount,oneImageCount,secondImageCount); }else if(scrollView.contentOffset.x == ViewWidth*){
endImageCount++;
oneImageCount++;
secondImageCount++;
if (endImageCount>AllImageCount) {
endImageCount = ;
}else if (oneImageCount>AllImageCount){
oneImageCount = ;
}
//适配2张图片
if (secondImageCount>AllImageCount){
secondImageCount = ;
}
}
//重新加载显示当前位置的图片
scrollView.contentOffset = CGPointMake(ViewWidth, );
self.endImageView.image = self.imageArray[endImageCount];
self.oneImageView.image = self.imageArray[oneImageCount];
self.secondImageView.image = self.imageArray[secondImageCount];
self.pageCtl.currentPage = oneImageCount;
} @end
iOS实现三屏复用循环广告[从服务器请求的广告]的更多相关文章
- iOS 的三种自建证书方法https请求相关配置
如果你的app服务端安装的是SLL颁发的CA,可以使用系统方法直接实现信任SSL证书,关于Apple对SSL证书的要求请参考:苹果官方文档CertKeyTrustProgGuide 这种方式不需要在B ...
- iOS回顾笔记(05) -- 手把手教你封装一个广告轮播图框架
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...
- Delphi IOS 蓝牙锁屏后台运行
Delphi IOS 后台运行 同样的程序,编译成android,锁屏后继续运行正常,蓝牙通讯正常,但在IOS下锁屏后程序的蓝牙就中断通讯了? IOS的机制就是这样,锁屏就关闭了. 音乐播放器是怎么做 ...
- iOS的三种多线程技术NSThread/NSOperation/GCD
1.iOS的三种多线程技术 1.NSThread 每个NSThread对象对应一个线程,量级较轻(真正的多线程) 2.以下两点是苹果专门开发的"并发"技术,使得程序员可以不再去关心 ...
- Android 和 iOS 实现录屏推流的方案整理
一.录屏推流实现的步骤 1. 采集数据 主要是采集屏幕获得视频数据,采集麦克风获得音频数据,如果可以实现的话,我们还可以采集一些应用内置的音频数据. 2. 数据格式转换 主要是将获取到的视频和音频转换 ...
- iOS 9 分屏多任务:入门(中文版)
本文转载至 http://www.cocoachina.com/ios/20150714/12555.html 本文由钢铁侠般的卿哥(微博)翻译自苹果官方文档:Adopting Multitaskin ...
- iOS 实现启动屏动画(Swift实现,包含图片适配)
代码地址如下:http://www.demodashi.com/demo/12090.html 准备工作 首先我们需要确定作为宣传的图片的宽高比,这个一般是与 UI 确定的.一般启动屏展示会有上下两部 ...
- iOS开发三步搞定百度推送
iOS开发三步搞定百度推送 百度推送很简单,准备工作:在百度云推送平台注册应用,上传证书. 步骤一: 百度云推送平台 http://push.baidu.com/sdk/push_client_s ...
- ios手机录屏软件哪个好
苹果手机中的airplay镜像,是苹果手机系统的一大特色,可以轻松把手机屏幕投射电脑,这个功能使苹果手机相较安卓手机投屏会更加轻松,那么如何实现苹果手机投射电脑屏幕?下面小编便来分享ios手机录屏软件 ...
随机推荐
- win7中的画图
下午做一个图像篡改定位实验,在win7下对图像进行修改,然后通过其最低位判断篡改位置. 程序如何都调试不出来.反复简化程序,发现win7中的画图对图像存在优化. 当更改图像后,会自动调整当前图像的显示 ...
- Java 集合系列02之 Collection架构
概要 首先,我们对Collection进行说明.下面先看看Collection的一些框架类的关系图: Collection是一个接口,它主要的两个分支是:List 和 Set. List和Set都是接 ...
- iOS- 微信支付 (服务器调起支付 )以及回调不成功的原因 不看后悔
写的不错,给留个言哈... 一. 支付准备工作 1. 微信相关准备工作 (1) 向微信官方开通支付功能. 这个不是前端的工作. (2) 导入官方下载的微信支付SDK包. 我用的是微信开放平台下载的SD ...
- 《HelloGitHub月刊》第02期
<HelloGithub>第02期 兴趣是最好的老师,而<HelloGitHub> 就是帮你找到兴趣! 因为我比较熟悉python语言,所以月刊中python语言的项目居多,个 ...
- android resources使用总结
http://developer.android.com/guide/topics/resources/more-resources.html http://developer.android.com ...
- 资料下载:敏捷个人的成长思考.pptx(第1次线下活动2011.04)
本文挪至 http://www.zhoujingen.cn/blog/629.html PDF下载地址:http://down.51cto.com/data/207112 推荐:你可能需要的在线电子书 ...
- UWP开发入门(十二)——神器Live Visual Tree
很久以前,我们就有Snoop这样的工具实时修改.查看正在运行的WPF程序,那时候调个样式,修改个模板,相当滋润.随着历史的车轮陷进WP的泥潭中,无论WP7的Silverlight还是WP8.1的run ...
- [Solution] DI原理解析及Castle、Unity框架使用
本节目录 DI介绍 DI基本原理 DI框架 Castle Windsor Unity Autofac Spring.Net DI介绍 控制反转(Inversion of Control,英文缩写为Io ...
- VC使用libcurl模拟登录CSDN并自动评论资源以获取积分
环境:Win7 64位+VC2008 软件及源码下载:(http://pan.baidu.com/s/1jGE52pK) 涉及到的知识点: C++多线程编程 libcurl的使用(包括发送http请求 ...
- SQL Server 2008 评估期已过解决方法
SQL Server 2008有180天的试用期,过期后会提示“评估期已过”的提示. 1.进入SQL Server安装中心: 2.选择“维护”-“版本升级” 3.输入密钥: 其他的根据提示操作. 附S ...