IOS中的動畫——Core Animation,iosanimation

來源:互聯網
上載者:User

IOS中的動畫——Core Animation,iosanimation

一、基礎動畫 CABasicAnimation
1  //初始化方式    CABasicAnimation * cabase=[CABasicAnimation animation];2  //通過keyPath設定需要實現動畫的屬性,此處設為boundscabase.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設定需要實現動畫的屬性,此處設為positionkeyfram.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";//常用效果kCATransitionFadekCATransitionMoveInkCATransitionPushkCATransitionReveal3 //設定動畫方向tran.subtype=kCATransitionFromLeft;//動畫方向kCATransitionFromRightkCATransitionFromLeftkCATransitionFromTopkCATransitionFromBottom4 //設定動畫保留以及動畫時間長度  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];

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.