【发布时间】:2019-06-28 15:17:30
【问题描述】:
我有一个自定义对象,它是UIView 的子类。我想要的行为是让这个对象最初出现在屏幕中间。然后,在 3 秒后,我希望对象转换(带有平滑动画)到已经有一个元素的水平 UIStackView,所以 customObject 理想情况下会移动到 stackView 的右半部分。
我目前无法实现此行为。
ViewController.swift:
class ViewController: UIViewController {
let customObject = CustomObject(frame: .init(x: 0,
y: 0,
width: 200.0,
height: 200.0))
let stackView: UIStackView = {
let sv = UIStackView()
sv.axis = .horizontal
return sv
}()
override func viewDidLoad() {
super.viewDidLoad()
let label = UILabel()
label.text = "first Element in horizontal stackView"
stackView.addArrangedSubview(label)
addCustomObjectToStackView()
}
func addCustomObjectToStackView() {
stackView.addArrangedSubview(customObject)
UIView.animate(withDuration: 1.0, animations: {
customObject.layoutIfNeeded()
})
}
}
【问题讨论】:
-
张贴一张你希望它在开始时的样子,以及你希望它在动画后的样子。它真的需要在堆栈视图中结束吗?
-
是的,我绝对希望它最终出现在 stackView 中。我知道如何为
UIView设置动画,否则。另外,我会发布一张图片 -
给你@DonMag
标签: ios swift autolayout frame uistackview