在UIImageView启动/完成动画后更改图像

问题描述:

我需要我的图像视图在每个动画的开头和结尾处更改其.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

简短的回答是,您不能使用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 
    } 
    ]; 
}