【问题标题】:Change image after UIImageView had started/completed animationUIImageView 开始/完成动画后更改图像
【发布时间】:2013-02-14 00:24:32
【问题描述】:

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

谢谢!

【问题讨论】:

标签: ios cocoa-touch core-animation quartz-graphics


【解决方案1】:

简短的回答是您不能使用核心动画来更改图像视图的图像。核心动画在图层上运行,而不是视图。此外,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
  }
  ];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2013-03-23
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    • 2016-03-06
    相关资源
    最近更新 更多