UIViewPropertyAnimator 没有按预期工作

UIViewPropertyAnimator doesn’t work as expected

我正在尝试在用户点击 UIView 时为 UIView 和 imageView 制作动画。这是我的代码:

// When the user taps on the featured story image view
@objc func feturedStoryTapped() {

    /*featuredStoryView.layer.zPosition = 1

    // Hide the necessary elements
    self.featuredTitle.isHidden = true
    self.bookTitle.isHidden = true
    self.authorName.isHidden = true

    // Unhide the necessary elements
    self.exitButton.isHidden = false*/

    let animator = UIViewPropertyAnimator(duration: 0.9, dampingRatio: 0.4, animations: {

        self.featuredStoryView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
        self.bookCover.frame = CGRect(x: 100, y: 75, width: 250, height: 280)

    })

    animator.startAnimation()

}

预期行为: 当我点击一次 UIView 时,视图应该占据整个屏幕并且 imageView 应该缩小一点。

问题 问题是,当我点击一次视图时,视图占据了整个屏幕,但 imageView 没有缩小,只有在我第二次点击时才会缩小。

我希望在我点击一次视图时执行这两个任务。我将添加一个 GIF 以帮助理解。

已修复,只需在 属性 动画器中的两行代码之间添加 view.layoutIfNeeded()

// When the user taps on the featured story image view
    @objc func feturedStoryTapped() {

    /*featuredStoryView.layer.zPosition = 1

    // Hide the necessary elements
    self.featuredTitle.isHidden = true
    self.bookTitle.isHidden = true
    self.authorName.isHidden = true

    // Unhide the necessary elements
    self.exitButton.isHidden = false*/

    let animator = UIViewPropertyAnimator(duration: 0.9, dampingRatio: 0.4, animations: {

        self.featuredStoryView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
        Self.view.layoutIfNeeded()

        self.bookCover.frame = CGRect(x: 100, y: 75, width: 250, height: 280)

    })

    animator.startAnimation()

}