我将在当前视图底部的aUIViewController中添加一个视图作为子视图。我可以使用alpha来做淡入动画,但是我想像键盘弹出式菜单一样显示它。

let popup: UIView = ..
popup.alpha = 0.0
self.view.addSubView(popup)
UIView.animate(withDuration: 0.3, animations: {
    popup.alpha = 1.0
}) { _ in
}

如何制作动画?

最佳答案

您可以更新帧,但我喜欢使用变换设置动画,这样您仍然可以使用约束,而不必处理它们。

let popup: UIView = ..
popup.transform = CGAffineTransform(translationX: 0, y: popup.bounds.height)
self.view.addSubView(popup)
UIView.animate(withDuration: 0.3, animations: {
    popup.transform = .identity
}) { _ in
}

假设视图位于屏幕底部,则将视图向下平移一个等于其高度的量,然后将变换动画化回其标识(即原始位置)。

10-07 19:51
查看更多