我正在尝试制作一个呼吸圈动画。只有breathInAnimation(成长圈)似乎有生气。调用breathOutAnimation(缩小圆圈),但似乎没有做任何事情。我猜它会立即恢复到起始状态,但我不明白为什么。
- (void)viewDidLoad
animationView = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2.0, self.view.frame.size.height / 2.0, 200, 200)];
animationView.backgroundColor = [UIColor blueColor];
animationView.layer.cornerRadius = 100;
animationView.center = self.view.center;
[self.view addSubview: animationView];
[self drawCircleEdge];
[self breathInAnimation];
[NSTimer timerWithTimeInterval:7.0 target:self selector:@selector(breathOutAnimation) userInfo:nil repeats:YES];
- (void)breathInAnimation
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
[scaleAnimation setValue:@"breathIn" forKey:@"id"];
scaleAnimation.duration = 4;
scaleAnimation.fromValue = [NSNumber numberWithFloat:0.1];
scaleAnimation.toValue = [NSNumber numberWithFloat:1];
[animationView.layer addAnimation:scaleAnimation forKey:@"scale"];
- (无效)breathOutAnimation
CABasicAnimation *breathOut = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
breathOut.duration = 8;
breathOut.fromValue = [NSNumber numberWithFloat:1];
breathOut.toValue = [NSNumber numberWithFloat:0.1];
[animationView.layer removeAllAnimations];
[animationView.layer addAnimation: breathOut forKey:@"scale"];
我也尝试过使用scaleAnimation的委托。
- (void)animationDidStop:(CABasicAnimation *)theAnimation2 finished:(BOOL)flag
这有效,但在动画完成后,圆圈会回到结束第一个动画后的状态。收缩 - >动画结束 - >再次全力以赴。
我不确定我在这里缺少什么。
答案 0 :(得分:2)
CAAnimations不会将转换应用于您的图层,它会在表示层上设置动画,然后在动画完成时切换回您的图层。
您应该在播放动画时应用变换。
-(void)breathOutAnimation
{
CABasicAnimation *breathOut = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
breathOut.duration = 8;
breathOut.fromValue = [NSNumber numberWithFloat:1];
breathOut.toValue = [NSNumber numberWithFloat:0.1];
[animationView.layer removeAllAnimations];
[animationView.layer addAnimation: breathOut forKey:@"scale"];
animationView.layer.transform = CATransform3DMakeScale(0.1, 0.1, 0.1);
}