【问题标题】:UIImageView doesn't animate when using Auto Layout使用自动布局时 UIImageView 没有动画
【发布时间】:2016-11-06 13:01:44
【问题描述】:

我的图片有以下限制:

  • 领先 = 0
  • 尾随 = 0
  • 顶部 = 75
  • 身高 = 250

我希望我的图像从屏幕右侧开始并移动到设置约束的初始位置。

这是我的代码:

imageLeadingConstraint.constant = 100
imageTrailingConstraint.constant = -100
imageTopConstraint.constant = 100
image.layoutIfNeeded()

UIView.animate(withDuration: 1.0) {
    self.imageLeadingConstraint.constant = 0
    self.imageTrailingConstraint.constant = 0
    self.imageTopConstraint.constant = 75
    self.image.layoutIfNeeded()
}

问题是图像正在移动到初始位置,但没有使用动画。
我的代码对吗?为什么图像只是出现在新位置而不是动画?

【问题讨论】:

    标签: ios swift uiimageview autolayout


    【解决方案1】:

    您应该为 imageView 的超级视图调用 layoutIfNeeded

    self.image.superview.layoutIfNeeded()
    

    【讨论】:

    • 用户已将此问题标记为基于 swift,您的答案在 Objective-C 中。
    【解决方案2】:

    我不确定我是否理解你,但是: 1)在动画块之外更改约束(第二次)。 2) 改用 self.image.superview?.layoutIfNeeded()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多