上架

iOS开发:两行代码实现动态启动页

2016-08-26  本文已影响3023人  吸氧

Github:https://github.com/lxypeter/CYLaunchAnimateViewController

很多如新浪微博,闲鱼等常用App在启动后都会有一个启动页的延伸,作为广告页或介绍页使用。仿照这种模式封装了一个Controller,简便地实现这种带动画的延伸页面。

Demo

现在主要提供了如下几种动画效果提供使用。具体可参照CYLaunchAnimateViewController.h 中的枚举。


CYLaunchAnimateTypeFade CYLaunchAnimateTypeFadeAndZoomIn
CYLaunchAnimateTypePointZoomIn1 CYLaunchAnimateTypePointZoomIn2
CYLaunchAnimateTypePointZoomOut

如何使用

作为启动页的延伸,具体的调用应该在 AppDelegate.m中完成。更多具体的客制化参数可到CYLaunchAnimateViewController.h中查看。

  1. 导入CYLaunchAnimateViewController.h
  #import "CYLaunchAnimateViewController.h"
  1. 初始化延伸启动页View
  //just an example
  UIView *launchView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
  /*
  * launchView setting...
  */
  1. 初始化CYLaunchAnimateViewController,并调用show方法。(在调用show方法展示前请确保keyWindow已经设置)
CYLaunchAnimateViewController *launchController = [[CYLaunchAnimateViewController alloc]initWithContentView:launchView animateType:CYLaunchAnimateTypePointZoomOut showSkipButton:YES];
[launchController show];

注意contentView并不一定要是屏幕大小,任意大小的View都可作为延伸启动页View,CYLaunchAnimateViewController会自动将View设置到屏幕中央。


FadeWithOutBackground

实现思路

CYLaunchAnimateViewController应该提供的是任意延伸页面的展示以及消失时的动画效果,因而实现主要分为三步:

1.延伸页面的设置

为适配任意大小的延伸页,在加载页面时将控制器viewbackgroundColor设为透明色并将自定义的延伸页作为subView设于屏幕正中。

- (void)configureSubViews{
    NSAssert(_contentView, @"contentView must not be nil!");
    self.view.backgroundColor = [UIColor clearColor];

    _contentView.center = CGPointMake(self.view.center.x, self.view.center.y);
    [self.view addSubview:_contentView];
    
    if(self.showSkipButton){
        self.skipButton.remainSec = _waitDuration;
        [self.view addSubview:self.skipButton];
        [self.skipButton addTarget:self action:@selector(dismissAtOnce) forControlEvents:UIControlEventTouchUpInside];
    }
}

2.延伸页面的展示

作为启动页的延伸,延伸页面需要紧跟静态启动页并在在根控制器视图上层(淡出动画所需)。因为要保证延伸页置于根控制器上层,网上有另一种实现方法是将延伸页的设置和控制的代码置于根控制器的生命周期之中,虽然可以实现相同的效果,但耦合性较强并不方便组件化,所以我采用了另一种方式——将延伸页面直接添加到程序已经初始化的keyWindow之上。

- (void)show{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    NSAssert(window,@"keyWindow must be init!");
    [window addSubview:self.view];
}

当页面加载完成,初始化计时器NSTimer,开始倒计时。有限的支持有小数位的等待时间,每0.1秒触发一次- (void)countDown方法,将误差控制在0.1秒以内。

- (void)configureTimer{
    self.timePass = 0.0;
    NSTimer *timer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];
    self.timer = timer;
    [timer fire];
}

刷新skipButton上的倒计时(如有),判断倒计时是否结束。结束时停止计时器,开始结束动画。

- (void)countDown{
    _timePass+=0.1;
    _skipButton.remainSec = _waitDuration-_timePass<0?0:_waitDuration-_timePass;
    if(_waitDuration<=_timePass){
        [self.timer invalidate];
        self.timer = nil;
        [self dismiss];
    }
}

3.结束动画效果的实现

结束动画就是CYLaunchAnimateViewController的view的消失过程。以核心动画和CAShapeLayer为基础,CYLaunchAnimateViewController自身作为动画的代理,监控动画的周期。以CYLaunchAnimateTypeFadeCYLaunchAnimateTypePointZoomIn1两种动画效果为例。

- (void)dismiss{
    
    switch (self.animateType){
        case CYLaunchAnimateTypeFade:{
            CABasicAnimation *animation = [CABasicAnimation animation];
            animation.delegate = self;
            [animation setDuration:_animateDuration];
            animation.keyPath = @"opacity";
            animation.toValue = @(0);
            [self.view.layer addAnimation:animation forKey:nil];
            break;
        }
        //...
        case CYLaunchAnimateTypePointZoomIn1:{
            CAShapeLayer *maskLayer = [CAShapeLayer layer];
            self.view.layer.mask = maskLayer;
            
            CGSize screenSize = [UIScreen mainScreen].bounds.size;
            
            CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"path"];
            [keyFrameAnimation setDuration:_animateDuration];
            keyFrameAnimation.delegate = self;
            
            UIBezierPath *pathOne = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.view.center.x, self.view.center.y) radius:hypot(screenSize.height, screenSize.width)/2 startAngle:0 endAngle:2 * M_PI clockwise:NO];
            
            UIBezierPath *pathTwo = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.view.center.x, self.view.center.y) radius:screenSize.width/2*0.5 startAngle:0 endAngle:2 * M_PI clockwise:NO];
            
            UIBezierPath *pathThree = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.view.center.x, self.view.center.y) radius:screenSize.width/2*0.7 startAngle:0 endAngle:2 * M_PI clockwise:NO];
            
            UIBezierPath *pathFour = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.view.center.x, self.view.center.y) radius:1 startAngle:0 endAngle:2 * M_PI clockwise:NO];
            
            keyFrameAnimation.values = @[(__bridge id)(pathOne.CGPath),(__bridge id)(pathTwo.CGPath),(__bridge id)(pathThree.CGPath),(__bridge id)(pathFour.CGPath)];
            keyFrameAnimation.keyTimes = @[@(0),@(0.5),@(0.9),@(1)];
            
            [maskLayer addAnimation:keyFrameAnimation forKey:nil];
            
            break;
        }
        //...
        default:
            break;
    }
}

当动画结束,CYLaunchAnimateViewController的view从keyWindow移除,执行completeblock中内容(如有)。

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    [self dismissAtOnce];
}

- (void)dismissAtOnce{
    [self.timer invalidate];
    self.timer = nil;
    [self.view removeFromSuperview];
    if (_complete) {
        self.complete();
    }
}
上一篇下一篇

猜你喜欢

热点阅读