iOS:当动画停止并保持动画与模态同步时的急动

时间:2014-02-28 10:57:50

标签: ios core-animation cabasicanimation

我正在尝试使用CABasicAnimation

对y轴上的视图执行变换
for(int x = 0; x < viewsArray.count; x++)
{
    CABasicAnimation *startAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
    [startAnimation setToValue:[NSNumber numberWithFloat:DEGREES_RADIANS(-55.0f)]];
    [startAnimation setDuration:5.0];
    startAnimation.delegate = self;
    UIView *view = [viewsArray objectAtIndex:x];
    [startAnimation setValue:@"rotate" forKey:@"id"];
    [view.layer addAnimation:startAnimation forKey:@"rotate"];
}

没什么特别的。我想保持模态和动画同步,所以 animationDidStop我尝试使用for循环再次设置转换:

if([[anim valueForKey:@"id"] isEqualToString:@"rotate"])
{
    aTransform = CATransform3DRotate(aTransform, DEGREES_RADIANS(-55.0), 0.0, 1.0, 0.0);
    for(int x = 0; x < viewsArray.count; x++)
    {
        UIView *view = [viewsArray objectAtIndex:x];
        view.layer.transform = aTransform;
    }
}

但是我意识到在动画停止后动画会猛然移动到animationDidStop中变换设置的角度。

有没有人知道为什么以及在不使用removedOnCompletion = NO;的情况下最好的方法是什么?我想避免使用它,并希望保持动画始终与模态层同步。

1 个答案:

答案 0 :(得分:1)

您可以在添加动画的同时进行设置,但是您需要为动画提供fromValue以阻止它立即更新表示层:

for(int x = 0; x < viewsArray.count; x++)
{
    UIView *view = [viewsArray objectAtIndex:x];
    NSString *keyPath = @"transform.rotation.y";
    NSNumber *toValue = [NSNumber numberWithFloat:DEGREES_RADIANS(-55.0f)];

    CABasicAnimation *startAnimation = [CABasicAnimation animationWithKeyPath:keyPath];
    [startAnimation setFromValue:[view.layer valueForKeyPath:keyPath]];
    //[startAnimation setToValue:toValue]; We don't need to set this as we're updating the current value on the layer instead.
    [startAnimation setDuration:5.0];
    [view.layer addAnimation:startAnimation forKey:@"rotate"];
    [view.layer setValue:toValue forKeyPath:keyPath]; // Update the modal
}