繁体   English   中英

如何将 UIView 居中在 UIButton 的中心

[英]How to center a UIView at center of UIButton

我正在使用 Lottie 在我的ViewController上添加 animation 。 这是我展示 animation 的方式:

    let tapAnimationView = AnimationView(name: "tap")
    tapAnimationView.frame = CGRect(x: 0, y: 0, width: 65, height: 60)
    tapAnimationView.center = myButton.center //CGPoint(x: myButton.center.x, y: self.view.center.y)
    tapAnimationView.loopMode = .loop
    tapAnimationView.animationSpeed = 0.5
    tapAnimationView.contentMode = .scaleAspectFill
    view.addSubview(tapAnimationView)
    tapAnimationView.play(fromFrame: 0, toFrame: 8, loopMode: .loop, completion: nil)

现在这段代码的作用是在靠近左上角的某个地方显示 animation。 如果我用注释中的行替换它,它会靠近按钮,但不像我想要的那样在按钮的中心。

请不要说我没有使用 AutoLayout Constraints 而是使用Autoresizing

你可以试试

override func viewDidLayoutSubviews() {
   super.viewDidLayoutSubviews()
   tapAnimationView.center = myButton.center
}

我不确定您将按钮添加到哪里。 所以在 self.view 中获取按钮点

let point = myButton.convert(point: myButton.center, to: self.view)
tapAnimationView.center = point.center

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM