当用户点击 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 来帮助理解。
最佳答案
修复它,只需在属性动画器内的两行代码之间添加 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/