我需要我的图像视图在每个动画的开头和结尾都改变它的.image。
这是动画:
- (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 :(得分: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
}
];
}