iOS开发-启动页广告实现

iOS开发-启动页广告实现

启动页广告实现是一个非常常见的广告展示模式。 就是在启动时候显示广告,之后点击跳转到广告页面或者其他APP。

一、实现启动页广告

启动页广告控件实现,将View放置在keyWindow上,显示广告图片,点击广告图片进行跳转。

如果没有操作,倒计时5s后进行App

具体代码如下

SDFullScreenADView.h

objectivec 复制代码
#import <UIKit/UIKit.h>

#define kFullScreenAdTag  9012

//跳转按钮样式
typedef NS_ENUM(NSUInteger, SDSkipButtonType) {
    SDSkipButtonTypeText = 0,
};

typedef void(^SDFullScreenADBlock)();

@interface SDFullScreenADView : UIView

/**
 广告图的显示时间(默认5秒)
 */
@property (nonatomic, assign) NSUInteger duration;

/**
 右上角按钮的样式(默认倒计时+跳过)
 */
@property (nonatomic, assign) SDSkipButtonType skipType;

/**
 广告图
 */
@property (nonatomic, strong) UIImage *adImage;

/**
 广告数据,跳转数据
 */
@property (nonatomic, strong) id data;


@property (nonatomic, copy) SDFullScreenADBlock block;

/**
 显示广告
 */
- (void)show;


/**
 默认广告
 
 @return id
 */
+ (SDFullScreenADView *)defaultFullScreenADView;

@end

SDFullScreenADView.m

objectivec 复制代码
#import "SDFullScreenADView.h"

#define kMainScreenWidth [UIScreen mainScreen].bounds.size.width

@interface SDFullScreenADView ()

@property (nonatomic, strong) UIImageView *adImageView;   //广告界面
@property (nonatomic, strong) UIButton *skipButton;       //跳过按钮
@property (nonatomic, strong) UILabel *timeLabel;         //倒计时控件

@property (nonatomic, strong) NSTimer *displayTimer;         //剩余时间倒计时

@end

@implementation SDFullScreenADView

- (instancetype)init {
    self = [super init];
    if (self) {
        [self configDefaultParameter];
        [self setupSubView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(adTapAction)];
        [self addGestureRecognizer:tap];
        
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        if (!window) {
            return nil;
        }
        [window addSubview:self];
    }
    return self;
}

#pragma mark - Private Method -
/**
 配置默认参数
 */
- (void)configDefaultParameter {
    self.duration = 5;
    self.skipType = SDSkipButtonTypeText;
    self.frame = [[UIScreen mainScreen] bounds];
}

/**
 设置控件
 */
- (void)setupSubView {
    [self addSubview:self.adImageView];
    self.adImageView.frame = self.bounds;
    
    [self addSubview:self.skipButton];
    self.skipButton.frame = CGRectMake(kMainScreenWidth - 80, 30, 70, 30);
    
    [self addSubview:self.timeLabel];
    self.timeLabel.frame = self.skipButton.frame;
}

- (void)setDuration:(NSUInteger)duration {
    _duration = duration;
}

- (void)setAdImage:(UIImage *)adImage {
    _adImage = adImage;
}

- (void)adTapAction {
    [self.displayTimer invalidate];
    self.displayTimer = nil;
    [self dismiss];
    
    if (self.block) {
        self.block();
    }
}

- (void)skipButtonAction {
    [self dismiss];
}

- (void)displayTimerAciton {
    self.duration--;
    if (self.duration == 0) {
        [self dismiss];
        return;
    }
    
    if (self.duration > 0) {
        _timeLabel.text = [NSString stringWithFormat:@"%lus跳过广告",(unsigned long)self.duration];
    }
}

/**
 显示广告
 */
- (void)show {
    if (!self.adImage) {
        [self removeFromSuperview];
        return;
    }
    
    [self addDisplayTimer];
    
    self.adImageView.image = self.adImage;
    
    self.alpha = 0.0;
    [UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.alpha = 1.0;
    } completion:^(BOOL finished) {
    }];
}

/**
 消失广告图
 */
- (void)dismiss {
    [UIView animateWithDuration:0.5 delay:0.3 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.transform = CGAffineTransformMakeScale(1.2, 1.2);
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - SETTER/GETTER
- (UIButton *)skipButton {
    if (!_skipButton) {
        _skipButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _skipButton.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
        _skipButton.layer.cornerRadius = 4;
        _skipButton.layer.masksToBounds = YES;
        _skipButton.titleLabel.font = [INSysFont inSysFontOfSize:14];
        [_skipButton addTarget:self action:@selector(skipButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _skipButton;
}

- (UIImageView *)adImageView {
    if (!_adImageView) {
        _adImageView = [[UIImageView alloc] initWithFrame:self.bounds];
        _adImageView.backgroundColor = [UIColor clearColor];
        _adImageView.clipsToBounds = YES;
        _adImageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _adImageView;
}

- (UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _timeLabel.backgroundColor = [UIColor clearColor];
        _timeLabel.textAlignment = NSTextAlignmentCenter;
        _timeLabel.font = [UIFont fontWithName:@"Heiti SC" size:12];
        _timeLabel.textColor = [UIColor whiteColor];
        _timeLabel.text = @"跳过广告";
    }
    return _timeLabel;
}

- (void)addDisplayTimer {
    if (self.displayTimer) {
        [self.displayTimer invalidate];
        self.displayTimer = nil;
    }
    self.displayTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(displayTimerAciton) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.displayTimer forMode:NSRunLoopCommonModes];
}

#pragma mark - DEALLOC
- (void)dealloc {
    [self.displayTimer invalidate];
    self.displayTimer = nil;
}

/**
 默认广告
 
 @return id
 */
+ (SDFullScreenADView *)defaultFullScreenADView {
    SDFullScreenADView *fullScreenAdView = [[SDFullScreenADView alloc] init];
    fullScreenAdView.duration = 3;
    fullScreenAdView.tag = kFullScreenAdTag;
    fullScreenAdView.alpha = 0.0;
    return fullScreenAdView;
}

@end

二、小结

iOS开发-启动页广告实现

启动页广告实现是一个非常常见的广告展示模式。 就是在启动时候显示广告,之后点击跳转到广告页面或者其他APP。

学习记录,每天不停进步。

相关推荐
mascon13 分钟前
U3D打包IOS的自我总结
ios
名字不要太长 像我这样就好18 分钟前
【iOS】继承链
macos·ios·cocoa
karshey1 小时前
【IOS webview】IOS13不支持svelte 样式嵌套
ios
潜龙95271 小时前
第4.3节 iOS App生成追溯关系
macos·ios·cocoa
游戏开发爱好者810 小时前
iOS App 电池消耗管理与优化 提升用户体验的完整指南
android·ios·小程序·https·uni-app·iphone·webview
玄辰星君11 小时前
【MAC】nacos 2.5.1容器docker安装
macos·docker·nacos
atwdy15 小时前
MacOS安装linux虚拟机
linux·运维·ubuntu·macos·utm
echola_mendes16 小时前
Dify:在MacOS系统下Dify的本地部署与使用
macos
Tim风声(网络工程师)16 小时前
如何通过mac的前24bit,模糊确认是那一台什么样的设备
运维·服务器·网络·macos
神策技术社区17 小时前
iOS 全埋点点击事件采集白皮书
大数据·ios·app