2013-02-14 54 views
0

我需要我的圖像視圖在每個動畫的開頭和結尾處更改其.image。在UIImageView啓動/完成動畫後更改圖像

這是動畫:

- (void)performLeft{ 

    CGPoint point0 = imView.layer.position; 
    CGPoint point1 = { point0.x - 4, point0.y }; 

    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position.x"]; 
    anim.fromValue = @(point0.x); 
    anim.toValue = @(point1.x); 
    anim.duration = 0.2f; 
    anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; 

    // First we update the model layer's property. 
    imView.layer.position = point1; 
    // Now we attach the animation. 
    [imView.layer addAnimation:anim forKey:@"position.x"]; 
} 

我知道,我可以打電話......

[anim animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)]; 

但我不知道我怎麼能使用動畫改變imView圖像? 那麼如何使用動畫來更改我的圖像視圖的.image

謝謝!

+0

哪種類型的動畫你找誰?你可以嘗試使用[myView commitAnimations]。看到這裏:http://www.raywenderlich.com/2454/how-to-use-uiview-animation-tutorial – 2013-02-14 04:00:43

+0

那麼這個問題叫核心動畫... – Tanner 2013-02-14 14:48:04

回答

2

簡短的回答是,您不能使用Core Animation來更改圖像視圖的圖像。核心動畫在圖層上運行,而不是視圖。此外,Core Animation僅創建更改的外觀。底層實際上並沒有改變。

我會建議使用UIView動畫而不是CAAnimation對象。然後你可以使用你的完成方法來改變圖像。

UIImage動畫更容易做,它會改變您正在動畫的圖像的屬性。

UIImage的動畫基於代碼會是這個樣子:

- (void)performLeft 
{ 
    CGFloat center = imView.center; 
    center.x = center.x - 4; 
    imView.image = startingImage; //Set your stating image before animation begins 
    [UIView animateWithDuration: 0.2 
    delay: 0.0 
    options: UIViewAnimationOptionCurveEaseIn 
    animations: 
    ^{ 
    imView.center = center; 
    } 
    completion: 
    ^{ 
    imView.image = endingImage; //Set the ending image once the animation completes 
    } 
    ]; 
}