在animationDidStop回调失败时识别CAAnimation

时间:2016-11-29 05:47:17

标签: ios objective-c iphone swift animation

如何在完成后识别CAAnimation?我通过将字符串值设置为animationID并在animationDidStop方法中检查来尝试KVO。但KVO有时会返回integer而不是string animationID,应用程序崩溃了。

这是我的代码:

-(void)moveLayer:(CALayer*)layer to:(CGPoint)point duration:(NSTimeInterval)duration animationID:(NSString*)animationid
{
    // Prepare the animation from the current position to the new position
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    animation.fromValue = [layer valueForKey:@"position"];
    [animation setDuration:duration];

    animation.toValue = [NSValue valueWithCGPoint:point];
    animation.delegate=self;
    [animation setValue:animationid forKey:@"animationID"];
    animation.fillMode = kCAFillModeForwards;
    animation.removedOnCompletion = NO;
    // Update the layer's position so that the layer doesn't snap back when the animation completes.
    layer.position = point;

    // Add the animation, overriding the implicit animation.
    [layer addAnimation:animation forKey:@"position"];
}

和回调是:

- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)flag
{
    if (flag) {

    id animationIDString=[animation valueForKey:@"animationID"];
    // this is coming as an int value sometimes and the code below this fails ..
    if([animationIDString isEqual:@"animation1"]) {
        //animation is animation1
        _movieBalloonImageView.hidden=NO;
        _storiesBalloonImageView.hidden=NO;
        _rhymesBalloonImageView.hidden=NO;
        [self.navigationController popViewControllerAnimated:NO];
}
}
}

可能的原因是什么?是因为动画在调用此委托之前被销毁了吗? 我将removedOnCompletion设置为NO,将fillmode设置为kCAFillModeForwards。但代码未按预期工作。

animation.fillMode = kCAFillModeForwards;
animation.removedOnCompletion = NO;

或者有没有其他方法可以做到这一点?我在viewController中的几个地方调用moveLayer方法。

0 个答案:

没有答案