ios - UIViewPropertyAnimator 未按预期工作

标签 ios swift uiviewpropertyanimator

当用户点击 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 一次时, View 应该占据整个屏幕,而 imageView 应该缩小一点。

问题 问题是,当我点击一次 View 时, View 占据整个屏幕,但 imageView 不会缩小,只有当我第二次点击时它才会缩小。

我希望当我点击 View 一次时执行这两个任务。我将添加一个 GIF 来帮助理解。

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()

}

关于ios - UIViewPropertyAnimator 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57533221/

相关文章:

ios - 后退按钮启动 UIViewPropertyAnimator?

ios - 有没有办法观察 UIViewPropertyAnimator 中 fractionComplete 的变化

ios - UIViewPropertyAnimator 在 iOS10 和 iOS11 上反转动画的不同行为。 `isReversed` 和 `fractionComplete` 属性上的错误?

ios - 如何快速解析这个json?

ios - 根据 View Controller 设置 UINavigationBar 颜色

iOS Facebook LoginButton 约束冲突

ios - 收到 NSNotification 后执行选择器会调用我的 View Controller 的 deinit

ios - 如何在 Xcode6 中为新创建的约束禁用 "Relative to margin"选项

ios - 删除 animateWithDuration :completion 的完成 block 内的 UIView

ios - swift 4 KVO_IS_RETAINING_ALL_OBSERVERS_OF_THIS_OBJECT_IF_IT_CRASHES_AN_OBSERVER_WAS_OVERRELEASED_OR_SMASHED