为了用户体验更好,动画是必不可少的。常用的动画 放大缩小 和 渐变消失或显示。废话不多说直接上代码:
CGFloat kAnimationDuration = 0.3f;
CAGradientLayer *contentLayer = (CAGradientLayer *)_contentView.layer;
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
scaleAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0, 0, 1)];
scaleAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 1, 1)];
scaleAnimation.duration = kAnimationDuration;
scaleAnimation.cumulative = NO;
scaleAnimation.repeatCount = 1;
[scaleAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
[contentLayer addAnimation: scaleAnimation forKey:@"myScale"];
CABasicAnimation *showViewAnn = [CABasicAnimation animationWithKeyPath:@"opacity"];
showViewAnn.fromValue = [NSNumber numberWithFloat:0.0];
showViewAnn.toValue = [NSNumber numberWithFloat:1.0];
showViewAnn.duration = kAnimationDuration;
showViewAnn.fillMode = kCAFillModeForwards;
showViewAnn.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
showViewAnn.removedOnCompletion = NO;
showViewAnn.delegate = self;
[contentLayer addAnimation:showViewAnn forKey:@"myShow"];
CAAnimationGroup *group = [CAAnimationGroup animation];
group.duration = kAnimationDuration;
group.removedOnCompletion = NO;
group.repeatCount = 1;
group.fillMode = kCAFillModeForwards;
[group setAnimations:@[scaleAnimation,showViewAnn]];
[contentLayer addAnimation:group forKey:@"animationOpacity"];
除此之外,还会用动画组合,多个动画同时执行。以上代码动画效果是 放大同时透明度由0变成1;