iOS動(dòng)畫

1、Core Animation

Core Animation了赵,即為核心動(dòng)畫,它是一組非常強(qiáng)大的動(dòng)畫處理API甸赃,使用它能做出非常炫麗的動(dòng)畫效果柿汛,而且往往是事半功倍。也就是說埠对,使用少量的代碼就可以實(shí)現(xiàn)非常強(qiáng)大的功能络断。Core Animation可以用在Mac OS X和iOS平臺。Core Animation的動(dòng)畫執(zhí)行過程都是在后臺操作的项玛,不會(huì)阻塞主線程貌笨。要注意的是,Core Animation是直接作用在CALayer上的襟沮,并非UIView锥惋。

QuartzCore.framework框架

CAAnimation{

? ? ? ? CAPropertyAnimation{

? ? ? ? ? ? CABasicAnimation{

? ? ? ? ? ? ? ? CASpringAnimation

? ? ? ? ? ? }

? ? ? ? ? ? CAKeyframeAnimation

? ? ? ? }

? ? ? ? CATransition

? ? ? ? CAAnimationGroup

? ? }

CAAnimation是所有動(dòng)畫對象的父類昌腰,負(fù)責(zé)控制動(dòng)畫的持續(xù)時(shí)間和速度,是個(gè)抽象類膀跌,不能直接使用遭商,應(yīng)該使用它具體的子類。

CAPropertyAnimation捅伤,是CAAnimation的子類株婴,也是個(gè)抽象類,要想創(chuàng)建動(dòng)畫對象暑认,應(yīng)該使用它的兩個(gè)子類:CABasicAnimation和CAKeyframeAnimation。

CABasicAnimation大审,是CAPropertyAnimation的子類蘸际,其子類是CASpringAnimation,它主要用于制作比較單一的動(dòng)畫徒扶,例如粮彤,平移、縮放姜骡、旋轉(zhuǎn)导坟、顏色漸變、邊框的值的變化等圈澈,也就是將layer的某個(gè)屬性值從一個(gè)值到另一個(gè)值的變化惫周。類似x -> y這種變化,然而對于x -> y -> z甚至更多的變化是不行的康栈。

1.移動(dòng)動(dòng)畫

UIView*moveView = [[UIViewalloc]initWithFrame:CGRectMake(100,100,100,100)];

? ? moveView.backgroundColor = [UIColor redColor];

? ? [self.viewaddSubview:moveView];


? ? CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];

? ? animation.duration=2;

? ? animation.repeatCount=2;

? ? animation.beginTime=CACurrentMediaTime() +1;// 1秒后執(zhí)行

? ? animation.fromValue= [NSValuevalueWithCGPoint:moveView.layer.position];//?起始幀

? ? animation.toValue=

? ? [NSValue?valueWithCGPoint:CGPointMake(300, 300)];?//?終了幀

? ? // 視圖添加動(dòng)畫

? ? [moveView.layeraddAnimation:animationforKey:@"move"];

2.旋轉(zhuǎn)動(dòng)畫

UIView*moveView = [[UIViewalloc]initWithFrame:CGRectMake(100,100,100,100)];

? ? moveView.backgroundColor = [UIColor redColor];

? ? [self.viewaddSubview:moveView];


? ? //?對Y軸進(jìn)行旋轉(zhuǎn)(指定Z軸的話递递,就和UIView的動(dòng)畫一樣繞中心旋轉(zhuǎn))

? ? CABasicAnimation*animation?=

? ? [CABasicAnimation?animationWithKeyPath:@"transform.rotation.y"];

? ? animation.duration=2;

? ? animation.repeatCount=2;

? ? animation.beginTime=CACurrentMediaTime() +1;// 1秒后執(zhí)行

? ? animation.fromValue=?[NSNumbernumberWithFloat:0.0];//?起始角度

? ? animation.toValue=?[NSNumbernumberWithFloat:M_PI];//?終止角度

? ? [moveView.layeraddAnimation:animationforKey:@"rotate"];

3.縮放動(dòng)畫

UIView*moveView = [[UIViewalloc]initWithFrame:CGRectMake(100,100,100,100)];

? ? moveView.backgroundColor = [UIColor redColor];

? ? [self.viewaddSubview:moveView];


? ? CABasicAnimation*animation?=

? ? [CABasicAnimation?animationWithKeyPath:@"transform.scale"];

? ? animation.duration=2.5;//?動(dòng)畫持續(xù)時(shí)間

