2011-10-04 55 views
3

我遇到了CAKeyframeAnimation的这个问题。动画UIView的图层后,我想定位UIView到我动画的位置,以便用户可以继续使用此UIView上的按钮。在CAKeyframeAnimation后定位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

我会尝试在将动画添加到图层之前设置结束位置。假设你正在改变层的positionanimationDidStop功能我会改变这些行

// 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)。否则,填充模式将无法正常工作。