动画分为两种UIView层动画 CALayer层动画。
UIView层动画分为两种类型:
1.属性动画
2.过渡动画
属性动画之block
[UIView animateWithDuration:1 animations:^{
self.AView.alpha = 0.3;
}];
[UIView animateWithDuration:3 animations:^{
self.AView.transform = CGAffineTransformMakeRotation(M_PI_4);
self.AView.backgroundColor = [UIColor grayColor];
self.AView.bounds = CGRectMake(0, 0, 100, 100);
} completion:^(BOOL finished) {
NSLog(@"结束");
}];
[UIView animateWithDuration:2 delay:2 options:UIViewAnimationOptionAllowUserInteraction animations:^{
self.AView.transform = CGAffineTransformMakeRotation(M_PI_4);
self.AView.backgroundColor = [UIColor grayColor];
self.AView.bounds = CGRectMake(0, 0, 100, 100);
} completion:^(BOOL finished) {
self.AView.backgroundColor = [UIColor redColor];
}];
[UIView animateWithDuration:2 delay:2 usingSpringWithDamping:0.1 initialSpringVelocity:2 options:UIViewAnimationOptionAllowUserInteraction animations:^{
self.AView.transform = CGAffineTransformMakeRotation(M_PI_4);
self.AView.backgroundColor = [UIColor grayColor];
self.AView.bounds = CGRectMake(0, 0, 100, 100);
} completion:^(BOOL finished) {
}];
过渡动画之 普通翻页效果
UIView *containerView =self.view.superview;
[UIViewtransitionWithView:containerView duration:2options:UIViewAnimationOptionTransitionCurlUpanimations:^{
[self.viewremoveFromSuperview] ;
[containerView addSubview:XVC.view];
} completion:^(BOOL finished) {
}];
CALayer层动画
CAlayer层在做属性动画时并不是所有的属性都可以做动画 只有属性后面带Animatable的才可以做动画。
CABasicAnimation *basicAnimation = [CABasicAnimationanimationWithKeyPath:@"backgroundColor"];
basicAnimation.duration =2;
basicAnimation.fromValue = (__bridgeid)([UIColoryellowColor].CGColor);
basicAnimation.toValue = (__bridgeid)([UIColorredColor].CGColor);
[self.AView.layeraddAnimation:basicAnimation forKey:@"BS"];
类似于QQ的弹框的抖动效果
CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimationanimationWithKeyPath:@"position.x"];
CGFloat x =self.AView.center.x;
CGFloat leftX = x -10;
CGFloat rightX = x +10;
NSNumber *X = [NSNumbernumberWithFloat:x];
NSNumber *leftx = [NSNumbernumberWithFloat:leftX];
NSNumber *righty = [NSNumbernumberWithFloat:rightX];
NSArray *values =@[X,leftx,X,righty,X,leftx,X,righty,X];
keyFrameAnimation.values = values;
[self.AView.layeraddAnimation:keyFrameAnimation forKey:@"keyFrame"];
CATransition 转场动画具有方块翻页效果
CATransition *catransition = [CATransitionanimation];
// 1获取self.view.superView
UIView *view =self.view.superview;
catransition.type =@"cube";
catransition.duration =3;
catransition.startProgress =0.3; // 开始动画
catransition.endProgress =0.8; // 结束动画
// 2给self.view.superView添加动画
[view.layeraddAnimation:catransition forKey:@"catransition"];
// 3移除原来的View,添加新的View
[self.viewremoveFromSuperview];
XViewController *XVC = [self.storyboardinstantiateViewControllerWithIdentifier:@"X"];
[view addSubview:XVC.view];
CAAnimationGroup可融合前面的动画 做出多动画效果
CAAnimationGroup *group = [CAAnimationGroupanimation];
CABasicAnimation *basicAnimation = [CABasicAnimationanimationWithKeyPath:@"backgroundColor"];
basicAnimation.duration =2;
basicAnimation.fromValue = (__bridgeid)([UIColoryellowColor].CGColor);
CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimationanimationWithKeyPath:@"position.x"];
CGFloat x =self.AView.center.x;
CGFloat leftX = x -10;
CGFloat rightX = x +10;
NSNumber *X = [NSNumbernumberWithFloat:x];
NSNumber *leftx = [NSNumbernumberWithFloat:leftX];
NSNumber *righty = [NSNumbernumberWithFloat:rightX];
NSArray *values =@[X ,leftx,X,righty,X,leftx,X,righty,X];
keyFrameAnimation.values = values;
group.duration =2;
group.animations =@[basicAnimation,keyFrameAnimation];
[self.AView.layeraddAnimation:group forKey:@"group"];