1 //初始化方式 CABasicAnimation * cabase=[CABasicAnimation animation]; 2 //通過keyPath設置需要實現動畫的屬性,此處設為bounds cabase.keyPath=@"bounds"; 3 //通過toValue設置動畫結束時候的狀態 cabase.toValue=[NSValue valueWithCGRect:CGRectMake(0, 0, 10, 100)]; //通過byValue設置每次改變的范圍 cabase.byValue=[NSValue valueWithCGRect:CGRectMake(0, 0, 10, 100)]; //設置開始時候的狀態 cabase.fromValue=[NSValue valueWithCGPoint:CGPointMake(0, 0)]; 4 //設置動畫持續的時間 cabase.duration=2; //保存動畫 cabase.fillMode=kCAFillModeForwards; //保存設置不取消 cabase.removedOnCompletion=NO; [_layer addAnimation:cabase forKey:nil];
案例:通過基礎動畫實現仿射變換動畫
CABasicAnimation * cabase=[CABasicAnimation animation]; cabase.keyPath=@"transform"; cabase.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 2, 1)]; cabase.duration=2; cabase.fillMode=kCAFillModeForwards; cabase.removedOnCompletion=NO; [_layer addAnimation:cabase forKey:nil];二、關鍵幀動畫
1 //初始化方式 CAKeyframeAnimation * keyfram=[CAKeyframeAnimation animation]; 2 //通過keyPath設置需要實現動畫的屬性,此處設為position keyfram.keyPath=@"position"; 3 //設置動畫的需要經過的點 CGPoint p1=CGPointZero; CGPoint p2=CGPointMake(150, 0); CGPoint p3=CGPointMake(150, 150); CGPoint p4=CGPointMake(0, 150); CGPoint p5=CGPointZero; NSValue * v1=[NSValue valueWithCGPoint:p1]; NSValue * v2=[NSValue valueWithCGPoint:p2]; NSValue * v3=[NSValue valueWithCGPoint:p3]; NSValue * v4=[NSValue valueWithCGPoint:p4]; NSValue * v5=[NSValue valueWithCGPoint:p5]; 4 //將對應的值添加到動畫並且設置動畫保留 keyfram.values=@[v1,v2,v3,v4,v5]; keyfram.duration=1; keyfram.fillMode=kCAFillModeForwards; keyfram.removedOnCompletion=NO; [_layer addAnimation:keyfram forKey:nil];
案例:通過關鍵幀動畫實現圖片搖擺
CAKeyframeAnimation * anima=[CAKeyframeAnimation animation]; //通過設置放射變換的角度來實現 anima.keyPath=@"transform.rotation"; float p1=4/180.0*M_PI; anima.duration=0.2; anima.values=@[@(-p1),@(p1),@(-p1)]; anima.fillMode=kCAFillModeForwards; anima.removedOnCompletion=NO; anima.repeatCount=MAXFLOAT; [_layer addAnimation:anima forKey:nil]; _layer.transform=CATransform3DMakeRotation(M_PI, 0, 0, 0);
三、轉場動畫
1 //初始化方式 CATransition * tran=[CATransition animation]; 2 //設置動畫效果 tran.type=@"rippleEffect"; //常用效果 kCATransitionFade kCATransitionMoveIn kCATransitionPush kCATransitionReveal 3 //設置動畫方向 tran.subtype=kCATransitionFromLeft; //動畫方向 kCATransitionFromRight kCATransitionFromLeft kCATransitionFromTop kCATransitionFromBottom 4 //設置動畫保留以及動畫時長 tran.fillMode=kCAFillModeForwards; tran.removedOnCompletion=NO; tran.duration=1; [self.myImageView.layer addAnimation:tran forKey:nil];四、UIView封裝動畫
UIKit直接將動畫集成到UIView類中,當內部的一些屬性發生改變時,UIView將為這些改變提供動畫支持。執行動畫所需要的工作由UIView類自動完成,但仍要在希望執行動畫時通知視圖,為此需要將改變屬性的代碼放在[UIViewbeginAnimations:nil context:nil]和[UIView commitAnimations]之間
1、常見方法解析:
//設置動畫代理 + (void)setAnimationDelegate:(id)delegate //設置當動畫即將開始時,執行delegate對象的selector,並且把beginAnimations:context:中傳入的參數傳進selector +(void)setAnimationWillStartSelector:(SEL)selector //設置動畫結束時調用方法 + (void)setAnimationDidStopSelector:(SEL)selector //設置動畫持續時間 +(void)setAnimationDuration:(NSTimeInterval)duration //設置動畫延遲 + (void)setAnimationDelay:(NSTimeInterval)delay //設置動畫開始時間 + (void)setAnimationStartDate:(NSDate *)startDate //設置動畫節奏 + (void)setAnimationCurve:(UIViewAnimationCurve)curve //設置動畫重復次數 + (void)setAnimationRepeatCount:(float)repeatCount //如果設置為YES,代表動畫每次重復執行的效果會跟上一次相反 +(void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses // 設置視圖view的過渡效果, transition指定過渡類型, cache設置YES代表使用視圖緩存,性能較好 + (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache
2、案例
//旋轉動畫 [UIView beginAnimations:@"roate" context:nil]; [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; [UIView setAnimationDuration:1.5]; [UIView setAnimationDelegate:self]; _view.transform=CGAffineTransformRotate(_view.transform, M_PI_2); [UIView setAnimationDidStopSelector:@selector(endAnimate)]; [UIView commitAnimations]; //轉場動畫 [UIView beginAnimations:@"transition" context:nil]; [UIView setAnimationCurve:UIViewAnimationCurveLinear]; [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:_mainView cache:YES]; [UIView setAnimationDuration:1.5]; NSInteger index1=[_mainView.subviews indexOfObject:_view]; NSInteger index2=[_mainView.subviews indexOfObject:_view2]; [_mainView exchangeSubviewAtIndex:index1 withSubviewAtIndex:index2]; [UIView commitAnimations];