iOS UIView Animation动画

1. UIView属性动画

常用方法animateWithDuration

  • duration,动画时间
  • delay,动画在延迟多久之后执行
  • options,动画的展示方式
  • animations,动画代码
  • completion,动画完成后代码
// 动画时间,,options,completion
+ (void)animateWithDuration:(NSTimeInterval)duration
                      delay:(NSTimeInterval)delay 
                    options:(UIViewAnimationOptions)options
                 animations:(void (^)(void))animations
                 completion:(void (^ __nullable)(BOOL finished))completion)

// delay = 0.0, options = 0, completion = NULL
+ (void)animateWithDuration:(NSTimeInterval)duration
                 animations:(void (^)(void))animations)

// delay = 0.0, options = 0
+ (void)animateWithDuration:(NSTimeInterval)duration
                 animations:(void (^)(void))animations
                 completion:(void (^ __nullable)(BOOL finished))completion)

1.1 可实现动画的属性

  • frame,实现移动的动画效果
  • bounds,实现内部子控件移动的动画效果
  • center,实现移动的动画效果
  • alpha,实现淡入淡出的动画效果
  • backgroundColor,实现背景色渐变的动画效果
  • transform,实现移动、缩放、旋转的动画效果,详见iOS CGAffineTransform仿射变换

frame属性

[UIView animateWithDuration:1 animations:^(void) {
    self.animationView.frame = CGRectMake(20, 80, 240, 240);
} completion:nil];

在这里插入图片描述

bounds属性

[UIView animateWithDuration:1 animations:^(void) {
    self.animationView.bounds = CGRectMake(30, 30, 160, 160);
} completion:nil];

在这里插入图片描述

center属性

[UIView animateWithDuration:1 animations:^(void) {
    self.animationView.center = CGPointMake(160, 220);
} completion:nil];

在这里插入图片描述
alpha属性

[UIView animateWithDuration:1 animations:^(void) {
    self.animationView.alpha = 0.2;
} completion:nil];

在这里插入图片描述
backgroundColor属性

[UIView animateWithDuration:1 animations:^(void) {
    self.animationView.backgroundColor = [UIColor blueColor];
} completion:nil];

在这里插入图片描述
transform属性

[UIView animateWithDuration:1 animations:^(void) {
    self.animationView.transform = CGAffineTransformMakeRotation(M_PI_4);
} completion:nil];

在这里插入图片描述

1.2 options动画的展示方式

optionsUIViewAnimationOptions,主要分为3种

动画效果

说明
UIViewAnimationOptionLayoutSubviews提交动画的时候布局子控件,表示子控件将和父控件一同动画。
UIViewAnimationOptionAllowUserInteraction动画时允许用户交流,比如触摸
UIViewAnimationOptionBeginFromCurrentState从当前状态开始动画
UIViewAnimationOptionRepeat动画无限重复
UIViewAnimationOptionAutoreverse执行动画回路,前提是设置动画无限重复
UIViewAnimationOptionOverrideInheritedDuration忽略外层动画嵌套的执行时间
UIViewAnimationOptionOverrideInheritedCurve忽略外层动画嵌套的时间变化曲线
UIViewAnimationOptionAllowAnimatedContent通过改变属性和重绘实现动画效果,如果key没有提交动画将使用快照
UIViewAnimationOptionShowHideTransitionViews用显隐的方式替代添加移除图层的动画效果
UIViewAnimationOptionOverrideInheritedOptions忽略嵌套继承的选项

动画运行加速

说明
UIViewAnimationOptionCurveEaseInOut由慢到快再到慢
UIViewAnimationOptionCurveEaseIn由慢到快
UIViewAnimationOptionCurveEaseOut由快到慢
UIViewAnimationOptionCurveLinear匀速

分别采用四种加速模式UIViewAnimationOptionCurveEaseInOutUIViewAnimationOptionCurveEaseInUIViewAnimationOptionCurveEaseOutUIViewAnimationOptionCurveLinear
在这里插入图片描述

转场动画

说明
UIViewAnimationOptionTransitionNone无转场动画
UIViewAnimationOptionTransitionFlipFromLeft转场从左翻转
UIViewAnimationOptionTransitionFlipFromRight转场从右翻转
UIViewAnimationOptionTransitionCurlUp上卷转场
UIViewAnimationOptionTransitionCurlDown下卷转场
UIViewAnimationOptionTransitionCrossDissolve转场交叉消失
UIViewAnimationOptionTransitionFlipFromTop转场从上翻转
UIViewAnimationOptionTransitionFlipFromBottom转场从下翻转

1.3 弹簧效果

弹簧效果的方法多了两个参数dampingRatiovelocity

+ (void)animateWithDuration:(NSTimeInterval)duration
                      delay:(NSTimeInterval)delay
     usingSpringWithDamping:(CGFloat)dampingRatio
      initialSpringVelocity:(CGFloat)velocity
                    options:(UIViewAnimationOptions)options
                 animations:(void (^)(void))animations
                 completion:(void (^ __nullable)(BOOL finished))completion;

dampingRatio参数的值为0.0f到1.0f,数值越小「弹簧」的振动效果越明显。
分别设置为0.2、0.5和1,显示如下
在这里插入图片描述
velocity参数表示初始的速度,数值越大一开始移动越快。
分别设置为5、10和20,效果如下
在这里插入图片描述

2. UIView过渡动画

当一个视图的内容需要变化时,可以使用过渡动画。

+ (void)transitionWithView:(UIView *)view
                  duration:(NSTimeInterval)duration
                   options:(UIViewAnimationOptions)options
                animations:(void (^ __nullable)(void))animations
                completion:(void (^ __nullable)(BOOL finished))completion);

view是参与转换的视图,options是UIViewAnimationOptions类型,

UIViewAnimationOptionTransitionCurlUpUIViewAnimationOptionTransitionCurlDown
在这里插入图片描述在这里插入图片描述
UIViewAnimationOptionTransitionCrossDissolve
在这里插入图片描述
UIViewAnimationOptionTransitionFlipFromLeftUIViewAnimationOptionTransitionFlipFromRight
在这里插入图片描述在这里插入图片描述
UIViewAnimationOptionTransitionFlipFromTopUIViewAnimationOptionTransitionFlipFromBottom
在这里插入图片描述在这里插入图片描述

还有一个过渡动画的方法,在动画过程中,首先将fromView从父视图中删除,然后将toView添加,就是做了一个替换操作。

+ (void)transitionFromView:(UIView *)fromView
                    toView:(UIView *)toView
                  duration:(NSTimeInterval)duration
                   options:(UIViewAnimationOptions)options
                completion:(void (^ __nullable)(BOOL finished))completion);

3. UIView关键帧动画

  • duration,总持续时间
  • options,枚举UIViewKeyframeAnimationOptions
  • frameStartTime,相对开始时间
  • frameDuration,相对持续时间
+ (void)animateKeyframesWithDuration:(NSTimeInterval)duration
                               delay:(NSTimeInterval)delay
                             options:(UIViewKeyframeAnimationOptions)options
                          animations:(void (^)(void))animations
                          completion:(void (^ __nullable)(BOOL finished))completion);
+ (void)addKeyframeWithRelativeStartTime:(double)frameStartTime
                        relativeDuration:(double)frameDuration
                              animations:(void (^)(void))animations);

options可以指定动画效果

说明
UIViewKeyframeAnimationOptionLayoutSubviews提交动画的时候布局子控件,表示子控件将和父控件一同动画。
UIViewKeyframeAnimationOptionAllowUserInteraction动画时允许用户交流,比如触摸
UIViewKeyframeAnimationOptionBeginFromCurrentState从当前状态开始动画
UIViewKeyframeAnimationOptionRepeat动画无限重复
UIViewKeyframeAnimationOptionAutoreverse执行动画回路,前提是设置动画无限重复
UIViewKeyframeAnimationOptionOverrideInheritedDuration忽略外层动画嵌套的执行时间
UIViewKeyframeAnimationOptionOverrideInheritedOptions忽略外层动画嵌套的时间变化曲线
UIViewKeyframeAnimationOptionCalculationModeLinear选择使用一个简单的线性插值计算的时候关键帧之间的值
UIViewKeyframeAnimationOptionCalculationModeDiscrete选择不插入关键帧之间的值,而是直接跳到每个新的关键帧的值
UIViewKeyframeAnimationOptionCalculationModePaced选择计算中间帧数值算法使用一个简单的节奏。这个选项的结果在一个均匀的动画
UIViewKeyframeAnimationOptionCalculationModeCubic选择计算中间帧使用默认卡特莫尔罗花键,通过关键帧的值
UIViewKeyframeAnimationOptionCalculationModeCubicPaced选择计算中间帧使用立方计划而忽略的时间属性动画

示例代码

[UIView animateKeyframesWithDuration:1 delay:0 options:0 animations:^{
    [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:0.2 animations:^{
        self.magentaView.frame = CGRectMake(120, 100, 160, 160);
    }];
    [UIView addKeyframeWithRelativeStartTime:0.2 relativeDuration:0.7 animations:^{
        self.magentaView.frame = CGRectMake(160, 240, 80, 80);
    }];
    [UIView addKeyframeWithRelativeStartTime:0.7 relativeDuration:1 animations:^{
        self.magentaView.frame = CGRectMake(20, 100, 160, 160);
    }];
} completion:nil];

显示如下
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值