2011-10-04 15 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)。否則,填充模式將無法正常工作。