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。

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

相关推荐
HerayChen1 小时前
HbuildderX运行到手机或模拟器的Android App基座识别不到设备 mac
android·macos·智能手机
hairenjing11231 小时前
在 Android 手机上从SD 卡恢复数据的 6 个有效应用程序
android·人工智能·windows·macos·智能手机
DisonTangor3 小时前
苹果发布iOS 18.2首个公测版:Siri接入ChatGPT、iPhone 16拍照按钮有用了
ios·chatgpt·iphone
- 羊羊不超越 -3 小时前
App渠道来源追踪方案全面分析(iOS/Android/鸿蒙)
android·ios·harmonyos
小李飞刀李寻欢3 小时前
Mac电脑如何解压rar压缩包
macos·rar·解压
Java小白笔记3 小时前
Mac中禁用系统更新
macos
AndyFrank3 小时前
mac crontab 不能使用问题简记
linux·运维·macos
Mac新人3 小时前
一招解决Mac没有剪切板历史记录的问题
macos·mac
王拴柱3 小时前
Mac保护电池健康,延长电池使用寿命的好方法
macos·mac
daa203 小时前
macos中安装和设置ninja
macos