iOS开发UI篇—核心动画(转场动画和组动画)
- - (void)demoAnimationGroup
- {
- static NSString * const kCAPostionKeyPath = @"position";
- static NSString * const kCAOpacityKeyPath = @"opacity";
- static NSString * const kCARotationKeyPath = @"transform.rotation";
- static NSString * const kCAScaleKeyPath = @"transform.scale";
- UIBezierPath *path = [UIBezierPath bezierPath];
- [path moveToPoint:_demoView.layer.position];
- [path addCurveToPoint:CGPointMake(260, 400) controlPoint1:CGPointMake(0, 460) controlPoint2:CGPointMake(320, 0)];
- //路径动画
- CAKeyframeAnimation *posAnimation = [CAKeyframeAnimation animationWithKeyPath:kCAPostionKeyPath];
- posAnimation.path = path.CGPath;
- posAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
- //透明度动画
- CABasicAnimation *opaAnimation = [CABasicAnimation animationWithKeyPath:kCAOpacityKeyPath];
- opaAnimation.duration = 2.0f;
- opaAnimation.toValue = @(0.3f);
- opaAnimation.autoreverses = YES;
- //旋转动画
- CABasicAnimation *rotAnimation = [CABasicAnimation animationWithKeyPath:kCARotationKeyPath];
- rotAnimation.duration = 2.0f;
- rotAnimation.toValue = @(M_PI);
- rotAnimation.autoreverses = YES;
- //缩放动画
- CABasicAnimation *scaAnmaiton = [CABasicAnimation animationWithKeyPath:kCAScaleKeyPath];
- scaAnmaiton.duration = 2.0f;
- scaAnmaiton.toValue = @(1.5f);
- scaAnmaiton.autoreverses = YES;
- //设置动画组
- CAAnimationGroup *group = [CAAnimationGroup animation];
- group.animations = @[posAnimation, opaAnimation, rotAnimation, scaAnmaiton];
- group.duration = 4.0f;
- group.removedOnCompletion = NO;
- group.fillMode = kCAFillModeForwards;
- [_demoView.layer addAnimation:group forKey:nil];
- }
这个动画组对demoView的layer加入了一些同时进行的动画,比如双控制点的贝塞尔路径,透明度的渐隐渐明,旋转以及缩放等。
CATransition
- - (void)viewTransition
- {
- static NSString * const kCATransitionTypeFlip = @"oglFlip"; //翻页效果
- CATransition *transition = [CATransition animation];
- transition.type = kCATransitionTypeFlip;
- transition.subtype = kCATransitionFromRight;
- transition.duration = 1.5f;
- [_transitionOrangeView.layer addAnimation:transition forKey:nil];
- [self.view performSelector:@selector(sendSubviewToBack:) withObject:_transitionOrangeView afterDelay:1.0f];
- }
这个方法简单的实现了view翻页的效果,当然目前控制器的根view只有一个subview,所以使用sendSubviewToBack的方法后显示的还是这个view,但是看到了动画效果。
- - (void)anotherTransition
- {
- _transitionBlueView = [[UIView alloc] initWithFrame:self.view.bounds];
- _transitionBlueView.backgroundColor = [UIColor blueColor];
- [UIView transitionFromView:_transitionOrangeView
- toView:_transitionBlueView
- duration:1.0f
- options:UIViewAnimationOptionTransitionCrossDissolve
- completion:nil];
- }
代码很简洁和易读。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。