4

我在使用 CAKeyframeAnimation 时遇到了这个问题。在为 UIView 的图层设置动画后,我想将 UIView 定位到我要设置动画的位置,以便用户可以继续使用此 UIView 上的按钮。

我目前正在使用此代码:

    // Open animation
    CAKeyframeAnimation *openAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.y"];
    openAnimation.fillMode = kCAFillModeForwards;
    openAnimation.duration = .55;
    openAnimation.delegate = self;
    openAnimation.removedOnCompletion = NO;


    openAnimation.values = [NSArray arrayWithObjects:
                            [NSNumber numberWithFloat:0.0],
                            [NSNumber numberWithFloat:58.0],
                            [NSNumber numberWithFloat:48.0], nil];

    openAnimation.keyTimes = [NSArray arrayWithObjects:
                              [NSNumber numberWithFloat:0.0],
                              [NSNumber numberWithFloat:0.62],
                              [NSNumber numberWithFloat:1.0], nil];

    openAnimation.timingFunctions = [NSArray arrayWithObjects:
                                     [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut], 
                                     [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut], nil];

    [locationView.layer addAnimation:openAnimation forKey:@"OPEN_ANIMATION"];

和委托方法:

- (void)animationDidStop:(CAKeyframeAnimation *)anim finished:(BOOL)flag
{
    locationView.y = -10.0;
}

这实际上可行,但最后我遇到了动画被删除的显示故障(UIView 层跳回原始位置)并立即调用委托方法,这使它跳到结束位置。

我感觉最后的过程是这样的:动画完成,渲染动画的最后一帧,跳回原来的位置,渲染这一帧,调用委托方法并将uiview放在它的结束位置。

如何解决此显示故障?

4

1 回答 1

4

在将动画添加到图层之前,我会尝试设置结束位置。假设您正在更改函数position中的图层,animationDidStop我会更改这些行

// Set end position for layer

CAKeyframeAnimation *openAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position.y"];
openAnimation.fillMode = kCAFillModeBackwards;
//openAnimation.removedOnCompletion = NO;

// The rest of your code

它们的 keypath 在这里很重要,因为您必须设置与 keypath 相同的层属性(在本例中position)。否则,填充模式将无法正常工作。

于 2011-11-23T17:50:58.627 回答