iOS核心动画-2

翻译 2015年07月10日 15:27:47

一.基本方式:使用UIView类的UIViewAnimation扩展
函数说明

复制代码
+ (void)beginAnimations:(NSString *)animationID context:(void *)context; // 开始准备动画
+ (void)commitAnimations; // 运行动画

// 没有get方法,下面的set在快外调用无效
+ (void)setAnimationDelegate:(id)delegate; // 委托default = nil
+ (void)setAnimationWillStartSelector:(SEL)selector; // default = NULL. -animationWillStart:(NSString *)animationID context:(void *)context
+ (void)setAnimationDidStopSelector:(SEL)selector; // default = NULL. -animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
+ (void)setAnimationDuration:(NSTimeInterval)duration; // default = 0.2动画时间
+ (void)setAnimationDelay:(NSTimeInterval)delay; // default = 0.0延迟多少时间开始执行动画
+ (void)setAnimationStartDate:(NSDate *)startDate; // default = now ([NSDate date])动画开始日期?不知道啥用.- -
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve; // default = UIViewAnimationCurveEaseInOut动画方式
+ (void)setAnimationRepeatCount:(float)repeatCount; // default = 0.0. May be fractional重复次数
+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses; // default = NO. YES的话,动画(非最后一次)结束后动态复原到最开始状态
+ (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState; // default = NO. YES,停止之前的动画,从现在这里开始新动画the current view position is always used for new animations -- allowing animations to "pile up" on each other. Otherwise, the last end state is used for the animation (the default).

+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache; // 添加动画到view上,cache是YES的时候比较高效,但是动画过程中不能更新界面上的内容,NO时每一帧都重新画,可以实时更新
+ (void)setAnimationsEnabled:(BOOL)enabled; // 是否忽略一些动画设置
+ (BOOL)areAnimationsEnabled;
复制代码


一个动画的代码

复制代码
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:2.7];
[UIView setAnimationTransition:transition forView:self.view cache:YES];
// operation>>>
[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
// end<<<<<<
[UIView commitAnimations];
复制代码

其中transition取值范围

复制代码
typedef enum {
UIViewAnimationTransitionNone,
UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,
UIViewAnimationTransitionCurlUp,
UIViewAnimationTransitionCurlDown,
} UIViewAnimationTransition;
复制代码

特点:基础,使用方便,但是效果有限

二.block方式:使用UIView类的UIViewAnimationWithBlocks扩展

函数说明

复制代码
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0);//间隔,延迟,动画参数(好像没用?),界面更改块,结束块

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

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

+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0);

+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0); // toView added to fromView.superview, fromView removed from its superview界面替换,这里的options参数有效
复制代码

举例:

复制代码
[UIView animateWithDuration:0.7 delay:0 options:0 animations:^(){
self.view.alpha = 0.2;
[self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];
self.view.alpha = 1;
} completion:^(BOOL finished)
{

}];
复制代码

当areAnimationsEnabled为NO时,上面不能动画显示

复制代码
[UIView transitionFromView:lImage toView:mImage duration:0.7 options:options completion:^(BOOL finished)
{
if (finished) {
[self.view addSubview:lImage];
[self.view sendSubviewToBack:lImage];
[self.view sendSubviewToBack:mImage];
} 
}];
复制代码

options取值范围

复制代码
enum {
UIViewAnimationOptionLayoutSubviews = 1 << 0,
UIViewAnimationOptionAllowUserInteraction = 1 << 1, // turn on user interaction while animating
UIViewAnimationOptionBeginFromCurrentState = 1 << 2, // start all views from current value, not initial value
UIViewAnimationOptionRepeat = 1 << 3, // repeat animation indefinitely
UIViewAnimationOptionAutoreverse = 1 << 4, // if repeat, run animation back and forth
UIViewAnimationOptionOverrideInheritedDuration = 1 << 5, // ignore nested duration
UIViewAnimationOptionOverrideInheritedCurve = 1 << 6, // ignore nested curve
UIViewAnimationOptionAllowAnimatedContent = 1 << 7, // animate contents (applies to transitions only)
UIViewAnimationOptionShowHideTransitionViews = 1 << 8, // flip to/from hidden state instead of adding/removing

UIViewAnimationOptionCurveEaseInOut = 0 << 16, // default
UIViewAnimationOptionCurveEaseIn = 1 << 16,
UIViewAnimationOptionCurveEaseOut = 2 << 16,
UIViewAnimationOptionCurveLinear = 3 << 16,

UIViewAnimationOptionTransitionNone = 0 << 20, // default
UIViewAnimationOptionTransitionFlipFromLeft = 1 << 20,
UIViewAnimationOptionTransitionFlipFromRight = 2 << 20,
UIViewAnimationOptionTransitionCurlUp = 3 << 20,
UIViewAnimationOptionTransitionCurlDown = 4 << 20,
UIViewAnimationOptionTransitionCrossDissolve = 5 << 20,//ios5
UIViewAnimationOptionTransitionFlipFromTop = 6 << 20,//ios5
UIViewAnimationOptionTransitionFlipFromBottom = 7 << 20,//ios5
};
typedef NSUInteger UIViewAnimationOptions;
复制代码

特点:快捷方便,效果更多.可以如上示例1那样实现界面个元素属性渐进变化的动态展示

三.core方式:使用CATransition类
使用要引入QuartzCore.framework
官方有个示例:ViewTransitions
基本上就是

复制代码
CATransition *transition = [CATransition animation];
transition.duration = 0.7;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionMoveIn;//{kCATransitionMoveIn, kCATransitionPush, kCATransitionReveal, kCATransitionFade};

//更多私有{@"cube",@"suckEffect",@"oglFlip",@"rippleEffect",@"pageCurl",@"pageUnCurl",@"cameraIrisHollowOpen",@"cameraIrisHollowClose"};
transition.subtype = kCATransitionFromLeft;//{kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom};

transition.delegate = self;
[self.view.layer addAnimation:transition forKey:nil];

// 要做的
[self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];
复制代码

其中需要注意的是:界面调整(动画前后展示的两个界面)需要在动画时间内完成,否则没有效果,对下次也有影响,我测的不是太完整,大家自己写个performSelector: withObject: afterDelay:试验下看看就知道.

特点:动画效果较多(但是私有),可以实现UIViewController之间的动画

demo:animations.zip

iOS核心动画笔记2

一注意:上图中的黑色虚线代表”继承”某个类,红色虚线代表“遵守”某个协议 CAAnimationGroup: 组动画 CAPropertyAnimation:属性动画 CATransition:...

iOS核心动画 Core Animation2-CABasicAnimation

Core Animation2-CABasicAnimation 本文目录 一、平移动画二、缩放动画三、旋转动画四、其他 CABasicAnimation是CAPropert...

iOS学习总结----核心动画(2)

实现动画分三部曲: (1)创建动画对象; (2)设置动画属性(时长,重复次数...); (3)将动画添加到图层(layer)上. 这几个Demo都是练习用的,比较简单 ...

iOS 动画Animation-2-1: 动画基础:核心动画简介

简单介绍Core Animation,核心动画,它是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍。也就是说,使用少量的代码就可以实现非常强大的功能。Core Ani...

iOS核心动画技巧

  • 2016年10月14日 10:02
  • 22.16MB
  • 下载

iOS核心动画高级篇

  • 2017年06月14日 16:20
  • 21.25MB
  • 下载

iOS 动画Animation-2-2: 动画基础:核心动画

上一篇已经简单的介绍过核心动画了,这次就针对实力详细介绍一下Animation 核心动画。...

iOS核心动画高级技巧

  • 2016年08月28日 23:54
  • 22.14MB
  • 下载

iOS核心动画高级技巧

  • 2016年09月13日 15:18
  • 20.35MB
  • 下载

iOS学习笔记09-核心动画CoreAnimation

一、CALayerCALayer包含在QuartzCore框架中,具有跨平台性,在iOS中使用Core Animation开发动画的本质是 将CALayer内容转化为位图从而供硬件操作 。常用属性: ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:iOS核心动画-2
举报原因:
原因补充:

(最多只允许输入30个字)