IOS之 基本动画原理

   IOS动画分为属性动画和过渡动画。ios4.0之前

属性动画

内容和设置主要放在方括号中既:如下

[UIView beginAnimations:@"move" context:@"aa"];

中间部分设置动画内容和属性

[UIView commitAnimations];

详见代码如下

[objc] view plaincopyprint?
  1.     [UIView beginAnimations:@"move" context:@"aa"];  
  2.     [UIView setAnimationDuration:1];//设置动画时间  
  3.     CGPoint point = self.View1.center;//  
  4.     [UIView setAnimationDelegate:self];//设置代理  
  5.     [UIView setAnimationWillStartSelector:@selector(startAnimation:)];//动画开始之前  
  6.     [UIView setAnimationDidStopSelector:@selector(stopAnition:)];//动画结束之后  
  7.       
  8. //    [UIView setAnimationRepeatAutoreverses:YES];  
  9.     self.View1.center = CGPointMake(point.x+1, point.y);  
  10. //    self.View1.backgroundColor = [UIColor orangeColor];  
  11.     [UIView commitAnimations];  

一下是能够设置属性动画的属性:

The following properties of the UIView class are animatable:

  @property frame
  @property bounds
  @property center
  @property transform
  @property alpha
  @property backgroundColor
  @property contentStretch

过渡动画:

[objc] view plaincopyprint?
  1. [UIView beginAnimations:@"move" context:@"text"];  
  2. [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.View3 cache:YES];  
  3.   
  4.   
  5. [UIView commitAnimations];  

能够设置的属性:

typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
    UIViewAnimationTransitionNone,
    UIViewAnimationTransitionFlipFromLeft,
    UIViewAnimationTransitionFlipFromRight,
    UIViewAnimationTransitionCurlUp,
    UIViewAnimationTransitionCurlDown,
};


4.0之后修改为调用block

Animating Views with Block Objects

//属性动画

+ animateWithDuration:delay:options:animations:completion: 
+ animateWithDuration:animations:completion:
+ animateWithDuration:animations:

//过渡动画

+ transitionWithView:duration:options:animations:completion:
+ transitionFromView:toView:duration:options:completion:

//关键帧动画
+ animateKeyframesWithDuration:delay:options:animations:completion:
+ addKeyframeWithRelativeStartTime:relativeDuration:animations:

//系统动画
+ performSystemAnimation:onViews:options:animations:completion:

//未知,还为测试
+ animateWithDuration:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion:
+ performWithoutAnimation:



CAAnimation动画

代码如下:具体过程。

[objc] view plaincopyprint?
  1. //CABasicAnimation 动画  
  2.   
  3. CABasicAnimation* baseAnimation = [CABasicAnimation animationWithKeyPath:@"center"];  
  4. baseAnimation.fromValue = [NSValue valueWithCGRect:CGRectMake(0000)];  
  5.   
  6. baseAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(400100)] ;  
  7. baseAnimation.duration = 3;  
  8. [self.View2.layer addAnimation:baseAnimation forKey:@"baseAnimation"];  
  9. // CAKeyframeAnimation 动画  
  10. CAKeyframeAnimation* keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];  
  11. keyAnimation.values =[NSArray arrayWithObjects:  
  12.                       [NSValue valueWithCGPoint:CGPointMake(self.View2.center.x+5self.View2.center.y)],  
  13.                       [NSValue valueWithCGPoint:CGPointMake(self.View2.center.xself.View2.center.y)],  
  14.                       [NSValue valueWithCGPoint:CGPointMake(self.View2.center.x-5self.View2.center.y)],  
  15.                       [NSValue valueWithCGPoint:CGPointMake(self.View2.center.xself.View2.center.y)],  
  16.                       [NSValue valueWithCGPoint:CGPointMake(self.View2.center.x+5self.View2.center.y)],  
  17.                       [NSValue valueWithCGPoint:CGPointMake(self.View2.center.xself.View2.center.y)],  
  18.                       nil nil];  
  19. keyAnimation.keyTimes = @[[NSNumber numberWithFloat:.1],  
  20.                           [NSNumber numberWithFloat:.2],  
  21.                           [NSNumber numberWithFloat:.3],  
  22.                           [NSNumber numberWithFloat:.4],  
  23.                           [NSNumber numberWithFloat:.5],  
  24.                           [NSNumber numberWithFloat:1]];//设置时间 ,百分比  
  25. keyAnimation.calculationMode = kCAAnimationDiscrete;  
  26. [self.View2.layer addAnimation:keyAnimation forKey:@"position"];  
  27. //CATransition 动画  
  28.  CATransition* transiton = [CATransition animation];  
  29. transiton.startProgress = 0;  
  30. transiton.endProgress = 1.0;  
  31. transiton.duration = 3;  
  32. transiton.type = kCATransitionReveal;//根据不同值显示不同动画  
  33. transiton.subtype = kCATransitionFromRight;  
  34. [self.view.layer addAnimation:transiton forKey:@"transtion"];  



        keyFrameAnimation.fillMode = kCAFillModeForwards;//确定动画 执行完毕之后模式(删除or保留)
        keyFrameAnimation.removedOnCompletion = NO;//删除动画设置为no


你可能感兴趣的:(IOS之 基本动画原理)