游戏陪玩源码设置开屏广告/引导页功能实现

发布来源:云豹科技
发布人:云豹科技
2021-10-08 10:05:59

游戏陪玩源码设置APP开屏引导页,通常适用于引导客户关注、公布重大消息,或是当广告位出租变现的作用,引导页一般有两种展现形式——图片或短视频,点击即可进入跳转链接,此功能具体实现代码如下:

 1. 游戏陪玩源码,加载启动页

 

    self.window.rootViewController =  [[UINavigationController alloc] initWithRootViewController:[[GuideViewController alloc] init]];
    
   [self.window makeKeyAndVisible];


在用户进入启动页时,首先加载程序启动图作为背景,在整个view上添加手势,作为点击事件响应


  self.navigationController.navigationBar.hidden = YES;
    _launchImgaeV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _window_width, _window_height)];
    _launchImgaeV.image = [[YBToolClass sharedInstance] getLaunchImage];
    [self.view addSubview:_launchImgaeV];
 
    [self requestData];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(goWeb)];
[self.view addGestureRecognizer:tap];


 2. 获取展示引导页面的时间,展示视频图片详情,如果不显示引导页直接跳过


    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    NSString *requestUrl = [purl stringByAppendingFormat:@"/?service=Guide.GetGuide"];
    session.requestSerializer.timeoutInterval = 5.0;
    
    
    [session POST:requestUrl parameters:nil headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSNumber *number = [responseObject valueForKey:@"ret"] ;
        if([number isEqualToNumber:[NSNumber numberWithInt:200]])
        {
            NSArray *data = [responseObject valueForKey:@"data"];
            int code = [minstr([data valueForKey:@"code"]) intValue];
            id info = [data valueForKey:@"info"];
            if (code == 0) {
                curIndex = 0;
                NSDictionary *infoDIc = [info firstObject];
                if ([minstr([infoDIc valueForKey:@"switch"]) isEqual:@"1"]) {
                    _listArray = [infoDIc valueForKey:@"list"];
                    if ([minstr([infoDIc valueForKey:@"type"]) isEqual:@"0"]) {
                        _showTime = [minstr([infoDIc valueForKey:@"time"]) intValue];
                        _allTime = _showTime * (int)_listArray.count;
                        [self showPic];
                    }else{
                        if (_listArray.count > 0) {
                            _videoUrl = [NSURL URLWithString:minstr([_listArray[0] valueForKey:@"thumb"])];
                            [self showVideo];
                        }else{
                            [self jumpBtnClick];
                        }
                    }
                }else{
                    [self jumpBtnClick];
                }
            }else{
                [self jumpBtnClick];
            }
        }else{
            [self jumpBtnClick];
        }
 
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self jumpBtnClick];
 
    }];


3. 在游戏系统源码内创建引导页图片类型数据详情


        _circleBtn = [UIButton buttonWithType:0];
        _circleBtn.frame = CGRectMake(_window_width-50, 40+statusbarHeight, 40, 40);
        [_circleBtn setTitle:YZMsg(@"跳过") forState:0];
        _circleBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        _circleBtn.layer.cornerRadius = 20;
        _circleBtn.layer.masksToBounds = YES;
        [_circleBtn addTarget:self action:@selector(jumpBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [_circleBtn setBackgroundColor:RGB_COLOR(@"#000000", 0.5)];
        [self.view addSubview:_circleBtn];
        float centerX = _circleBtn.width/2.0;
        float centerY = _circleBtn.height/2.0;
        //半径
        float radius = (_circleBtn.width-3)/2.0;
    
        //创建贝塞尔路径
        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(centerX, centerY) radius:radius startAngle:(-0.5f*M_PI) endAngle:1.5f*M_PI clockwise:YES];
    
        //添加背景圆环
    
        CAShapeLayer *backLayer = [CAShapeLayer layer];
        backLayer.frame = _circleBtn.bounds;
        backLayer.fillColor =  [[UIColor clearColor] CGColor];
        backLayer.strokeColor  = [UIColor whiteColor].CGColor;
        backLayer.lineWidth = 3;
        backLayer.path = [path CGPath];
        backLayer.strokeEnd = 1;
        [_circleBtn.layer addSublayer:backLayer];
    
        //创建进度layer
        _progressLayer = [CAShapeLayer layer];
        _progressLayer.frame = _circleBtn.bounds;
        _progressLayer.fillColor =  [[UIColor clearColor] CGColor];
        //指定path的渲染颜色
        _progressLayer.strokeColor  = [[UIColor blackColor] CGColor];
        _progressLayer.lineCap = kCALineCapRound;
        _progressLayer.lineWidth = 3;
        _progressLayer.path = [path CGPath];
        _progressLayer.strokeEnd = 0;
    
        //设置渐变颜色
        CAGradientLayer *gradientLayer =  [CAGradientLayer layer];
        gradientLayer.frame = _circleBtn.bounds;
        [gradientLayer setColors:[NSArray arrayWithObjects:(id)[RGB_COLOR(@"#ff7200", 1) CGColor],(id)[RGB_COLOR(@"#ff7200", 1) CGColor], nil]];//normalColors
        gradientLayer.startPoint = CGPointMake(1, 1);
        gradientLayer.endPoint = CGPointMake(0, 0);
        [gradientLayer setMask:_progressLayer]; //用progressLayer来截取渐变层
        [_circleBtn.layer addSublayer:gradientLayer];
        _progressTimer = [NSTimer scheduledTimerWithTimeInterval:_countTime target:self selector:@selector(progresTimeDaoJiShi) userInfo:nil repeats:YES];
    _launchImgaeV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _window_width, _window_height)];
    _launchImgaeV.image = [[YBToolClass sharedInstance] getLaunchImage];
    [self.view addSubview:_launchImgaeV];


