动画效果提供了状态或页面转换时流畅的用户体验,在iOS系统中,咱们不需要自己编写绘制动画的代码,Core Animation提供了丰富的api来实现你需要的动画效果。

UIKit只用UIView来展示动画,动画支持UIView下面的这些属性改变:

 

1、commitAnimations方式使用UIView动画

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  5. [button setTitle:@"改变" forState:UIControlStateNormal];
  6. button.frame = CGRectMake(10, 10, 60, 40);
  7. [button addTarget:self action:@selector(changeUIView) forControlEvents:UIControlEventTouchUpInside];
  8. [self.view addSubview:button];
  9. }
  10. - (void)changeUIView{
  11. [UIView beginAnimations:@"animation" context:nil];
  12. [UIView setAnimationDuration:1.0f];
  13. [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
  14. [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
  15. [UIView commitAnimations];
  16. }

下面是点击改变后的效果(两种):

 

动画的常量有一下四种
  1. UIViewAnimationTransitionNone,
  2. UIViewAnimationTransitionFlipFromLeft,
  3. UIViewAnimationTransitionFlipFromRight,
  4. UIViewAnimationTransitionCurlUp,
  5. UIViewAnimationTransitionCurlDown,

1.2 交换本视图控制器中2个view位置

[self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];

先添加两个view ,一个redview  一个yellowview

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. UIView *redView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  5. redView.backgroundColor = [UIColor redColor];
  6. [self.view addSubview:redView];
  7. UIView *yellowView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  8. yellowView.backgroundColor = [UIColor yellowColor];
  9. [self.view addSubview:yellowView];
  10. UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  11. [button setTitle:@"改变" forState:UIControlStateNormal];
  12. button.frame = CGRectMake(10, 10, 300, 40);
  13. [button addTarget:self action:@selector(changeUIView) forControlEvents:UIControlEventTouchUpInside];
  14. [self.view addSubview:button];
  15. UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  16. [button1 setTitle:@"改变1" forState:UIControlStateNormal];
  17. button1.frame = CGRectMake(10, 60, 300, 40);
  18. [button1 addTarget:self action:@selector(changeUIView1) forControlEvents:UIControlEventTouchUpInside];
  19. [self.view addSubview:button1];
  20. }
  1. - (void)changeUIView1{
  2. [UIView beginAnimations:@"animation" context:nil];
  3. [UIView setAnimationDuration:1.0f];
  4. [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
  5. [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
  6. //  交换本视图控制器中2个view位置
  7. [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];
  8. [UIView commitAnimations];
  9. }

这样看起来就像两页一样了。

1.3 、   [UIView setAnimationDidStopSelector:@selector(animationFinish:)];

在commitAnimations消息之前,可以设置动画完成后的回调,设置方法是:

[UIView setAnimationDidStopSelector:@selector(animationFinish:)];

 

2、使用:CATransition

  1. - (void)changeUIView2{
  2. CATransition *transition = [CATransition animation];
  3. transition.duration = 2.0f;
  4. transition.type = kCATransitionPush;
  5. transition.subtype = kCATransitionFromTop;
  6. [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];
  7. [self.view.layer addAnimation:transition forKey:@"animation"];
  8. }

transition.type 的类型可以有

淡化、推挤、揭开、覆盖

NSString * const kCATransitionFade;

NSString * const kCATransitionMoveIn;

NSString * const kCATransitionPush;

NSString * const kCATransitionReveal;

这四种,

transition.subtype 
也有四种

NSString * const kCATransitionFromRight;

NSString * const kCATransitionFromLeft;

NSString * const kCATransitionFromTop;

NSString * const kCATransitionFromBottom;

 

2.2 私有的类型的动画类型:

立方体、吸收、翻转、波纹、翻页、反翻页、镜头开、镜头关

  1. animation.type = @"cube"
  2. animation.type = @"suckEffect";
  3. animation.type = @"oglFlip";//不管subType is "fromLeft" or "fromRight",official只有一种效果
  4. animation.type = @"rippleEffect";
  5. animation.type = @"pageCurl";
  6. animation.type = @"pageUnCurl"
  7. animation.type = @"cameraIrisHollowOpen ";
  8. animation.type = @"cameraIrisHollowClose ";

下图是第一个cube立方体的效果:

2.3 CATransition的 startProgress  endProgress属性

这两个属性是float类型的。
可以控制动画进行的过程,可以让动画停留在某个动画点上,值在0.0到1.0之间。endProgress要大于等于startProgress。
比如上面的立方体转到,可以设置endProgress= 0.5,让动画停留在转动一般的位置。
上面这些私有的动画效果,在实际应用中要谨慎使用。因为在app store审核时可能会以为这些动画效果而拒绝通过。
 
 

3、UIView的 + (void)animateWithDuration

:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
方法。
这个方法是在iOS4.0之后才支持的。
比 1 里的UIView的方法简洁方便使用。
DidView里添加moveView。
  1. moveView = [[UIView alloc] initWithFrame:CGRectMake(10, 180, 200, 40)];
  2. moveView.backgroundColor = [UIColor blackColor];
  3. [self.view addSubview:moveView];
  1. - (void)changeUIView3{
  2. [UIView animateWithDuration:3 animations:^(void){
  3. moveView.frame = CGRectMake(10, 270, 200, 40);
  4. }completion:^(BOOL finished){
  5. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 40, 40)];
  6. label.backgroundColor = [UIColor blackColor];
  7. [self.view addSubview:label];
  8. }];
  9. }

然后用UIView animateWithDuration动画移动,移动动画完毕后添加一个Label。

3.2、 animateWithDuration的嵌套使用

  1. - (void)changeUIView3{
  2. [UIView animateWithDuration:2
  3. delay:0
  4. options:UIViewAnimationOptionCurveEaseOut animations:^(void){
  5. moveView.alpha = 0.0;
  6. }completion:^(BOOL finished){
  7. [UIView animateWithDuration:1
  8. delay:1.0
  9. options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
  10. animations:^(void){
  11. [UIView setAnimationRepeatCount:2.5];
  12. moveView.alpha = 1.0;
  13. }completion:^(BOOL finished){
  14. }];
  15. }];
  16. }

这个嵌套的效果是先把view变成透明,在从透明变成不透明,重复2.5次透明到不透明的效果。

文中例子的代码:AnimateDemo

iOS动画效果和实现的更多相关文章

  1. ios 动画效果CATransition笔记

    初学ios开发,很多概念还不清楚,所以只有边学边做例子.又怕学了后面忘了前面,因此用自己的博客来纪录自己的学习历程,也是对自己学习不要懈怠做个监督. 刚学ios做动画效果.因为ios封装得很好,实现i ...

  2. iOS 动画效果:Core Animation & Facebook's pop

    本文转载至 http://www.cocoachina.com/ios/20151223/14739.html 感谢原创作者分享 前言相信很多人对实现 iOS 中的动画效果都特别头疼,往往懒得动手,功 ...

  3. iOS动画效果集合、 通过摄像头获取心率、仿淘宝滑动样式、瀑布流、分类切换布局等源码

    iOS精选源码 动画知识运用及常见动画效果收集 较为美观的多级展开列表 MUImageCache -简单轻量的图片缓存方案 iOS 瀑布流之栅格布局 一用就上瘾的JXCategoryView iOS ...

  4. iOS动画效果合集、飞吧企鹅游戏、换肤方案、画板、文字效果等源码

    iOS精选源码 动画知识运用及常见动画效果收集 3D卡片拖拽卡片叠加卡片 iFIERO - FLYING PENGUIN 飞吧企鹅SpriteKit游戏(源码) Swift封装的空数据提醒界面Empt ...

  5. ios动画效果集锦(持续更新)

    1.树叶滚动进度:http://www.jianshu.com/p/800496caa055 2.列表滚动动画和滚动视差效果http://www.jianshu.com/p/42e1eb59a1af ...

  6. iOS 动画效果。简单的提示消失

    UILabel * label1 = [[UILabel alloc]initWithFrame:CGRectMake(, , , )]; label1.text = @"qingjoin& ...

  7. (转)iOS动画Core Animation

    文章转载:http://blog.sina.com.cn/s/blog_7b9d64af0101b8nh.html 在iOS中动画实现技术主要是:Core Animation. Core Animat ...

  8. IOS动画(Core Animation)总结 (参考多方文章)

    一.简介 iOS 动画主要是指Core Animation框架.官方使用文档地址为:Core Animation Guide. Core Animation是IOS和OS X平台上负责图形渲染与动画的 ...

  9. 轻松实现Android,iOS的一个手势动画效果

    先来看效果 这是iOS下的效果,android下完全一致.通过do_GestureView组件和do_Animation组件,deviceone能很容易实现复杂的跨平台纯原生动画效果,这个示例就是通过 ...

随机推荐

  1. IntelliJ运行下载的Servlet时报错 Error running Tomcat 8.5.8: Unable to open debugger port (127.0.0.1:49551): java.net.SocketException

    学习Java Servlet时,从Wrox上下载了示例代码,准备run/debug时发现以下错误: Error running Tomcat 8.5.8: Unable to open debugge ...

  2. 架构师养成记--11.Executor概述

    常用方法 Executors.newFiexdPool(int nThreads);固定线程数量的线程池: Executors.newSingleThreadExecutor();单个线程的线程池: ...

  3. redis 操作 list 的测试

    lpush 从头部压入数据 127.0.0.1:6379> lpush listname value1 (integer) 1//返回list的当前长度 127.0.0.1:6379> l ...

  4. Base64编码【转】

    转http://www.cnblogs.com/luguo3000/p/3940197.html 开发者对Base64编码肯定很熟悉,是否对它有很清晰的认识就不一定了.实际上Base64已经简单到不能 ...

  5. UML大战需求分析--阅读笔记3

    这次阅读的是第四章,流程分析利器之 – 活动图.对需求有两种分析的方式:结构建模与行为建模.活动图是行为建模中经常使用的一种图.由流程图发展而来. 活动图中有一些名词:开始状态.结束状态.活动.判断. ...

  6. jdk 安装 环境变量配置

    右键选择 计算机→属性→高级系统设置→高级→环境变量 1.系统变量→新建 变量名:JAVA_HOME 变量值:(变量值填写你的jdk的安装目录,例如本人是 C:\Program Files\Java\ ...

  7. jquery 返回顶部 兼容web移动

    返回顶部图片 http://hovertree.com/texiao/mobile/6/tophovertree.gif 具体实现代码 <span id="tophovertree&q ...

  8. 前端面霸系列(1):doctype 、Quirks Mode & Standards Mode 、document.compatMode

    近几日,气压猛降,雾霾铺天盖地,眼看一场腥风血雨就要在前端江湖爆发,这场战争不仅是百度.腾讯.阿狸.搜狐网易新浪等江湖豪门抢夺人才的大战,也是诸位江湖人士重新洗牌的好时机.每年10月,江湖的波动胜过华 ...

  9. struts.xml

    <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC    " ...

  10. 【转】使用SQL Tuning Advisor STA优化SQL

    SQL优化器(SQL Tuning Advisor STA)是Oracle10g中推出的帮助DBA优化工具,它的特点是简单.智能,DBA值需要调用函数就可以给出一个性能很差的语句的优化结果.下面介绍一 ...