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。

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

相关推荐
AIsoft_86881 小时前
iPhone录音重点标记:会议录音与重点内容同步保存功能实测
ios·iphone
00后程序员张1 小时前
使用Instruments工具深入分析iOS应用性能与启动时间优化
android·macos·ios·小程序·uni-app·cocoa·iphone
2601_960906722 小时前
AI研发加速中式及泛亚洲
人工智能·vscode·macos·sublime text·phpstorm
sg_knight2 小时前
Codex CLI 安装全攻略:macOS / Linux / Windows(WSL2)三端实战
linux·windows·macos·openai·ai编程·coding·codex
90后的晨仔3 小时前
Xcode 26.6编译 AFNetworking 报错全解:5 种方案从应急到根治
ios
初级代码游戏17 小时前
iOS开发 Swift 速记1:变量和基本数据类型
开发语言·ios·swift
for_ever_love__18 小时前
iOS:网络请求再学习
网络·学习·ios·objective-c
2501_915921431 天前
SwiftUI开发框架入门指南:从基础到实战
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
2501_916007471 天前
Python实现HTTPS爬虫的完整指南:使用requests、BeautifulSoup、Selenium和Scrapy
爬虫·python·ios·小程序·https·uni-app·iphone
LDyun_1 天前
云手机低延迟怎么实现的?为什么云手机能够 7×24 小时在线?
ios·智能手机·安卓·玩游戏