IOS动画分为属性动画和过渡动画。ios4.0之前
属性动画
内容和设置主要放在方括号中既:如下
[UIView beginAnimations:@"move" context:@"aa"];
中间部分设置动画内容和属性
[UIView commitAnimations];
详见代码如下
- [UIView beginAnimations:@"move" context:@"aa"];
- [UIView setAnimationDuration:1];
- CGPoint point = self.View1.center;
- [UIView setAnimationDelegate:self];
- [UIView setAnimationWillStartSelector:@selector(startAnimation:)];
- [UIView setAnimationDidStopSelector:@selector(stopAnition:)];
-
-
- self.View1.center = CGPointMake(point.x+1, point.y);
-
- [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
过渡动画:
- [UIView beginAnimations:@"move" context:@"text"];
- [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.View3 cache:YES];
-
-
- [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动画
代码如下:具体过程。
-
-
- CABasicAnimation* baseAnimation = [CABasicAnimation animationWithKeyPath:@"center"];
- baseAnimation.fromValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 0, 0)];
-
- baseAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(400, 100)] ;
- baseAnimation.duration = 3;
- [self.View2.layer addAnimation:baseAnimation forKey:@"baseAnimation"];
-
- CAKeyframeAnimation* keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
- keyAnimation.values =[NSArray arrayWithObjects:
- [NSValue valueWithCGPoint:CGPointMake(self.View2.center.x+5, self.View2.center.y)],
- [NSValue valueWithCGPoint:CGPointMake(self.View2.center.x, self.View2.center.y)],
- [NSValue valueWithCGPoint:CGPointMake(self.View2.center.x-5, self.View2.center.y)],
- [NSValue valueWithCGPoint:CGPointMake(self.View2.center.x, self.View2.center.y)],
- [NSValue valueWithCGPoint:CGPointMake(self.View2.center.x+5, self.View2.center.y)],
- [NSValue valueWithCGPoint:CGPointMake(self.View2.center.x, self.View2.center.y)],
- nil nil];
- keyAnimation.keyTimes = @[[NSNumber numberWithFloat:.1],
- [NSNumber numberWithFloat:.2],
- [NSNumber numberWithFloat:.3],
- [NSNumber numberWithFloat:.4],
- [NSNumber numberWithFloat:.5],
- [NSNumber numberWithFloat:1]];
- keyAnimation.calculationMode = kCAAnimationDiscrete;
- [self.View2.layer addAnimation:keyAnimation forKey:@"position"];
-
- CATransition* transiton = [CATransition animation];
- transiton.startProgress = 0;
- transiton.endProgress = 1.0;
- transiton.duration = 3;
- transiton.type = kCATransitionReveal;
- transiton.subtype = kCATransitionFromRight;
- [self.view.layer addAnimation:transiton forKey:@"transtion"];
keyFrameAnimation.fillMode = kCAFillModeForwards;//确定动画 执行完毕之后模式(删除or保留)
keyFrameAnimation.removedOnCompletion = NO;//删除动画设置为no