? ? animation.repeatCount=2;//?重復(fù)次數(shù)

? ? animation.fromValue=?[NSNumbernumberWithFloat:1.0];//?開始時(shí)的倍率

? ? animation.toValue=?[NSNumbernumberWithFloat:2.0];//?結(jié)束時(shí)的倍率

? ? [moveView.layeraddAnimation:animationforKey:@"scale"];

4.漸隱動(dòng)畫

UIView*moveView = [[UIViewalloc]initWithFrame:CGRectMake(100,100,100,100)];

? ? moveView.backgroundColor = [UIColor redColor];

? ? [self.viewaddSubview:moveView];


? ? //opacity 指layer的透明度

? ? CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];

? ? basicAnimation.fromValue=@(1.0);

? ? basicAnimation.toValue?= @(0.0);//[NSNumber numberWithFloat:0.0]

? ? basicAnimation.repeatCount=2;

? ? basicAnimation.duration=2;

? ? [moveView.layeraddAnimation:basicAnimationforKey:@"op"];

4.顏色漸變

UIView*moveView = [[UIViewalloc]initWithFrame:CGRectMake(100,100,100,100)];

? ? moveView.backgroundColor = [UIColor redColor];

? ? [self.viewaddSubview:moveView];


? ? CABasicAnimation?* animation?=?[CABasicAnimation?animationWithKeyPath:@"backgroundColor"];

? ? animation.duration=2;

? ? animation.repeatCount=2;

? ? animation.beginTime=CACurrentMediaTime() +1;// 1秒后執(zhí)行

? ? animation.fromValue=?(__bridgeid_Nullable)([UIColorredColor].CGColor);

? ? animation.toValue=?(__bridgeid_Nullable)([UIColorpurpleColor].CGColor);

? ? [moveView.layeraddAnimation:animationforKey:@"bgcolor"];


5.組合動(dòng)畫

UIView*moveView = [[UIViewalloc]initWithFrame:CGRectMake(100,100,100,100)];

? ? moveView.backgroundColor = [UIColor redColor];

? ? [self.viewaddSubview:moveView];


? ? CABasicAnimation?* animation1?= [CABasicAnimation?animationWithKeyPath:@"transform.translation.x"];

? ? animation1.toValue=?[NSNumbernumberWithFloat:80];;//?終點(diǎn)

? ? CABasicAnimation?* animation2?=[CABasicAnimation?animationWithKeyPath:@"transform.translation.z"];

? ? animation2.fromValue=?[NSNumbernumberWithFloat:0.0];//?開始時(shí)的角度

? ? animation2.toValue=?[NSNumbernumberWithFloat:M_PI];//?結(jié)束時(shí)的角度


? ? /*?動(dòng)畫組?*/

? ? CAAnimationGroup?*group?=?[CAAnimationGroup?animation];

? ? group.duration=1.0;

? ? group.repeatCount=1;

? ? group.animations=

? ? [NSArrayarrayWithObjects:animation1,?animation2,nil];

? ? [moveView.layeraddAnimation:groupforKey:@"move-rotate"];

觸摸控制

-(void)move:(CGPoint)position{

? ? CASpringAnimation *animation = [CASpringAnimation animationWithKeyPath:@"position"];

? ? animation.fromValue= [NSValuevalueWithCGPoint:self.moveView.center];

? ? animation.toValue= [NSValuevalueWithCGPoint:position];

? ? //? ? 設(shè)置fillModel 必須設(shè)置 removedOnCompletion

? ? animation.removedOnCompletion = NO;

? ? animation.fillMode = kCAFillModeBoth;

? ? [self.moveView.layeraddAnimation:animationforKey:@"move"];

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent*)event{

? ? [self move:[[touches anyObject]locationInView:self.view]];

}

停在終點(diǎn)

animation.removedOnCompletion = NO;

? ? animation.fillMode = kCAFillModeForwards;

捕獲動(dòng)畫開始時(shí)和終了時(shí)的事件

-?(void)animationDidStart:(CAAnimation?*)theAnimation {

?}

-?(void)animationDidStop:(CAAnimation?*)theAnimation?finished:(BOOL)flag {

?}

動(dòng)畫表


所有動(dòng)畫類型


CASpringAnimation 是CABasicAnimation的子類?彈簧動(dòng)畫

self.moveView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];

? ? self.moveView.backgroundColor = [UIColor redColor];

? ? [self.viewaddSubview:self.moveView];

