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:)];//动画结束之后// [UIView setAnimationRepeatAutoreverses:YES];self.View1.center = CGPointMake(point.x+1, point.y);// self.View1.backgroundColor = [UIColor orangeColor];[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 动画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 动画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];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 动画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
如果觉得《IOS之 基本动画原理》对你有帮助,请点赞、收藏,并留下你的观点哦!