iOS实现动态的开屏广告示例代码_IOS

一、实现效果图

二、实现思路:

用一个固定的png图片左启动图,应该和广告视图需要进行动画的期初的位置一致,当启动图消失的时候,呈现出图片,实际遇到的困难是,因为广告图片是从网络请求加载的,当时把广告视图放在了请求数据的块里面,广告出现的时候会闪一下,放在外面就没事了。

三、实现示例

1.广告的头文件

// XBAdvertView.h
// scoreCount
//
// Created by 王国栋 on 15/12/22.
// Copyright  2015年 xiaobai. All rights reserved.
// 

#import <UIKit/UIKit.h>
@protocol XBAdvertViewDelegate <NSObject>
/**
 * 图片被点击的代理
 */
-(void)adViewClick;
@end
@interface XBAdvertView : UIView 

@property (nonatomic,weak) id<XBAdvertViewDelegate> delegate; 

@property (nonatomic,strong) UIImage* adimage; 

@end 

2.广告的m文件

//
// XBAdvertView.m
// scoreCount
//
// Created by 王国栋 on 15/12/22.
// Copyright  2015年 xiaobai. All rights reserved.
// 

#import "XBAdvertView.h"
#import "MacroDefinition.h"
#import "UIDeviceHardware.h" 

#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height 

#define AppViewOriginCenterY kScreenH*0.335
#define AdvertViewRatio 0.75 

#define AppViewObjCenterY (kScreenH*AdvertViewRatio+35) 

#define AppNameObjCenterY AppViewObjCenterY+30
#define AppNameOriginCenterY kScreenH+20 

#define AppImageViewW 60/0.6
#define AppImageViewH AppImageViewW 

@interface XBAdvertView() 

///**
// * 广告的图片
// */
//@property (nonatomic,strong) UIImage * advertImage;
///**
// * app图标
// */
//@property (nonatomic,strong) UIImage* appImage;
//
//@property (nonatomic,strong)UILabel * appName;
//
///**
// * 图片的URL
// */
//@property (nonatomic,strong) NSString* picURL;
//
///**
// * 代理类去处理点击的方法
// */ 

@property (nonatomic,strong) UIImageView * advertImv;
@property (nonatomic,strong) UIImageView * appImv;
@property (nonatomic,strong) UILabel * appName;
@property (nonatomic,strong) UILabel * appPinyin;
@property (nonatomic,strong) UIImage *image;
@end
@implementation XBAdvertView 

