iOS 启动页加入动态的广告实现

做产品的时候有的时候需要在程序启动的时候加入广告,不难。只要在appdelegate.m 的启动加载完成方法加入需要的广告视图就可以了,但是有的时候需要让启动图看起来就是一个广告,只有一个界面。而且还要这个广告里面会动,iOS的启动图只能是静态的,而且固定,为了实现看起来的动画效果,只能进行伪造了。



实现思路:


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



广告的头文件

//  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
广告的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];





  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值