【问题标题】:UIView.animateWithDuration: animation always stops at original positionUIView.animateWithDuration:动画总是停在原始位置
【发布时间】:2015-09-27 13:53:33
【问题描述】:

我在视图中间添加了一个ImageView(没有设置约束)。

使用此代码,我想将此 ImageView 从其原始位置移动到新位置。

不幸的是,它从新位置移动到原来的位置 - 为什么?

let opts = UIViewAnimationOptions.CurveEaseInOut
UIView.animateWithDuration(1, delay: 0, options: opts, animations: {
     self.ivSun.center.x += 100
}, completion: nil) 

【问题讨论】:

  • 你的意思是动画完成后回到原点还是跳到x + 100然后动画回到原点?
  • 你多久使用一次这个动画?你经常重复吗?

标签: ios swift cocoa-touch animation uiimageview


【解决方案1】:

在动画完成后添加。

self.ivSun.layer.position = self.ivSun.layer.presentationLayer().position

我的游戏中实现了代码。

        CATransaction.begin()
        CATransaction.setCompletionBlock { () -> Void in
                    self.viewBall.layer.position = self.viewBall.layer.presentationLayer().position
            }
        }
        var animation = CABasicAnimation(keyPath: "position")
        animation.duration = ballMoveTime
        animation.fromValue = NSValue(CGPoint: viewBall.layer.presentationLayer().position)
        animation.toValue = NSValue(CGPoint: CGPointMake(self.borderRight, self.borderMiddle))
        animation.removedOnCompletion = false
        animation.fillMode = kCAFillModeForwards
        viewBall.layer.addAnimation(animation, forKey: "transform")
        CATransaction.commit()

根据需要更改位置值。

【讨论】:

  • 你在哪里写的?你能显示完整的代码吗?
  • 这很好用!非常感谢。但是为什么移动视图这么难呢?
  • 你也可以通过 UIView 动画来做到这一点,但你告诉我这还没有完成,所以我给出了这段代码。 bcoz 这没什么,但我必须看到你的完整代码,这样我才能知道问题是什么。用这个动画(这需要时间。)。还有一件事,你可以通过删除一些代码行来实现这一点,就像你可以从代码中删除 CATRransaction 一样,只需使用 CABasicAnimation。 :-)
  • 这似乎有点过头了。他可能应该解决 UIView 动画为什么不起作用的根本问题。比如,你是否使用约束来布局你的视图?如果是这样,那么您必须更新约束并要求视图更新动画块中的布局。 @AntonSack 如果您使用约束,我将添加代码以轻松制作动画。
【解决方案2】:

试试这个。

func animateImage()
{
    UIView.animateWithDuration(3.0, delay: 0.0, options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in
        self.imageView.center = CGPointMake(self.imageView.center.x+10, self.imageView.center.y+10)
    }, completion: nil)
}

【讨论】:

    【解决方案3】:

    如果不了解上下文和相关代码的更多信息,很难回答“为什么”它会快速回到原来的位置,但作为替代方案,您可以将完成处理程序设置为您想要的任何最终位置/坐标。

    通常我在我的小部件/视图上使用约束,并为它们的值设置动画(例如,相对于为小部件的框架设置动画)......效果非常好。

    【讨论】:

      【解决方案4】:

      这是由于自动布局。需要在动画中更改和更新约束,如下所示:

      - (IBAction)moveAction:(id)sender {
          self.spaceConstraint.constant += 220;
      
          [UIView animateWithDuration:0.5 animations:^{
               [self.imageView layoutIfNeeded];
          }];
      }
      

      【讨论】:

        【解决方案5】:
        var animation = CABasicAnimation(keyPath: "position")
        animation.duration = ballMoveTime
        animation.fromValue = NSValue(CGPoint: viewBall.layer.presentationLayer().position)
        animation.toValue = NSValue(CGPoint: CGPointMake(self.borderRight, self.borderMiddle))
        animation.removedOnCompletion = false
        animation.fillMode = kCAFillModeForwards
        viewBall.layer.addAnimation(animation, forKey: "transform")
        CATransaction.commit()
        

        【讨论】:

          猜你喜欢
          • 2015-12-24
          • 1970-01-01
          • 2011-04-20
          • 1970-01-01
          • 1970-01-01
          • 2015-12-16
          • 2014-11-24
          • 2019-06-06
          • 1970-01-01
          相关资源
          最近更新 更多