启动广告模块-掘金启动广告实现记录

Posted by DragonLi on July 29, 2017

客户端的启动广告模块

目前掘金的广告模块 iOS 移动端实现

  • 按照服务器返回几个时间节点确定是否显示
  • 一次请求二份广告数据,预先缓存下一次的相关数据
  • 从启动到广告出现,中间不会空白

没必要写 demo,首先一点,我是采取一个控制器实现的,并没有使用 view 添加到 window(网上通常做法),因为掘金的加上 view, 会出现空白,比较明显(持续0.5-1.2秒不等)

  • 展位图,直接使用启动图的图片,这样即使图片下载等待中用户看到也是启动的图,因为是下部分需要展示掘金的 LOGO, 所有广告的尺寸也需要这样处理,广告图片未下载完毕,全屏的启动图展示阶段,下载完毕,改变尺寸
 if (imgUrl) {
    XTWeakSelf;
    [self.aDImgView sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:self.imageLoading options:SDWebImageHighPriority  completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    [weakSelf.aDImgView setImage:[weakSelf imageCompressForWidth:image targetWidth:mainWidth]];
    weakSelf.aDImgView.frame = CGRectMake(0, 0, mainWidth, mainHeight *0.8);
        }];
    }

  • 关闭的消失动画相关代码
CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
opacityAnimation.duration = .3;
opacityAnimation.fromValue = [NSNumber numberWithFloat:0.8];
opacityAnimation.toValue = [NSNumber numberWithFloat:0.3];
opacityAnimation.removedOnCompletion = YES;
opacityAnimation.fillMode = kCAFillModeForwards;
[self.aDImgView.layer addAnimation:opacityAnimation forKey:@"animateOpacity"];

  • 跳过的圆圈转动实现
//设置圆圈
- (void)setAnimationSkipWithAttribute:(UIColor *)strokeColor lineWidth:(NSInteger)lineWidth {
    self.shapelayer = [CAShapeLayer layer];
    UIBezierPath *BezierPath = [UIBezierPath bezierPathWithOvalInRect:self.skipBtn.bounds];
    self.shapelayer.lineWidth = lineWidth;
    self.shapelayer.strokeColor = [strokeColor?strokeColor:[UIColor whiteColor] CGColor];
    self.shapelayer.fillColor = [UIColor clearColor].CGColor;
    self.shapelayer.path = BezierPath.CGPath;
    [self.skipBtn.layer addSublayer:self.shapelayer];
}


-(void)startAnimation{
    CABasicAnimation *pathAnimaton = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
    pathAnimaton.duration = _adTime -0.6;
    pathAnimaton.fromValue = @(0.0f);
    pathAnimaton.toValue = @(1.0f);
    [self.shapelayer addAnimation:pathAnimaton forKey:nil];
}

  • 广告嵌入到程序入口
- (void)launchAdSetting{
    if (self.window.rootViewController.view) {
        self.rootVC = self.window.rootViewController;
        self.imgAdView = [[LBLaunchImageAdView alloc]init];
        self.window.rootViewController = self.imgAdView;
    }

}


 XTWeakSelf;
    LaunchAdDataController *loadingAd =[LaunchAdDataController new];
    [loadingAd queryloadingAdsuccess:^(ResultInfo * loadingAdModel) {
        if (loadingAdModel ) {
    [ self.imgAdVC addLBlaunchImageAdViewWithImgUrl: loadingAdModel.screenshotUrl];
 self.imgAdView.clickBlock = ^(clickType type){
};
}elsel{
      [weakSelf changeRootVC];

}

/**
 *
 此处 VC 已经是之前的主界面VC, 可以跳转推送
 *
 */
- (void)changeRootVC{
    self.imgAdVC = nil;
    self.window.rootViewController = self.rootVC;
}



  • 但是,启动如果有广告,改变了 程序的rootVC,收到推送,处理跳转,会有冲突…,后来直接忽视广告,进入广告就行了,可能处理方式不太完美,但是(用户点击推送,到接入界面,实际测试本身就慢,从启动app, 到用户看到希望打开的界面,你再让他看看广告,估计这样更不好吧)
// 点击通知进入应用
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void (^)())completionHandler {
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        //处理远程推送内容
        [self changeRootVC];
        [self notification:userInfo sender:@"程序未打开"];
        [MiPushSDK handleReceiveRemoteNotification:userInfo];
    }
    completionHandler(UNNotificationPresentationOptionBadge + UNNotificationPresentationOptionSound);
}