- (void)setAdimage:(UIImage *)adimage
{
 self.advertImv.image = adimage; 

 [UIView animateWithDuration:1.0 delay:0.5 options:UIViewAnimationOptionCurveEaseIn animations:^{
  UIDeviceResolution ios_Model = [UIDeviceHardware currentResolution]; //获取设备尺寸
  if (ios_Model==UIDevice_iPhoneHiRes||ios_Model==UIDevice_iPhoneStandardRes||ios_Model==UIDevice_iPhoneTallerHiRes){
   self.appImv.center = CGPointMake(self.appImv.center.x, SCREEN_HEIGHT-108+20); 

  }else{
   self.appImv.center = CGPointMake(self.appImv.center.x, SCREEN_HEIGHT-108+25); 

  }
  self.appName.center= CGPointMake(self.appName.center.x, SCREEN_HEIGHT-108+self.image.size.height/2+5+15);
  self.appImv.transform = CGAffineTransformMakeScale(0.6, 0.6);
  self.appPinyin.center = CGPointMake(self.appPinyin.center.x,SCREEN_HEIGHT-15-10);
  //self.appPinyin.frame = CGRectMake(0, CGRectGetMaxY(self.appName.frame)+5, SCREEN_WIDTH, 20);
 } completion:^(BOOL finished) { 

  //  [UIView animateWithDuration:1.0 animations:^{
  //
  //   self.advertImv.alpha=1.0f;
  //  }];
  self.advertImv.alpha=1.0f;
  [UIView animateWithDuration:3.0 animations:^{ 

   self.advertImv.alpha=1.0f; 

  } completion:^(BOOL finished) { 

   [NSThread sleepForTimeInterval:2.0]; 

   [self removeFromSuperview]; 

  }];
 }]; 

}
- (instancetype)initWithFrame:(CGRect)frame
{ 

 NSLog(@"initWithFrame");
 if (self = [super initWithFrame:frame]) { 

  //设置广告
  self.backgroundColor = [UIColor whiteColor];
  self.advertImv = [[UIImageView alloc]init];
  self.advertImv.backgroundColor = [UIColor grayColor];
  self.advertImv.contentMode=UIViewContentModeScaleToFill;
  self.advertImv.alpha = 0;//设置为透明
  [self addSubview:self.advertImv];
  //添加手势
  UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(click)];
  tap.numberOfTapsRequired=1;
  [self.advertImv addGestureRecognizer:tap]; 

  //设置app图标
  self.appImv =[[ UIImageView alloc]init];
  self.appImv.image = [UIImage imageNamed:@"iphone6p"];
  [self addSubview:self.appImv];
  //设置app 的名字
  self.appName = [[UILabel alloc]init];
  self.appName.text = @"乐校";
  self.appName.font = UIFont(18);
  self.appName.textColor = BLUE_22C4FF;
  self.appName.textAlignment=NSTextAlignmentCenter;
  [self addSubview:self.appName];
  self.appPinyin =[[UILabel alloc]init];
  self.appPinyin.textAlignment = NSTextAlignmentCenter;
  self.appPinyin.font = UIFont(13);
  self.appPinyin.textColor = BLUE_22C4FF;
  self.appPinyin.text =@"使大学生活更精彩";
  [self addSubview:self.appPinyin]; 

  //设置广告尺寸 

  UIDeviceResolution ios_Model = [UIDeviceHardware currentResolution]; //获取设备尺寸
  if (ios_Model==UIDevice_iPhoneHiRes||ios_Model==UIDevice_iPhoneStandardRes||ios_Model==UIDevice_iPhoneTallerHiRes){
   self.image = [UIImage imageNamed:@"iphone5"];
   self.appImv.frame = CGRectMake(0, 0, self.image.size.width, self.image.size.height);
  }else if (ios_Model==UIDevice_iPhone6HiRes){
   self.image = [UIImage imageNamed:@"iphone6"];
   self.appImv.frame = CGRectMake(0, 0, self.image.size.width, self.image.size.height);
  }else if (ios_Model==UIDevice_iPhone6pHiRes){
   self.image = [UIImage imageNamed:@"iphone6p"];
   self.appImv.frame = CGRectMake(0, 0, self.image.size.width, self.image.size.height);
  }
  //  self.appImv.frame = CGRectMake(0, 0, AppImageViewW, AppImageViewH);
  if (ios_Model==UIDevice_iPhoneHiRes||ios_Model==UIDevice_iPhoneStandardRes){
   self.appImv.center = CGPointMake(kScreenW/2, AppViewOriginCenterY+5);
  }else if (ios_Model==UIDevice_iPhone6HiRes){
   self.appImv.center = CGPointMake(kScreenW/2, AppViewOriginCenterY);
  }else if (ios_Model==UIDevice_iPhoneTallerHiRes||ios_Model==UIDevice_iPhone6pHiRes){
   self.appImv.center = CGPointMake(kScreenW/2, AppViewOriginCenterY);
  }
  //设置app名字的尺寸
  self.appName.frame =CGRectMake(0, 0, AppImageViewW, 30);
  self.appName.center=CGPointMake(kScreenW/2, AppNameOriginCenterY);
  //设置app拼音的尺寸
  self.appPinyin.frame =CGRectMake(0, 0, SCREEN_WIDTH, 20);
  self.appPinyin.center=CGPointMake(kScreenW/2, AppNameOriginCenterY+AppImageViewH/2);
  //设置广告尺寸
  //self.advertImv.image = adimg;
  self.advertImv.frame= CGRectMake(0, 0, kScreenW,kScreenH); 

 }
 return self;
} 

/**
 * 交给代理类处理图片点击后的按钮
 */
-(void)click
{
 if ([self.delegate respondsToSelector:@selector(adViewClick)]) { 

  [self.delegate adViewClick];
 }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
 // Drawing code
}
*/ 

@end 
[self.view setBackgroundColor:[UIColor greenColor]]; 

 XBAdvertView * ad = [[XBAdvertView alloc]initWithFrame:[UIScreen mainScreen].bounds];
 UIImage * image = [UIImage imageNamed:@"ad.jpg"];
 ad.adimage = image;
 [self.view addSubview:ad]; 

四、总结

以上就是iOS实现动态开屏广告的全部内容了,希望对大家学习或开发iOS能有所帮助,如果有疑问大家可以留言交流。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索ios
, 开屏广告实现
, ios开屏广告
动态开屏广告
动态新闻列表实现代码、android 开屏广告实现、ios 开屏广告实现、孔雀开屏动态图片、孔雀开屏动态图,以便于您获取更多的相关知识。

