0

I have code that animate a view (change constraints)

UIView .animate(withDuration: TimeInterval(duration), delay: 0.0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseInOut, animations: {

        self.topConstraint!.constant = self.yPositioOfOpenPanel
        self.superview?.layoutIfNeeded()

    }

The strange behaviour is that on iPhone X layoutSubviews is called but on other simulators and real devices is not. (And I think it should be)

Same behaviour is when I change constraint manually

if let constraint = topConstraint {
            constraint.constant = self.superview!.frame.height - recogniser.location(in: self.superview).y + yPositionOfBeginTouchInPanel
        }

EDIT:

As suggested I am adding some new information.

Animations works very well, everything is animating as it should.

The problem is that I would like to trigger some new events, while animation is in place. (I would use topConstraint.constant to calculate something while animations is in place, or when I change topConstraint manually) That's why I would like to set some trigger on method layoutSubviews. This method should be triggered when animations changes subviews.

I think that this works prior iOS 11 (but I can't be 100% sure, because this functionality is new in my program, but as I can remember I use this in other programs and it worked). It is still working for iPhone X (Simulator) but not for other simulators or real devices (obviously the real iPhone X is not out yet).

Marko Zadravec
  • 8,298
  • 10
  • 55
  • 97

2 Answers2

1

I think you should slightly change the animation code:

// update constraints before you call animate
self.topConstraint!.constant = self.yPositioOfOpenPanel
// tell superview that the layout will need to be updated
self.superview?.setNeedsLayout()

UIView.animate(withDuration: TimeInterval(duration), delay: 0.0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseInOut, animations: {
    // here you tell it to update the layout according to new constraints
    self.superview?.layoutIfNeeded()
}
Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90
0

It turns out that I need to do is:

UIView .animate(withDuration: TimeInterval(duration), delay: 0.0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseInOut, animations: {

        self.topConstraint!.constant = self.originalConstraintConstant
        self.superview?.layoutIfNeeded()
        self.layoutIfNeeded()

    }

This triggered layoutIfNeeded inside this view.

But I still don't know why this works on all iPhones, and other works only on iPhone X. It should be a bug inside iOS or Xcode.

Marko Zadravec
  • 8,298
  • 10
  • 55
  • 97