? ? CASpringAnimation *animation = [CASpringAnimation animationWithKeyPath:@"bounds"];

? ? animation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, CGRectGetWidth(self.moveView.frame)*1.5, CGRectGetHeight(self.moveView.frame)*1.5)];

? ? animation.mass=2;

? ? //阻力

? ? animation.damping=3;

? ? //初始速率

? ? animation.initialVelocity=50;

? ? //剛度

? ? animation.stiffness=100;

? ? //持續(xù)時(shí)間

? ? animation.duration= animation.settlingDuration;

? ? [self.moveView.layeraddAnimation:animationforKey:@"jamp"];


CAKeyframeAnimation,是CAPropertyAnimation的子類

value形式

self.moveView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];

? ? self.moveView.backgroundColor = [UIColor redColor];

? ? [self.viewaddSubview:self.moveView];


? ? CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

? ? //設(shè)置value

? ? NSValue *value1=[NSValue valueWithCGPoint:CGPointMake(100, 100)];

? ? NSValue *value2=[NSValue valueWithCGPoint:CGPointMake(200, 100)];

? ? NSValue *value3=[NSValue valueWithCGPoint:CGPointMake(200, 200)];

? ? NSValue *value4=[NSValue valueWithCGPoint:CGPointMake(100, 200)];

? ? NSValue *value5=[NSValue valueWithCGPoint:CGPointMake(100, 300)];

? ? NSValue *value6=[NSValue valueWithCGPoint:CGPointMake(200, 400)];

? ? animation.values=@[value1,value2,value3,value4,value5,value6];

? ? //重復(fù)次數(shù) 默認(rèn)為1

? ? animation.repeatCount=MAXFLOAT;

? ? //設(shè)置是否原路返回默認(rèn)為不

? ? animation.autoreverses=YES;

? ? //設(shè)置移動(dòng)速度啥么,越小越快

? ? animation.duration=4.0f;

? ? animation.removedOnCompletion = NO;

? ? animation.fillMode = kCAFillModeForwards;

? ? animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

? ? animation.delegate=self;

? ? //給這個(gè)view加上動(dòng)畫效果

? ? [self.moveView.layeraddAnimation:animationforKey:nil];

path形式

self.moveView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];

? ? self.moveView.backgroundColor = [UIColor redColor];

? ? [self.viewaddSubview:self.moveView];


? ? CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

? ? //創(chuàng)建一個(gè)CGPathRef對象登舞,就是動(dòng)畫的路線

? ? CGMutablePathRef path = CGPathCreateMutable();

? ? //自動(dòng)沿著弧度移動(dòng)

? ? CGPathAddEllipseInRect(path, NULL, CGRectMake(150, 200, 200, 100));

? ? //設(shè)置開始位置

? ? CGPathMoveToPoint(path,NULL,100,100);

? ? //沿著直線移動(dòng)

? ? CGPathAddLineToPoint(path,NULL, 200, 100);

? ? CGPathAddLineToPoint(path,NULL, 200, 200);

? ? CGPathAddLineToPoint(path,NULL, 100, 200);

? ? CGPathAddLineToPoint(path,NULL, 100, 300);

? ? CGPathAddLineToPoint(path,NULL, 200, 400);

? ? //沿著曲線移動(dòng)

? ? CGPathAddCurveToPoint(path,NULL,50.0,275.0,150.0,275.0,70.0,120.0);

? ? CGPathAddCurveToPoint(path,NULL,150.0,275.0,250.0,275.0,90.0,120.0);

? ? CGPathAddCurveToPoint(path,NULL,250.0,275.0,350.0,275.0,110.0,120.0);

? ? CGPathAddCurveToPoint(path,NULL,350.0,275.0,450.0,275.0,130.0,120.0);

? ? animation.path=path;

? ? CGPathRelease(path);

? ? animation.autoreverses=YES;

? ? animation.repeatCount=MAXFLOAT;

? ? animation.removedOnCompletion = NO;

? ? animation.fillMode = kCAFillModeForwards;

? ? animation.duration=4.0f;

? ? animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

? ? animation.delegate=self;

? ? [self.moveView.layeraddAnimation:animationforKey:nil];

一般推薦UI框架自帶的貝塞爾曲線,可以用以下代碼替換CGMutablePathRef那一段悬荣,代碼如下:

// ②.1創(chuàng)建路徑

? ? UIBezierPath * path = [UIBezierPath bezierPathWithRect:CGRectMake(75, 75, 200, 200)];

? ? // ②.2設(shè)置路徑的屬性

