我正在旋转UIBarButtonItem。 动画工作得很好。但是,当我从服务器接收数据时,我想平稳地停止动画。
我尝试使用presentationLayer捕获当前值,但我得到的只是0。
- (void)animateLeftBarButtonItem
{
//Animate Button
CABasicAnimation *leftBarButtonItemRotator = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
leftBarButtonItemRotator.delegate = self;
leftBarButtonItemRotator.removedOnCompletion = NO;
leftBarButtonItemRotator.fillMode = kCAFillModeForwards;
leftBarButtonItemRotator.duration = 5.0;
leftBarButtonItemRotator.fromValue = @0.0f;
leftBarButtonItemRotator.toValue = @(50*(-2.0f * M_PI));
leftBarButtonItemRotator.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[self.navigationItem.leftBarButtonItem.customView.layer addAnimation:leftBarButtonItemRotator forKey:@"leftBarButtonRotation"];
}
有什么建议吗?
此致
答案 0 :(得分:1)
好的,我非常需要这个,我花了一个小时尝试不同的黑客。
下一个想法似乎有效。它基于以下事实:似乎每个动画都添加到一个图层(即使它在同一个属性上运行),CA混合它。所以我们开始另一个具有相同属性的动画,因为我们希望它们淡出它们。完成后我们删除前一个。
我接下来提供的示例中有几个关键部分。我加粗了他们。
希望它有所帮助。
-(void)slowBeat{ //check if animations alredy running? [CATransaction begin]; CAKeyframeAnimation* zoom = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"]; zoom.duration = 1.0f; zoom.speed = .75f; zoom.repeatCount = INFINITY; zoom.fillMode = kCAFillModeForwards; zoom.removedOnCompletion = YES; zoom.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; zoom.calculationMode = kCAAnimationCubicPaced; zoom.keyTimes = [NSArray arrayWithObjects: [NSNumber numberWithFloat:0.0f], [NSNumber numberWithFloat:0.80f], [NSNumber numberWithFloat:0.95f], [NSNumber numberWithFloat:1.0f], nil]; zoom.values = [NSArray arrayWithObjects: [NSNumber numberWithFloat:1.0f], [NSNumber numberWithFloat:1.0f], [NSNumber numberWithFloat:1.3f], [NSNumber numberWithFloat:1.0f], nil]; [self.contentWrapper.layer addAnimation:zoom forKey:@"slowBeatPosition"]; CAKeyframeAnimation* fadeInAndOut = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"]; fadeInAndOut.duration = 1.0f; fadeInAndOut.speed = .75f; fadeInAndOut.repeatCount = INFINITY; fadeInAndOut.fillMode = kCAFillModeForwards; fadeInAndOut.removedOnCompletion = YES; fadeInAndOut.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; fadeInAndOut.calculationMode = kCAAnimationCubicPaced; fadeInAndOut.keyTimes = [NSArray arrayWithObjects: [NSNumber numberWithFloat:0.0f], [NSNumber numberWithFloat:0.80f], [NSNumber numberWithFloat:0.95f], [NSNumber numberWithFloat:1.0f], nil]; fadeInAndOut.values = [NSArray arrayWithObjects: (id)[UIColor colorWithWhite:1.0f alpha:0.1f].CGColor, (id)[UIColor colorWithWhite:1.0f alpha:0.1f].CGColor, (id)[UIColor colorWithWhite:1.0f alpha:0.7f].CGColor, (id)[UIColor colorWithWhite:1.0f alpha:0.1f].CGColor, nil]; [self.redView.layer addAnimation:fadeInAndOut forKey:@"slowBeatColor"]; [CATransaction commit]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [CATransaction begin]; [CATransaction setCompletionBlock:^{ [self.contentWrapper.layer removeAnimationForKey:@"slowBeatPosition"]; [self.redView.layer removeAnimationForKey:@"slowBeatColor"]; [self.contentWrapper.layer removeAnimationForKey:@"slowBeatPosition2"]; [self.redView.layer removeAnimationForKey:@"slowBeatColor2"]; }]; CABasicAnimation* zoom2 = [CABasicAnimation animationWithKeyPath:@"transform"]; zoom2.duration = 0.4f; zoom2.speed = 1.0f; zoom2.repeatCount = 1; zoom2.fillMode = kCAFillModeForwards; zoom2.removedOnCompletion = NO; zoom2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; zoom2.toValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; [self.contentWrapper.layer addAnimation:zoom2 forKey:@"slowBeatPosition2"]; CABasicAnimation* fadeInAndOut2 = [CABasicAnimation animationWithKeyPath:@"backgroundColor"]; fadeInAndOut2.duration = 0.4f; fadeInAndOut2.speed = 1.0f; fadeInAndOut2.repeatCount = 1; fadeInAndOut2.fillMode = kCAFillModeForwards; fadeInAndOut2.removedOnCompletion = NO; fadeInAndOut2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; fadeInAndOut2.toValue = (id)[UIColor colorWithWhite:1.0f alpha:0.1f].CGColor; [self.redView.layer addAnimation:fadeInAndOut2 forKey:@"slowBeatColor2"]; [CATransaction commit]; }); }