繁体   English   中英

UIImageView框架没有按预期动画(大小和原点)

[英]UIImageView frame not animating as expected (size and origin)

与UIView帧动画有关的问题。 视图应在原点和大小两者中设置动画,大小增加,原点线性移动以使视图保持在同一位置。 但是会发生什么,视图减小到大小(0,0),然后增加到仍然不正确的大小。 见附件视频。

该问题的视频: https//media.pairby.com/I/u/a/IualExcJXn7CqLsGkcNZfwyEw5MKi3SV/v.mp4

func animateIn() {
  // Make _iconView large
  let w = bounds.width
  _iconView.frame = CGRect(
    x: frame.midX - w/2,
    y: frame.midY - w/2,
    width: w, height: w)

  isHidden = false

  UIView.animate(withDuration: 0.2, animations: {
    self.alpha = 1

    // Animate it smaller
    let w = self.bounds.width * 0.5
    self._iconView.frame = CGRect(
      x: self.frame.midX - w/2,
      y: self.frame.midY - w/2,
      width: w, height: w)
  })
}

func animateOut() {
  UIView.animate(withDuration: 3, delay: 0, options: .beginFromCurrentState, animations: {
    self.alpha = 0

    // Make it large again
    let w = self.bounds.width
    self._iconView.frame = CGRect(
      x: self.frame.midX - w/2,
      y: self.frame.midY - w/2,
      width: w, height: w)

  }, completion: { _ in self.isHidden = true })
}

更多细节:

self是UIView的子类,仅限于superview。

_iconView是一个UIImageView

animateIn保证之前运行animateOut

animateOut是不能按预期工作的函数, animateIn工作

然而,还不完全清楚你需要做什么

  1. 您无需更改大小即可 - 您可以简单地为_iconView框架设置其当前状态的动画,以及

  2. 因为_iconView是self子视图 ,所以需要相对于边界而不是框架定位它。

试试这样:

func doAnim() -> Void {     

    UIView.animate(withDuration: 3, delay: 0, options: .beginFromCurrentState, animations: {
        self.alpha = 0

        let s = self.bounds.width
        let halfS = s / 2

        self._iconView.frame = CGRect(
            x: self.bounds.midX - halfS,
            y: self.bounds.midY - halfS,
            width: s,
            height: s)

    })

}

通过从layoutSubviews()函数中删除所有_iconView相关代码来解决它。 我不确定为什么它首先被调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM