iOS動畫實現總結

來源:互聯網
上載者:User

iOS動畫實現總結
在iOS中,動畫實現方向有兩種,一種是操作UIView的animation方法,另外一種就是核心動畫,但到iOS7中,UIView又跟核心動畫牽扯在一起。 方式一(利用核心動畫添加動畫)核心動畫的層次關係   轉場動畫(CATransition) 用於做情境的轉換動畫,能偶為層提供移出螢幕和一如螢幕的動畫效果。UINavigationController就是通過CATransition實現了講控制器的師徒推入螢幕的動畫效果。常用屬性type動畫過度類型subtype:動畫過度方向startProgress:動畫起點(在整體的百分比)(可用的值從0到1,在動畫中起點或終點的逗留時間,開始的時間一定要比結束的時間小,下同)endProgress:動畫終點(在整體動畫的百分比) 

CABasicAnimation *baseAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];  baseAnimation.fromValue = [NSValue valueWithCGRect:CGRectMake(20, 20, 100, 100)];  baseAnimation.toValue = [NSValue valueWithCGRect:CGRectMake(100, 100, 200, 200)];  baseAnimation.duration = 2.0;  baseAnimation.removedOnCompletion = NO;  baseAnimation.fillMode = kCAFillModeForwards;  baseAnimation.repeatCount = MAXFLOAT;  [self.myView.layer addAnimation:baseAnimation forKey:nil];

 

 基本動畫(CABasicAnimation),是CAPropertyAnimation的子類,一個動畫可控制一個屬性的變化,變化值只能是兩個值中變化,可以在fromValue和toValue兩個值中設定  
CABasicAnimation *baseAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];  baseAnimation.fromValue = [NSValue valueWithCGRect:CGRectMake(20, 20, 100, 100)];  baseAnimation.toValue = [NSValue valueWithCGRect:CGRectMake(100, 100, 200, 200)];  baseAnimation.duration = 2.0;  baseAnimation.removedOnCompletion = NO;  baseAnimation.fillMode = kCAFillModeForwards;  baseAnimation.repeatCount = MAXFLOAT;  [self.myView.layer addAnimation:baseAnimation forKey:nil];

 

  幀動畫(CAKeyframeAnimation),幀動畫也是CAPropertyAnimation的子類,所以也是控制一個view的屬性做動畫,與CABaseAnimation不同的是,CAKeyFrameAnimation可以添加多個主要畫面格,而CABaseAnimation可以看做是兩個主要畫面格的幀動畫,我們可以好好利用幀動畫的主要畫面格來做比較有趣的動畫,如泡泡效果。  
   CAAnimationGroup *group = [[CAAnimationGroup alloc] init];//    位移    CAKeyframeAnimation *positionAnima = [CAKeyframeAnimation animationWithKeyPath:@"position"];    positionAnima.calculationMode = kCAAnimationCubicPaced;    positionAnima.duration = 5;    positionAnima.fillMode = kCAFillModeForwards;    positionAnima.removedOnCompletion = NO;    positionAnima.repeatCount = MAXFLOAT;    positionAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];//    添加移動路徑    CGMutablePathRef curvedPath = CGPathCreateMutable();    CGRect circleContainer = CGRectInset(myView.frame, myView.frame.size.width / 2 - 3, myView.frame.size.height / 2 - 3);    CGPathAddEllipseInRect(curvedPath, NULL, circleContainer);    positionAnima.path = curvedPath;    CGPathRelease(curvedPath);    [myView.layer addAnimation:positionAnima forKey:nil];//    縮放X    CAKeyframeAnimation *scaleX = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale.x"];    scaleX.duration = 1.0;    scaleX.values = @[@1.0,@1.1,@1.0];    scaleX.keyTimes = @[@0.0,@0.5,@1.0];    scaleX.repeatCount = MAXFLOAT;    scaleX.autoreverses = YES;    scaleX.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];    [myView.layer addAnimation:scaleX forKey:nil];//    縮放Y    CAKeyframeAnimation *scaleY = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale.y"];    scaleY.duration = 1.5;    scaleY.values = @[@1.0,@1.1,@1.0];    scaleY.keyTimes = @[@0.0,@0.5,@1.0];    scaleY.repeatCount = MAXFLOAT;    scaleY.autoreverses = YES;    group.animations = @[positionAnima,scaleX,scaleY];    scaleY.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];    [myView.layer addAnimation:scaleY forKey:nil];

 

運行效果 由於本人的電腦是黑蘋果,所以將就一下啦,哈哈,白蘋果應該不會這樣的。 動畫組(CAAnimationGroup)CAAnimation的子類,可以儲存一組動畫對象,講CAAnimationGroup對象加入層後,組中所有動畫可以同時運行,所以,當我們需要做多個動畫並且執行的時間不一樣的時候,動畫組就不適用。例如上面的泡泡效果。group.animations = [裡面放動畫對象];  方式二(利用UIView添加動畫)UIView動畫(手碼)添加單個動畫 
[UIView beginAnimations:nil context:nil];[UIView setAnimationDuration:4];CGPoint point = self.myView.center;point.y += 150;[self.myView setCenter:point];[UIView commitAnimations];

 

  添加多個動畫 
  [UIView beginAnimations:nil context:nil];    [UIView setAnimationDuration:4];    CGPoint point = self.myView.center;    point.y += 150;    [self.myView setCenter:point];    [UIView commitAnimations];    [UIView beginAnimations:nil context:nil];    [UIView setAnimationDuration:4];    [self.myView setAlpha:0.1];    [UIView commitAnimations];

 

 UIView動畫(Block)[UIView animateWithDuration:4 animations:^{        CGPoint point = self.myView.center;        point.y += 150;        [self.myView setCenter:point];    }];  UIView動畫(Block幀動畫),從iOS7開啟,蘋果提供了比較便捷的方法來調用幀動畫,不用建立幀動畫執行個體,直接對layer的屬性進行控制。 
[UIView animateKeyframesWithDuration:0.5 delay:1 options:UIViewKeyframeAnimationOptionAutoreverse animations:^{        self.view.bounds = CGRectMake(30, 30, 30, 30);    } completion:^(BOOL finished) {    }];

 

UIView轉場動畫。+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion  這個方法應該不好理解,簡單來說,這個方法調用完畢後,相當於執行了兩句代碼,  
// 添加toView到父視圖[fromView.superview addSubview:toView];// 把fromView從父視圖中移除[fromView.superview removeFromSuperview];- duration:動畫的期間- duration:動畫的期間- options:轉場動畫的類型- animations:將改變視圖屬性的代碼放在這個block中- completion:動畫結束後,會自動調用這個block

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.