首页 > 解决方案 > 使用所需的动画和方向为特定视图设置动画

问题描述

我正在尝试通过将约束设置为 0 或 100 来设置视图的动画以显示和显示。我遇到的问题是如何更改动画样式或方向?因为视图将从下到上进行动画处理,但我想从上到下显示视图。

我不能使用self.view.layoutIfneeded,因为我不希望它为整个视图设置动画。我只想以正确的方向为特定视图设置动画。谢谢。

UIView.animate(withDuration: 0.5, delay: 0, options: .allowUserInteraction, animations: {
                //self.view.layoutIfNeeded()
                self.topButtonView.layoutIfNeeded()
            }, completion: nil)

标签: iosswiftuiviewuiviewcontrolleruicollectionview

解决方案


利用CGAffineTransform

// move the view to top 100 offset from current y
self.topButtonView.transform = CGAffineTransform.identity.translatedBy(x: 0, y: -100)
// then reset the transform
// it will animate downward (back to it's original position)
UIView.animate(withDuration: 0.5, delay: 0, options: .allowUserInteraction, animations: {
    self.topButtonView.transform = .identity
}, completion: nil)

推荐阅读