? ? // ②.3將路徑設(shè)置給動(dòng)畫

? ? [animation setPath:path.CGPath];

創(chuàng)建一個(gè)模仿刪除App的晃動(dòng)動(dòng)畫:

// ①.初始化動(dòng)畫屬性

? ? CAKeyframeAnimation * animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];// 旋轉(zhuǎn)(旋轉(zhuǎn)搖晃)

? ? // ②.設(shè)置動(dòng)畫屬性 ??思路:搖晃菠秒,從一個(gè)點(diǎn),搖晃到另一個(gè)點(diǎn)

? ? // ②.1定義角度值

? ? // NSArray * array = @[角度數(shù)值]

? ? CGFloat angle = M_PI_4 / 9.0;// 每次抖動(dòng)5度

? ? NSArray * array = @[@(angle),@(-angle),@(angle)];// 開始角度氯迂,經(jīng)過角度践叠,回復(fù)角度 相當(dāng)于 左、右囚戚、左 然后重復(fù)

? ? // ②.2設(shè)置動(dòng)畫數(shù)組

? ? [animation setValues:array];

? ? // 修改動(dòng)畫的填充方法

? ? [animation setFillMode:kCAFillModeForwards];

? ? [animation setRemovedOnCompletion:NO];

? ? // 設(shè)置時(shí)長

? ? animation.duration = 0.2f;

? ? // 設(shè)置重復(fù)

? ? [animation setRepeatCount:MAXFLOAT];

? ? // 反轉(zhuǎn)動(dòng)畫

? ? // ③.將動(dòng)畫添加到圖層

? ? [demoView.layer addAnimation:animation forKey:nil];


CAAnimationGroup酵熙,CAAnimation的子類,可以保存一組動(dòng)畫對象驰坊,將CAAnimationGroup對象加入層后匾二,組中所有動(dòng)畫對象可以同時(shí)并發(fā)運(yùn)行

self.moveView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

? ? self.moveView.backgroundColor = [UIColor redColor];

? ? [self.viewaddSubview:self.moveView];


? ? CAAnimationGroup *group = [CAAnimationGroup animation];

? ? // 平移

? ? CABasicAnimation *anim = [CABasicAnimation animation];

? ? anim.keyPath=@"position";

? ? anim.toValue = [NSValue valueWithCGPoint:CGPointMake(arc4random_uniform(200), arc4random_uniform(500))];

? ? // 縮放

? ? CABasicAnimation *anim1 = [CABasicAnimation animation];

? ? anim1.keyPath = @"transform.scale";

? ? // 0 ~ 1

? ? staticCGFloatscale =0.1;

? ? if(scale <1) {

? ? ? ? scale =1.5;

? ? }else{

? ? ? ? scale =0.2;

? ? }

? ? anim1.toValue=@(scale);

? ? // 旋轉(zhuǎn)

? ? CABasicAnimation *anim2 = [CABasicAnimation animation];

? ? anim2.keyPath = @"transform.rotation";

? ? anim2.toValue=@(arc4random_uniform(360) /180.0*M_PI);

? ? group.animations=@[anim,anim1,anim2];

? ? group.duration=0.5;

? ? // 取消反彈

? ? // 告訴在動(dòng)畫結(jié)束的時(shí)候不要移除

? ? group.removedOnCompletion = NO;

? ? // 始終保持最新的效果

? ? group.fillMode = kCAFillModeForwards;

? ? [self.moveView.layer addAnimation:group forKey:nil];


CATransition,CAAnimation的子類,用于做轉(zhuǎn)場動(dòng)畫察藐,能夠?yàn)閷犹峁┮瞥銎聊缓鸵迫肫聊坏膭?dòng)畫效果皮璧。iOS比Mac OS X的轉(zhuǎn)場動(dòng)畫效果少一點(diǎn)。UINavigationController就是通過CATransition實(shí)現(xiàn)了將控制器的視圖推入屏幕的動(dòng)畫效果分飞。

CATransition的type屬性 ??

?1.#define定義的常量???

?????kCATransitionFade???交叉淡化過渡???

?????kCATransitionMoveIn?新視圖移到舊視圖上面???

?????kCATransitionPush???新視圖把舊視圖推出去???

?????kCATransitionReveal?將舊視圖移開,顯示下面的新視圖 ??

?2.用字符串表示???

?????pageCurl????????????向上翻一頁???

?????pageUnCurl??????????向下翻一頁???

