我在使用 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"];
委托(delegate)方法:
- (void)animationDidStop:(CAKeyframeAnimation *)anim finished:(BOOL)flag
{
locationView.y = -10.0;
}
这确实有效,但最后我遇到了这个显示故障,动画被删除(UIView 层跳回原始位置)并立即调用委托(delegate)方法,这使其跳转到结束位置。
我觉得最后的过程是这样的:动画完成,渲染动画的最后一帧,跳回到原来的位置,渲染这一帧,调用委托(delegate)方法并将uiview放在它的结束位置.
如何解决此显示故障?
最佳答案
我会尝试在将动画添加到图层之前设置结束位置。假设您要更改 animationDidStop
函数中层的 position
,我会更改这些行
// Set end position for layer
CAKeyframeAnimation *openAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position.y"];
openAnimation.fillMode = kCAFillModeBackwards;
//openAnimation.removedOnCompletion = NO;
// The rest of your code
它们的键路径在这里很重要,因为您必须设置与键路径相同的图层属性(在本例中为 position
)。否则,填充模式将无法正常工作。
关于ios - 在 CAKeyframeAnimation 之后定位 UIView 会导致显示故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7647467/