UIImageView frame does not liven up as expected (size and origin)

Including a UIView frame animation problem. The view should be animated both in origin and in size, with an increase in size and a beginning moving linearly in order to maintain the view in the same place. But it happens that the view is reduced to size (0,0), and then enlarged to a size that is still not correct. See the attached video.

Problem video: 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 })
}

More details:

self is a subclass of UIView limited to a superview.

_iconView is an UIImageView

animateIn guaranteed to work until animateOut

animateOutis a function that does not work properly, animateInworks

+4
2

, , , ...

  • - _iconView

  • _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)

    })

}
0

, _iconView layoutSubviews(). , .

0

Source: https://habr.com/ru/post/1685166/


All Articles