?????rippleEffect????????滴水效果???

?????suckEffect??????????收縮效果悴务,如一塊布被抽走???

?????cube????????????????立方體效果???

?????oglFlip?????????????上下翻轉(zhuǎn)效果?

subtype的屬性

kCATransitionFromRight,右

kCATransitionFromLeft譬猫,左

kCATransitionFromTop讯檐,上

kCATransitionFromBottom,下

// 轉(zhuǎn)場動(dòng)畫

? ? CATransition *animation = [CATransition animation];

? ? animation.duration=0.6;

? ? animation.fillMode = kCAFillModeForwards;

? ? animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];

? ? // 轉(zhuǎn)場效果-動(dòng)畫類型type 動(dòng)畫方向subtype

? ? NSIntegerindex =0;

? ? if(0== index)

? ? {

? ? ? ? animation.type=@"cube";

? ? ? ? animation.subtype = kCATransitionFromLeft;

? ? }

? ? elseif(1== index)

? ? {

? ? ? ? animation.type=@"moveIn";

? ? ? ? animation.subtype = kCATransitionFromLeft;

? ? }

? ? elseif(2== index)

? ? {

? ? ? ? animation.type=@"reveal";

? ? ? ? animation.subtype = kCATransitionFromLeft;

? ? }

? ? elseif(3== index)

? ? {

? ? ? ? animation.type=@"fade";

? ? ? ? animation.subtype = kCATransitionFromLeft;

? ? }

? ? elseif(4== index)

? ? {

? ? ? ? animation.type=@"pageCurl";

? ? ? ? animation.subtype = kCATransitionFromLeft;

? ? }

? ? elseif(5== index)

? ? {

? ? ? ? animation.type=@"pageUnCurl";

? ? ? ? animation.subtype = kCATransitionFromRight;

? ? }

? ? elseif(6== index)

? ? {

? ? ? ? animation.type=@"suckEffect";

? ? ? ? animation.subtype = kCATransitionFromLeft;

? ? }

? ? elseif(7== index)

? ? {

? ? ? ? animation.type=@"rippleEffect";

? ? ? ? animation.subtype = kCATransitionFromLeft;

? ? }

? ? elseif(8== index)

? ? {

? ? ? ? animation.type=@"oglFlip";

? ? ? ? animation.subtype = kCATransitionFromLeft;

? ? }

? ? elseif(9== index)

? ? {

? ? ? ? animation.type=@"rotate";

? ? ? ? animation.subtype=@"90cw";

? ? }

? ? elseif(10== index)

? ? {

? ? ? ? animation.type=@"push";

? ? ? ? animation.subtype = kCATransitionFromLeft;

? ? }

? ? elseif(11== index)

? ? {

? ? ? ? animation.type = @"cameraIrisHollowOpen";

? ? ? ? animation.subtype = kCATransitionFromLeft;

? ? }

? ? elseif(12== index)

? ? {

? ? ? ? animation.type = @"cameraIrisHollowClose";

? ? ? ? animation.subtype = kCATransitionFromLeft;

? ? }

? ? elseif(13== index)

? ? {

? ? ? ? animation.type=@"kCATransitionFade";

? ? ? ? animation.subtype = kCATransitionFromLeft;

? ? }

? ? elseif(14== index)

? ? {

? ? ? ? animation.type = @"kCATransitionMoveIn";

? ? ? ? animation.subtype = kCATransitionFromLeft;

? ? }

? ? elseif(15== index)

? ? {

? ? ? ? animation.type=@"kCATransitionPush";

? ? ? ? animation.subtype = kCATransitionFromLeft;

? ? }

? ? elseif(16== index)

? ? {

? ? ? ? animation.type = @"kCATransitionReveal";

? ? ? ? animation.subtype = kCATransitionFromLeft;

? ? }

? ? // 添加轉(zhuǎn)場動(dòng)畫到導(dǎo)航視圖控制上

? ? [self.navigationController.view.layer addAnimation:animation forKey:nil];


? ? TestViewController *vc = [[TestViewController alloc] init];

? ? [self.navigationController pushViewController:vc animated:YES];

轉(zhuǎn)場Demo參考地址:https://github.com/potato512/SYAnimation


2染服、UIView動(dòng)畫

UIKit自己封裝了一套動(dòng)畫api

@interfaceUIView(UIViewAnimation)

+ (void)beginAnimations:(nullable NSString *)animationID context:(nullable void*)context;? // additional context info passed to will start/did stop selectors. begin/commit can be nested

