Determine constraint constant while it is animatin

2019-08-23 06:19发布

问题:

I've got a view controller set up with a top and bottom view like so:

----------------
|              |
|              |
|              |
|              |
---------------- < constraint with constant of 250
|              |
|              |
|              |
|              |
----------------

Using AutoLayout in IB, I've set each of the views to conform to the outside of the parent and given the center constraint a constant of 250.

I then have an IBOutlet to the center constraint which I can animate:

// MARK: - View Containers
@IBOutlet weak public var topView: UIView!
@IBOutlet weak public var bottomView: UIView!

// MARK: - Constraints
@IBOutlet weak var splitViewDividerConstraint: NSLayoutConstraint!

And animating:

func animateConstraint() {
    splitViewDividerConstraint.constant = someNumber

    UIView.animate(withDuration: duration, animations: {
        self.view.layoutIfNeeded()
    })
}

Under certain circumstances I need to determine whether or not the constraint is above or below specific y positions (let's say when it moves up past a y point of 100 or less, or down past a y point of 400 or greater).

This works fine so long as the constraint isn't animating, but if it is, because I'm technically setting the constraint constant before the animation begins, it always returns the destination constraint position, which will be inaccurate if the constraint animation is still occurring.

Is there a way to retrieve the current position of a constraint while it is being animated?