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。

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

相关推荐
自学AI的鲨鱼儿3 小时前
mac npm 安装 codex 报错 npm ENOTEMPTY
macos·npm·codex
Digitally6 小时前
如何将真我(realme)手机数据传输至 iPhone
ios·智能手机·iphone
Sephiroth.Ma10 小时前
Mac 提示“Docker 已损坏,无法打开”?我这样排查后 10 分钟修好
macos·docker·容器
量子炒饭大师10 小时前
【OpenClaw修炼宝典】—— 【macOS安装篇】想玩《爪子船长》复刻版却卡在安装?OpenClaw 从零环境搭建与编译全攻略 (小白避坑指南)
macos·openclaw·小龙虾·龙虾
JFSJHFZJ10 小时前
解密iPhone核心技术,读懂苹果的硬实力
ios·cocoa·iphone
不才小强11 小时前
macOS 屏幕录制开发完全指南:ScreenCaptureKit与音频采集实战
macos·音视频
JXSJHF12 小时前
iPhone隐藏功能大盘点,免费好用不占内存
ios·iphone
ShiLuoHeroKing21 小时前
Mole:面向专业用户的Mac系统清理开源方案
macos
ZZH_AI项目交付1 天前
为什么很多复杂跳转,最后都得先回首页?
flutter·ios
vx-bot5556661 天前
企业微信ipad协议在客户画像构建中的应用实践
ios·企业微信·ipad