iOS基本功

UIViewAnimationWithBlocks

2016-12-29  本文已影响32人  Z了个Y

本文主要介绍下UIView基于block形式的基本动画

一、基本动画

[UIView animateWithDuration:0.25 // 动画的持续时间  
                      delay:0 // 动画执行的延迟时间 
                    options:0 // 执行的动画选项, 
                 animations:^{ // 要执行的动画代码 } 
                 completion:^(BOOL finished) { // 动画执行完毕后的调用}
];

缩略版

[UIView animateWithDuration:0.25 // 动画的持续时间  
                 animations:^{ // 要执行的动画代码 } 
                 completion:^(BOOL finished) { // 动画执行完毕后的调用}
];

精简版

[UIView animateWithDuration:0.25 // 动画的持续时间  
                 animations:^{ // 要执行的动画代码 } 
];

带弹簧效果

//弹簧效果
- (void)animationWithSpring
{
    /**
     * Damping:阻尼系数 值越小弹簧效果越明显 取值0到1
     * Velocity:初始的速度,数值越大一开始移动越快
     */
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:20 options:0 animations:^{
        _redView.zy_centerY += 150;
    } completion:nil];
}

二、关键帧动画

让一个view绕矩形运动

//关键帧动画
- (void)keyFrameAnimation
{
    /**
     *  RelativeStartTime:相对于Duration(4秒)所开始的时间(第0秒开始动画)
     *  relativeDuration:相对于Duration(4秒)所持续的时间
     */
    [UIView animateKeyframesWithDuration:4 delay:0 options:0 animations:^{
        [UIView addKeyframeWithRelativeStartTime:0.0                   relativeDuration:1/4.0 animations:^{
            //第0秒开始动画 持续1秒
            _redView.zy_centerX += 100;
        }];
        [UIView addKeyframeWithRelativeStartTime:1/4.0 relativeDuration:1/4.0 animations:^{
            //第1秒开始动画 持续1秒
            _redView.zy_centerY += 100;
        }];
        [UIView addKeyframeWithRelativeStartTime:2/4.0 relativeDuration:1/4.0 animations:^{
            //第2秒开始动画 持续1秒
            _redView.zy_centerX -= 100;
        }];
        [UIView addKeyframeWithRelativeStartTime:3/4.0 relativeDuration:1/4.0 animations:^{
            //第3秒开始动画 持续1秒
            _redView.zy_centerY -= 100;
        }];
    } completion:nil];
}

三、转场动画

- (void)transitionAnimation
{
    [UIView transitionWithView:_redView duration:1.0 options:UIViewAnimationOptionTransitionCurlUp animations:^{
        _redView.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];
    } completion:nil];
}
上一篇 下一篇

猜你喜欢

热点阅读