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



我正在尝试使用 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;的最佳方法是什么?我想避免使用它,并希望使动画始终与模态层同步。

您可以在添加动画的同时设置它,但您需要为动画提供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
}

最新更新