+ (void)commitAnimations;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // starts up any animations when the top level animation is commited

// no getters. if called outside animation block, these setters have no effect.

+ (void)setAnimationDelegate:(nullableid)delegate;? ? ? ? ? ? ? ? ? ? ? ? ? // default = nil

+ (void)setAnimationWillStartSelector:(nullable SEL)selector;? ? ? ? ? ? ? ? // default = NULL. -animationWillStart:(NSString *)animationID context:(void *)context

+ (void)setAnimationDidStopSelector:(nullable 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. used if repeat count is non-zero

+ (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState;? // default = NO. If 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;? // current limitation - only one per begin/commit block

+ (void)setAnimationsEnabled:(BOOL)enabled;? ? ? ? ? ? ? ? ? ? ? ? // ignore any attribute changes while set.

#if UIKIT_DEFINE_AS_PROPERTIES

@property(class, nonatomic, readonly) BOOL areAnimationsEnabled;

#else

+ (BOOL)areAnimationsEnabled;

#endif

+ (void)performWithoutAnimation:(void(NS_NOESCAPE^)(void))actionsWithoutAnimationNS_AVAILABLE_IOS(7_0);

#if UIKIT_DEFINE_AS_PROPERTIES

@property(class,nonatomic,readonly)NSTimeIntervalinheritedAnimationDurationNS_AVAILABLE_IOS(9_0);

#else

+ (NSTimeInterval)inheritedAnimationDuration NS_AVAILABLE_IOS(9_0);

#endif

@end

@interfaceUIView(UIViewAnimationWithBlocks)

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void(^)(void))animations completion:(void(^__nullable)(BOOLfinished))completionNS_AVAILABLE_IOS(4_0);

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void(^)(void))animations completion:(void(^__nullable)(BOOLfinished))completionNS_AVAILABLE_IOS(4_0);// delay = 0.0, options = 0

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

/* Performs `animations` using a timing curve described by the motion of a spring. When `dampingRatio` is 1, the animation will smoothly decelerate to its final model values without oscillating. Damping ratios less than 1 will oscillate more and more before coming to a complete stop. You can use the initial spring velocity to specify how fast the object at the end of the simulated spring was moving before it was attached. It's a unit coordinate system, where 1 is defined as travelling the total animation distance in a second. So if you're changing an object's position by 200pt in this animation, and you want the animation to behave as if the object was moving at 100pt/s before the animation started, you'd pass 0.5. You'll typically want to pass 0 for the velocity. */?

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void(^)(void))animations completion:(void(^__nullable)(BOOLfinished))completionNS_AVAILABLE_IOS(7_0);

+ (void)transitionWithView:(UIView*)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void(^__nullable)(void))animations completion:(void(^__nullable)(BOOLfinished))completionNS_AVAILABLE_IOS(4_0);

+ (void)transitionFromView:(UIView*)fromView toView:(UIView*)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void(^__nullable)(BOOLfinished))completionNS_AVAILABLE_IOS(4_0);// toView added to fromView.superview, fromView removed from its superview

/* Performs the requested system-provided animation on one or more views. Specify addtional animations in the parallelAnimations block. These additional animations will run alongside the system animation with the same timing and duration that the system animation defines/inherits. Additional animations should not modify properties of the view on which the system animation is being performed. Not all system animations honor all available options.

?*/

+ (void)performSystemAnimation:(UISystemAnimation)animation onViews:(NSArray<__kindofUIView*> *)views options:(UIViewAnimationOptions)options animations:(void(^__nullable)(void))parallelAnimations completion:(void(^__nullable)(BOOLfinished))completionNS_AVAILABLE_IOS(7_0);

@end

@interfaceUIView (UIViewKeyframeAnimations)

+ (void)animateKeyframesWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewKeyframeAnimationOptions)options animations:(void(^)(void))animations completion:(void(^__nullable)(BOOLfinished))completionNS_AVAILABLE_IOS(7_0);

+ (void)addKeyframeWithRelativeStartTime:(double)frameStartTime relativeDuration:(double)frameDuration animations:(void (^)(void))animations NS_AVAILABLE_IOS(7_0); // start time and duration are values between 0.0 and 1.0 specifying time and duration relative to the overall time of the keyframe animation

@end

例子

1.普通方式

[UIView beginAnimations:@"firstAnimation" context:nil];

//動(dòng)畫持續(xù)時(shí)間

? ? [UIView setAnimationDuration:2.f];