4. 在游戏陪玩源码内创建引导页视频数据详情


- (void)showVideo{
    _launchImgaeV.hidden = YES;
    NSMutableDictionary * headers = [NSMutableDictionary dictionary];
    [headers setObject:h5url forKey:@"referer"];
    AVURLAsset *urlAsset =  [AVURLAsset URLAssetWithURL:_videoUrl options:@{@"AVURLAssetHTTPHeaderFieldsKey" : headers}];
    AVPlayerItem * songItem =   [AVPlayerItem playerItemWithAsset:urlAsset];
    AVPlayer *player = [[AVPlayer alloc]initWithPlayerItem:songItem];
    
//    AVPlayer *player = [AVPlayer playerWithURL:_videoUrl];
    player.volume = 1.0;
    self.videoPlayer = player;
    _videoLayer = [AVPlayerLayer playerLayerWithPlayer:self.videoPlayer];
    _videoLayer.videoGravity = AVLayerVideoGravityResizeAspect;
    _videoLayer.position = CGPointMake(_window_width/2, _window_height/2);
    _videoLayer.bounds = self.view.bounds;
    
    //Layer只能添加到Layer上面
    [self.view.layer addSublayer:_videoLayer];
    
    AVPlayerItem *playerItem = self.videoPlayer.currentItem;
    
    // 给AVPlayer添加观察者 必须实现 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
    
    //监控状态属性(AVPlayer也有一个status属性,通过监控它的status也可以获得播放状态)
    [playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    
    __weak typeof(self)WeakSelf = self;
 
    //播放进度观察者  //设置每0.1秒执行一次
    _playerTimeObserver =  [self.videoPlayer addPeriodicTimeObserverForInterval:CMTimeMake(1, NSEC_PER_SEC) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
        //进度 当前时间/总时间
        CGFloat progress = CMTimeGetSeconds(WeakSelf.videoPlayer.currentItem.currentTime) / CMTimeGetSeconds(WeakSelf.videoPlayer.currentItem.duration);
        if (progress > 0.0f) {
            [WeakSelf creatJumpBtn];
        }
    }];
    [self.videoPlayer play];
    
    _launchImgaeV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _window_width, _window_height)];
    _launchImgaeV.image = [[YBToolClass sharedInstance] getLaunchImage];
    [self.view addSubview:_launchImgaeV];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.videoPlayer.currentItem];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}
 
- (void)progresTimeDaoJiShi{
    _countTime += 0.1;
    _progressLayer.strokeEnd = _countTime/_allTime;
    [_progressLayer removeAllAnimations];
    int aaaa = _countTime *10;
    if (aaaa % (_showTime * 10) == 0) {
        int index = aaaa / (_showTime * 10);
        if (index < _imgaeArray.count) {
            curIndex = index;
            UIImageView *imgaeV = _imgaeArray[index-1];
            UIImageView *nextImgaeV = _imgaeArray[index];
            imgaeV.hidden = YES;
            nextImgaeV.hidden = NO;
        }
    }
    if (_countTime >= _allTime) {
        [self jumpBtnClick];
    }
}


5. 当点击跳过时,游戏陪玩源码自动响应,执行进入主页或者登录页面的代码,至此是整个登录页显示过程


    [self stop];
    UIApplication *app =[UIApplication sharedApplication];
    AppDelegate *app2 = (AppDelegate *)app.delegate;
    UINavigationController *nav;
    if ([Config getOwnID]) {
        nav = [[UINavigationController alloc]initWithRootViewController:[[ZYTabBarController alloc]init]];
    }else{
        nav = [[UINavigationController alloc]initWithRootViewController:[[PhoneLoginVC alloc]init]];
    }
    app2.window.rootViewController = nav;


以上就是《游戏陪玩源码设置开屏广告/引导页功能实现》的全部内容,在游戏陪玩源码中,引导页功能还是很有用的,毕竟作为游戏市场的周边行业,游戏陪玩平台可以为电竞游戏、游戏周边产品导流,实现其流量平台的广告优势之处。

声明:以上内容为云豹科技作者本人原创,未经作者本人同意,禁止转载,否则将追究相关法律责任www.yunbaokj.com

声明:
以上内容为云豹科技作者本人原创,未经作者本人同意,禁止转载,否则将追究相关法律责任
立即查看