如何将带有锚点的视图定位到刚刚完成动画的视图?

How to position a view with anchors to a view that has just finished animating?

在两个轴上锚定到屏幕中心的视图动画(转换)到具有新大小的新位置。 当动画结束时,完成块调用一个动画发生在另一个视图上,它的前导锚点与第一个动画视图的尾随锚点保持一定距离。 我遇到的问题是我不知道使第二个视图的动画考虑到刚刚完成的视图的新框架的解决方案。第二个视图需要向右结束并以第一个视图的 Y 为中心。

我尝试过的每件事都有相同的结果:第二个视图在动画到其新位置和大小之前考虑了第一个视图的位置。如何更新它的约束? 我尝试使用 NSLayoutConstraint 变量,更新约束,但没有任何效果。列出相关代码,谢谢。

func setupViews() { // called in viewDidLoad
        view.addSubview(programIcon)
        view.addSubview(programTitle)
        let iconConstraints = [
        programIcon.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        programIcon.centerYAnchor.constraint(equalTo: view.centerYAnchor),
        programIcon.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -37.5),
        programIcon.heightAnchor.constraint(equalTo: programIcon.widthAnchor)
        ]
        NSLayoutConstraint.activate(iconConstraints)

        titleHiddenX = programTitle.leadingAnchor.constraint(equalTo: view.trailingAnchor)
        titleHiddenX.isActive = true
        titleHiddenY = programTitle.topAnchor.constraint(equalTo: view.topAnchor) // could do without?
        titleHiddenY.isActive = true

        titleVisibleX = programTitle.leadingAnchor.constraint(equalTo: programIcon.trailingAnchor, constant: 10)
        titleVisibleX.isActive = false
        titleVisibleY = programTitle.centerYAnchor.constraint(equalTo: programIcon.centerYAnchor)
        titleVisibleY.isActive = false
        } 
    @objc func animateIcon() {
        if animationHasBeenShown {
            print("animation has beed shown already")
        } else {
            //I have removed the math part as its not relevant here.

            UIView.animate(withDuration: 0.5, animations: {
                self.programIcon.transform = scale.concatenating(translation)
            }) { (true) in
                self.programTitle.setNeedsUpdateConstraints()
                self.animateElementsOnScreen()
                print("icon frame \(self.programIcon.frame)")
            }
        }
    }
   func animateElementsOnScreen() {
        UIView.animate(withDuration: 0.5, animations: { // this needs to change.

            self.titleHiddenX.isActive = false
            self.titleHiddenY.isActive = false
            self.titleVisibleX.isActive = true
            self.titleVisibleY.isActive = true
            self.view.layoutIfNeeded()
        }) { (true) in
            print("title frame \(self.programTitle.frame)")
        } // and so on, more views animating.

来自 Apple 的文档 (https://developer.apple.com/documentation/uikit/uiview/1622459-transform):

In iOS 8.0 and later, the transform property does not affect Auto Layout. Auto layout calculates a view’s alignment rectangle based on its untransformed frame.

如果您想移动缩放视图AND保持其自动布局/ constraints 属性,您需要为视图的约束更改设置动画,而不是应用 .transform.