? ? //動(dòng)畫的代理對象

? ? [UIView setAnimationDelegate:self];

? ? //設(shè)置動(dòng)畫將開始時(shí)代理對象執(zhí)行的SEL

? ? [UIViewsetAnimationWillStartSelector:@selector(startAnimation:)];

? ? [UIViewsetAnimationDidStopSelector:@selector(stopAnimation:)];

? ? //設(shè)置動(dòng)畫延遲執(zhí)行的時(shí)間

? ? [UIView setAnimationDelay:0.f];

? ? //設(shè)置動(dòng)畫的重復(fù)次數(shù)

? ? [UIView setAnimationRepeatCount:4];

? ? //設(shè)置開始動(dòng)畫的時(shí)間,默認(rèn)是now

? ? [UIView setAnimationStartDate:[NSDate date]];


? ? //設(shè)置動(dòng)畫的曲線

? ? /*

?? ? ? ? UIViewAnimationCurve的枚舉值:

?? ? ? ? UIViewAnimationCurveEaseInOut,? ? ? ? // 慢進(jìn)慢出(默認(rèn)值)

?? ? ? ? UIViewAnimationCurveEaseIn,? ? ? ? ? ? // 慢進(jìn)

?? ? ? ? UIViewAnimationCurveEaseOut,? ? ? ? ? // 慢出

?? ? ? ? UIViewAnimationCurveLinear? ? ? ? ? ? // 勻速

?? ? ? ? */

? ? [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];


? ? //設(shè)置是否從當(dāng)前狀態(tài)開始播放動(dòng)畫

? ? /*假設(shè)上一個(gè)動(dòng)畫正在播放别洪,且尚未播放完畢,我們將要進(jìn)行一個(gè)新的動(dòng)畫:

?? ? ? ? 當(dāng)為YES時(shí):動(dòng)畫將從上一個(gè)動(dòng)畫所在的狀態(tài)開始播放

?? ? ? ? 當(dāng)為NO時(shí):動(dòng)畫將從上一個(gè)動(dòng)畫所指定的最終狀態(tài)開始播放(此時(shí)上一個(gè)動(dòng)畫馬上結(jié)束)*/

? ? [UIView setAnimationBeginsFromCurrentState:YES];


? ? //設(shè)置動(dòng)畫是否繼續(xù)執(zhí)行相反的動(dòng)畫

? ? [UIView setAnimationRepeatAutoreverses:NO];

? ? //是否使用動(dòng)畫效果(對象屬性依然會(huì)被改變柳刮,只是沒有動(dòng)畫效果)YES使用挖垛;NO禁用

? ? [UIView setAnimationsEnabled:YES];


? ? //設(shè)置視圖的過渡效果

? ? /* 第一個(gè)參數(shù):UIViewAnimationTransition的枚舉值如下

?? ? ? ? UIViewAnimationTransitionNone,? ? ? ? ? ? ? //不使用動(dòng)畫

?? ? ? ? UIViewAnimationTransitionFlipFromLeft,? ? ? //從左向右旋轉(zhuǎn)翻頁

?? ? ? ? UIViewAnimationTransitionFlipFromRight,? ? //從右向左旋轉(zhuǎn)翻頁

?? ? ? ? UIViewAnimationTransitionCurlUp,? ? ? ? ? ? //從下往上卷曲翻頁

?? ? ? ? UIViewAnimationTransitionCurlDown,? ? ? ? ? //從上往下卷曲翻頁

?? ? ? ? 第二個(gè)參數(shù):需要過渡效果的View

?? ? ? ? 第三個(gè)參數(shù):是否使用視圖緩存,YES:視圖在開始和結(jié)束時(shí)渲染一次秉颗;NO:視圖在每一幀都渲染*/

? ? [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:[[UIView alloc] initWithFrame:CGRectZero] cache:YES];

? ? 結(jié)束動(dòng)畫方法:????[UIView commitAnimations];


2.Block方式

[UIView animateWithDuration:1.0

? ? ? ? ? ? ? ? ? ? ? ? ? delay:2.0

? ? ? ? ? ? ? ? ? ? ? ? options:UIViewAnimationOptionCurveLinear

?? ? ? ? ? ? ? ? ? ? animations:^{

? ? ? ? //執(zhí)行的動(dòng)畫代碼

? ? }

?? ? ? ? ? ? ? ? ? ? completion:^(BOOLfinished) {

? ? ? ? //動(dòng)畫執(zhí)行完成之后的回調(diào)

? ? }];


