【发布时间】:2020-12-24 11:37:14
【问题描述】:
我正在创建一个 UIView,它基本上是一个带边框的透明 UIView。
此视图正在使用此代码进行动画处理
UIView.animate(withDuration: 1.0, delay: 0, options: [.repeat, .autoreverse], animations: {
var frame = areaScanRect.frame
frame.size.width = frame.size.width * 0.8
frame.size.height = frame.size.height * 1.2
areaScanRect.frame = frame
}, completion: nil)
视图的框架类似于 (0,0,200,100)。
我需要这个视图以主视图为中心。
如果我只是将矩形添加到主视图中,它会出现在 (0,0),而不是显然在屏幕中心。
然后我添加约束,使其居中。
areaScanRect.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
areaScanRect.centerXAnchor.constraint(equalTo: view.centerXAnchor),
areaScanRect.centerYAnchor.constraint(equalTo: view.centerYAnchor),
areaScanRect.widthAnchor.constraint(equalToConstant: areaScanRect.bounds.size.width),
areaScanRect.heightAnchor.constraint(equalToConstant: areaScanRect.bounds.size.height)
])
areaScanRect 显示在正确的位置,但随着动画的进行,没有居中。
为什么?
【问题讨论】: