iOS动画——UIKit动画
iOS动画
iOS有很多动画技术,API主要分布在两个库中,一个是UIKit,另一个是CoreAnimation,先对UIKit动画做一下总结。
UIKit动画
在UIKit中,很多API都可以看到animated参数,表示是否动画显示,其实这是UIKit封装CoreAnimation后的结果。
比如大家肯定都写过模态视图和导航控制器,他们在视图展示的时候都会有一个animated参数。
[self.navigationController pushViewController:vc animated:YES];
[self presentViewController:vc animated:YES completion:nil];
这些动画除了系统提供,其实我们是可以自己定制的,这里先简单提一下,下面在详细说。
主要API
UIKit主要API散落在UIView+UIViewAnimationWithBlocks和UIView+UIViewKeyframeAnimations两个分类
@interface UIView(UIViewAnimationWithBlocks) + (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations + (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion + (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion + (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion + (void)performSystemAnimation:(UISystemAnimation)animation onViews:(NSArray *)views options:(UIViewAnimationOptions)options animations:(void (^)(void))parallelAnimations completion:(void (^)(BOOL finished))completion @end @interface UIView (UIViewKeyframeAnimations) + (void)animateKeyframesWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewKeyframeAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
+ (void)addKeyframeWithRelativeStartTime:(double)frameStartTime relativeDuration:(double)frameDuration animations:(void (^)(void))animations NS_AVAILABLE_IOS(7_0); @end
我们每个API挨着看一下
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
这个是动画的一个常用API,用于创建常用的动画,参数列表如下
| 参数 | 说明 |
| duration | 持续时间 |
| delay | 延时时间开始 |
| options | 动画选项(下面详细解释) |
| animations | 动画block |
| completion | 动画结束后的回调 |
下面我们看一个动画例子:
[UIView animateWithDuration:0.5 delay:1.0 options:UIViewAnimationOptionAutoreverse animations:^{
CGRect frame = testView.frame;
frame.origin.y += ;
testView.frame = frame;
} completion:^(BOOL finished) {
btn.hidden = YES;
}];
这是在按钮click事件里面的一段代码,btn就是该按钮,testView定义如下
testView = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
testView.backgroundColor = [UIColor blackColor];
[self.view addSubview:testView];
大家运行代码会发现,1秒后动画开始,运行了0.5秒,视图先向下再向上运动100回到原点,然后突然跳到100的位置按钮消失。
回到原点的原因是我们options写了UIViewAnimationOptionAutoreverse动画选项,该项会在东环正向运行后自动翻转动画运行一遍。
大家应该能开出来options可以设置动画的一些行为
options大体上可以分为三类,分为动画属性,动画线性关系,和动画转场效果。
动画属性:
| UIViewAnimationOptionLayoutSubviews | 在AutoLayout下,如果修改AutoLayout,那么子视图也会跟着一起变化 |
| UIViewAnimationOptionAllowUserInteraction | 在动画时,允许用户交互,比如按钮在运动者还可以点击 |
| UIViewAnimationOptionBeginFromCurrentState | 从当前状态开始动画 |
| UIViewAnimationOptionRepeat | 重复动画 |
| UIViewAnimationOptionAutoreverse | 动画执行完毕自动翻转 |
| UIViewAnimationOptionOverrideInheritedDuration | 忽略外层动画嵌套的执行时间 |
| UIViewAnimationOptionOverrideInheritedCurve | 忽略外层动画线性关系 |
动画线性关系
| UIViewAnimationOptionShowHideTransitionViews | 用显隐的方式替代添加移除图层的动画效果 |
| UIViewAnimationOptionOverrideInheritedOptions | 忽略嵌套继承的选项 |
| UIViewAnimationOptionCurveEaseInOut | 时间曲线函数,由慢到快 |
| UIViewAnimationOptionCurveEaseIn | 时间曲线函数,由慢到特别快 |
| UIViewAnimationOptionCurveEaseOut | 时间曲线函数,由快到慢 |
| UIViewAnimationOptionCurveLinear | 时间曲线函数,匀速 |
动画转场效果
| UIViewAnimationOptionTransitionNone | 无转场动画 |
| UIViewAnimationOptionTransitionFlipFromLeft | 转场从左翻转 |
| UIViewAnimationOptionTransitionFlipFromRight | 转场从右翻转 |
| UIViewAnimationOptionTransitionCurlUp | 上卷转场 |
| UIViewAnimationOptionTransitionCurlDown | 下卷转场 |
| UIViewAnimationOptionTransitionCrossDissolve | 转场交叉消失 |
| UIViewAnimationOptionTransitionFlipFromTop | 转场从上翻转 |
| UIViewAnimationOptionTransitionFlipFromBottom | 转场从下翻转 |
转场动画
转场动画的API如下,是为了控制视图的跳转而使用的
[UIView transitionWithView:subView duration:1.0 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
[subView addSubview:testView];
} completion:^(BOOL finished) {
}];
运行这段代码可以看到视图被翻转过来的时候添加了testView在上面。
[UIView transitionFromView:subView toView:testView duration:1.0 options:UIViewAnimationOptionTransitionFlipFromLeft completion:nil];
这个方法可以控制一个控制器中的视图切换。
关键帧动画:
[UIView animateKeyframesWithDuration: delay: options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
[UIView addKeyframeWithRelativeStartTime: relativeDuration:0.25 animations:^{
CGRect frame = testView.frame;
frame.origin.y += * flag;
testView.frame = frame;
}];
[UIView addKeyframeWithRelativeStartTime:0.25 relativeDuration:0.25 animations:^{
CGRect frame = testView.frame;
frame.origin.y -= * flag;
testView.frame = frame;
}];
[UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5 animations:^{
CGRect frame = testView.frame;
frame.origin.y += * flag;
testView.frame = frame;
}];
} completion:^(BOOL finished) {
btn.hidden = YES;
}];
可以看到,关键帧动画我们很方便的可以控制动画的整个过程,addKeyframeWithRelativeStartTime:是添加关键帧方法,参数startTime是一个在0~1之间的数字,表示开始时间占总时间的%多少,比如上例中的第一帧就是0,第二针就是25%也就是在0.5秒开始。relativeDuration和开始时间一样,是运行时间占整个时间的百分比。
弹簧动画
[UIView animateWithDuration:0.5 delay:1.0 usingSpringWithDamping: initialSpringVelocity:0.1 options:UIViewAnimationOptionAutoreverse animations:^{
CGRect frame = testView.frame;
frame.origin.y += * flag;
testView.frame = frame;
} completion:^(BOOL finished) {
btn.hidden = YES;
}];
弹簧动画的阻尼值,也就是相当于摩擦力的大小,该属性的值从0.0到1.0之间,越靠近0,阻尼越小,弹动的幅度越大,反之阻尼越大,弹动的幅度越小,如果大道一定程度,会出现弹不动的情况。
弹簧动画的速率,或者说是动力。值越小弹簧的动力越小,弹簧拉伸的幅度越小,反之动力越大,弹簧拉伸的幅度越大。这里需要注意的是,如果设置为0,表示忽略该属性,由动画持续时间和阻尼计算动画的效果。
iOS动画——UIKit动画的更多相关文章
- iOS 视图,动画渲染机制探究
腾讯Bugly特约作者:陈向文 终端的开发,首当其冲的就是视图.动画的渲染,切换等等.用户使用 App 时最直接的体验就是这个界面好不好看,动画炫不炫,滑动流不流畅.UI就是 App 的门面,它的体验 ...
- iOS关于CoreAnimation动画知识总结
一:UIKit动画 在介绍CoreAnimation动画前先简单介绍一下UIKit动画,大部分简单的动画都可以使用UIKit动画实现,如果想实现更复杂的效果,则需要使用Core Animation了: ...
- IOS中的动画菜单
SvpplyTable(可折叠可张开的菜单动画) 允许你简单地创建可折叠可张开的菜单动画效果,灵感来自于Svpply app.不同表格项目使用JSON定义,你可以定义每个菜单项和任何子菜单,为每个项目 ...
- iOS中的动画
iOS中的动画 Core Animation Core Animation是一组非常强大的动画处理API,使用它能做出非常绚丽的动画效果,而且往往是事半功倍,使用它需要添加QuartzCore .fr ...
- iOS之核心动画(Core Animation)
Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍.也就是说,使用少量的代码就可以实现非常强大的功能. Core ...
- iOS 8自定义动画转场上手指南
原文:http://www.cocoachina.com/ios/20150126/11011.html iOS 5发布的时候,苹果针对应用程序界面的设计,提出了一种全新的,革命性的方法—Storyb ...
- iOS 转场动画探究(一)
什么是转场动画: 转场动画说的直接点就是你常见的界面跳转的时候看到的动画效果,我们比较常见的就是控制器之间的Push和Pop,还有Present和Dismiss的时候设置一下系统给我们的modalTr ...
- iOS 转场动画探究(二)
这篇文章是接着第一篇写的,要是有同行刚看到的话建议从前面第一篇看,这是第一篇的地址:iOS 转场动画探究(一) 接着上一篇写的内容: 上一篇iOS 转场动画探究(一)我们说到了转场要素的第四点,把那个 ...
- iOS中 Animation 动画大全 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博! iOS开发者交流QQ群: 446310206 1.iOS中我们能看到的控件都是UIView的子类,比如UIButt ...
随机推荐
- SpringBoot yaml的配置及使用
application.yml配置如下 person: lastName: hello age: boss: false birth: // maps: {k ...
- v-on(事件处理)
1.监听事件 v-on:click="msg+=1" (msg是写在data里) 2.方法事件处理器 v-on:click = "jia" (jia是写在me ...
- Maven学习总结(30)——Maven项目通用三级版本号说明
项目版本号说明 当前版本号:1.0.0-SNAPSHOT 本项目采用通用的三级版本号,版本号格式是[主版本号].[副版本号].[修复版本号]-[稳定状态],如:1.0.0-SNAPS ...
- POJ 3270 置换群问题
题目大意是: 每头牛都有一个对应的值a[i],现在给定一个初始的牛的序列,希望通过两两交换,能够使这些牛按值升序排列,每次交换都会耗费一个 a[i]+a[j] 希望耗费最小,求出这个最小耗费 个人觉得 ...
- [luoguP1005] 矩阵取数游戏(DP + 高精度)
传送门 和奶牛那个题很像,每一行状态互不影响,也就是求 n 遍DP 不过高精度非常恶心,第一次写,调了我一上午. ——代码 #include <cstdio> #include <c ...
- [K/3Cloud] 首页增加一个自定义页签及页面内容
在K3Cloud登录后的门户首页增加一个页签,如增加一个[BBS论坛] 2013-7-30 11:18:51 上传 下载附件 (84.81 KB) 增加页签 可以这么来做: 进入BOS IDE ,找 ...
- Self Centos + Windows server 2016
Set up by Derek: 2019-1-25 登陆个人物理机: license 60天Free , 如果过期,就在 VMware ESXI 6.5.0的黑屏界面去reset. https:/ ...
- 理解 LARGE_INTEGER的定义
http://bbs.csdn.net/topics/310239341 #if defined(MIDL_PASS) typedef struct _LARGE_INTEGER { #else // ...
- 20181010关于pt-kill自动杀死运行超长的进程
转自: http://blog.chinaunix.net/uid-16844903-id-4442030.htmlhttp://blog.chinaunix.net/uid-31396856-id- ...
- graylog 市场
https://marketplace.graylog.org/addons/246dc332-7da7-4016-b2f9-b00f722a8e79