spring動(dòng)畫

/**

?? ? *? 第一個(gè)參數(shù):動(dòng)畫持續(xù)時(shí)間

?? ? *? 第二個(gè)參數(shù):動(dòng)畫延時(shí)開始的時(shí)間

?? ? *? 第三個(gè)參數(shù):Spring動(dòng)畫痢毒,震動(dòng)效果,范圍0~1蚕甥,數(shù)值越小震動(dòng)效果越明顯

?? ? *? 第四個(gè)參數(shù):初始速度哪替,數(shù)值越大初始速度越快

?? ? *? 第五個(gè)參數(shù):動(dòng)畫的過渡效果

?? ? *? 第六個(gè)參數(shù):執(zhí)行的動(dòng)畫代碼

?? ? *? 第七個(gè)參數(shù):動(dòng)畫執(zhí)行完成之后的回調(diào)

?? ? */

? ? [UIView animateWithDuration:1.0

? ? ? ? ? ? ? ? ? ? ? ? ? delay:2.0

?? ? ? ? usingSpringWithDamping:0.5

? ? ? ? ? initialSpringVelocity:100.f

? ? ? ? ? ? ? ? ? ? ? ? options:UIViewAnimationOptionCurveLinear

?? ? ? ? ? ? ? ? ? ? animations:^{

?? ? ? ? ? ? ? ? ? ? }

?? ? ? ? ? ? ? ? ? ? completion:^(BOOLfinished) {

?? ? ? ? ? ? ? ? ? ? }];


3.轉(zhuǎn)場動(dòng)畫

? ? //單視圖轉(zhuǎn)場動(dòng)畫

? ? + (void)transitionWithView:(UIView *)view

duration:(NSTimeInterval)duration

options:(UIViewAnimationOptions)options

animations:(void(^__nullable)(void))animations

completion:(void(^__nullable)(BOOLfinished))completion

? ? //雙視圖轉(zhuǎn)場動(dòng)畫

? ? + (void)transitionFromView:(UIView *)fromView

toView:(UIView *)toView

duration:(NSTimeInterval)duration

options:(UIViewAnimationOptions)options

completion:(void(^__nullable)(BOOLfinished))completion

這兩個(gè)都是轉(zhuǎn)場動(dòng)畫,不同的是第一個(gè)是單視圖轉(zhuǎn)場,第二個(gè)是雙視圖轉(zhuǎn)場.不過需要注意的是:單視圖轉(zhuǎn)場動(dòng)畫只能用作屬性動(dòng)畫做不到的轉(zhuǎn)場效果,比如屬性動(dòng)畫不能給UIImageview的image賦值操作做動(dòng)畫效果等.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市梢灭,隨后出現(xiàn)的幾起案子夷家,更是在濱河造成了極大的恐慌,老刑警劉巖敏释,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件库快,死亡現(xiàn)場離奇詭異,居然都是意外死亡钥顽,警方通過查閱死者的電腦和手機(jī)义屏,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蜂大,“玉大人闽铐,你說我怎么就攤上這事∧唐郑” “怎么了兄墅?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長澳叉。 經(jīng)常有香客問我隙咸,道長沐悦,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任五督,我火速辦了婚禮藏否,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘充包。我一直安慰自己副签,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布基矮。 她就那樣靜靜地躺著淆储,像睡著了一般。 火紅的嫁衣襯著肌膚如雪家浇。 梳的紋絲不亂的頭發(fā)上遏考,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天,我揣著相機(jī)與錄音蓝谨,去河邊找鬼。 笑死青团,一個(gè)胖子當(dāng)著我的面吹牛譬巫,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播督笆,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼芦昔,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了娃肿?” 一聲冷哼從身側(cè)響起咕缎,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎料扰,沒想到半個(gè)月后凭豪,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡晒杈,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年嫂伞,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片拯钻。...
    茶點(diǎn)故事閱讀 39,696評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡帖努,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出粪般,到底是詐尸還是另有隱情拼余,我是刑警寧澤,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布亩歹,位于F島的核電站匙监,受9級特大地震影響凡橱,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜舅柜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一梭纹、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧致份,春花似錦变抽、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至滔蝉,卻和暖如春击儡,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背蝠引。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工阳谍, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人螃概。 一個(gè)月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓矫夯,卻偏偏與公主長得像,于是被迫代替她去往敵國和親吊洼。 傳聞我的和親對象是個(gè)殘疾皇子训貌,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內(nèi)容