时间: 2024-08-25 09:27:23

iOS实现动态的开屏广告示例代码_IOS的相关文章

IOS设置按钮为圆角的示例代码_IOS

iOS中很多时候都需要用到指定风格的圆角按钮,以下是UIButton提供的创建圆角按钮方法 设置按钮的4个角:      左上:UIRectCornerTopLeft      左下:UIRectCornerBottomLeft      右上:UIRectCornerTopRight      右下:UIRectCornerBottomRight 示例代码: UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(50, 60

iOS实现爆炸的粒子效果示例代码_IOS

照例我们先看看效果图 怎么样?效果很不错吧,下面来一起看看实现的过程和代码示例. 实现原理 从图中可以大致看出,爆炸点点都是取的某坐标的颜色值,然后根据一些动画效果来完成的. 取色值 怎么取的view的某个点的颜色值呢?google一下,就可以找到很多答案.就不具体说了.创建1*1的位图,然后渲染到屏幕上,然后得到RGBA.我这里写的是UIView的extension. extension UIView { public func colorOfPoint(point:CGPoint) -> U

利用iOS绘制图片生成随机验证码示例代码_IOS

先来看看效果图 实现方法 .h文件 @property (nonatomic, retain) NSArray *changeArray; @property (nonatomic, retain) NSMutableString *changeString; @property (nonatomic, retain) UILabel *codeLabel; -(void)changeCode; @end .m文件 @synthesize changeArray = _changeArray;

iOS实现只有底部边框线的输入框示例代码_IOS

实现效果图: 实现过程 输入框一般有无边框(空白输入框),全边框(矩形输入框),加边框很简单,只需要设置UITextField的layer.borderColor属性和layer.borderWidth属性就可以了,如果要实现只带底部框线的输入框就不太好弄了,百度了一下找到了一个最笨也是挺不错的一个方法,那就是在下面直接给它加一条线就可以了. 示例代码: UITextField *passwordTextField = [[UITextField alloc] initWithFrame:CGR

iOS中 LGLAlertView 提示框的实例代码_IOS

使用与iOS8 以后,只是把系统的UIAlertController进行了封装,省的每次用的时候要写很多的代码.封装后只需要一句代码即可 , deome 地址 :https://github.com/liguoliangiOS/LGLAlertView.git 上代码LGLAlertView.h: #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> typedef NS_ENUM(NSInteger, LGLAlert

iOS实现动态元素的引导图效果_IOS

前言 最近越来越多的APP,已经抛弃掉第一次进入的3-4页的导入页面,而是另外采取了在功能页面悬浮一个动态效果来展示相应的功能点.这个模块主要是实现app首次进入时显示的动态的引导图,在用户进行右滑或者左滑的时候,屏幕上的一些元素做出相应的隐藏消失以及位置移动. 实现效果: 图片资源来自网络,侵权即删 先来看看是如何使用的,然后再介绍相关的方法及属性 NSMutableArray * elementsDataArr = [[NSMutableArray alloc] init]; /* 动画元素

静态与动态加载Dll [示例代码]

1.DLL源代码 MyDll.h [cpp] view plaincopyprint? //////////////////////////////////////////////////////////////////////////  // MyDll.h  // 声明函数  int _stdcall Add(int a,int b);  int _stdcall Sub(int a,int b);  /////////////////////////////////////////////

[Android] 通过GridView仿微信动态添加本地图片示例代码

前面文章讲述的都是"随手拍"中图像处理的操作,此篇文章主要讲述GridView控件实现添加本地图片并显示.主要是关于GridView控件的基本操作,通常可以通过自定义继承BaseAdapter的适配器加载图片,而下面讲述的不是自定义的适配器,而是调用SimpleAdapter实现的.至于上传发布与网络交互此处不讲述,后面文章会讲! 一. 实现效果 主要是通过点击+从本地相册中添加图片,同时显示图片至GridView.点击图片可以进行删除操作,同时界面中的发布EditView控件也很好看

javaScript 动态访问JSon元素示例代码_javascript技巧

复制代码 代码如下: $(document).ready(function () { var obj = {Name: 'Allen', Age: '30'}; for (var o in obj) { var a = console.log(o); // Name ,Age var a = console.log(obj[o]); //Allen,30 } }); </script>