我正在try 转换一些视点中心,以便能够在这CGPoint之间划清界限.

然而,这些点完全垂直对齐,convert方法返回意外的值.显然,我确实遗漏了一些东西,也搞不清楚.我在此请求:)

我快速设置了这个代码,重现了这个问题:

class ViewController: UIViewController {
    
    private lazy var backgroundView: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = .systemBackground
        return view
    }()
    
    private lazy var firstView: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = .systemRed
        return view
    }()
    
    private lazy var secondView: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = .systemBlue
        return view
    }()
    
    private lazy var firstSubview: UIImageView = {
        let view = UIImageView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = .systemMint
        return view
    }()
    
    private lazy var secondSubview: UIImageView = {
        let view = UIImageView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = .systemGreen
        return view
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(backgroundView)
        
        backgroundView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
        backgroundView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 20).isActive = true
        backgroundView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -20).isActive = true
        backgroundView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
        
        backgroundView.addSubview(firstView)
        backgroundView.addSubview(secondView)
        
        firstView.topAnchor.constraint(equalTo: backgroundView.topAnchor).isActive = true
        firstView.leadingAnchor.constraint(equalTo: backgroundView.leadingAnchor).isActive = true
        firstView.trailingAnchor.constraint(equalTo: backgroundView.trailingAnchor).isActive = true
        
        secondView.topAnchor.constraint(equalTo: firstView.bottomAnchor, constant: 10).isActive = true
        secondView.leadingAnchor.constraint(equalTo: backgroundView.leadingAnchor).isActive = true
        secondView.trailingAnchor.constraint(equalTo: backgroundView.trailingAnchor).isActive = true
        
        firstView.addSubview(firstSubview)
        
        firstSubview.topAnchor.constraint(equalTo: firstView.topAnchor).isActive = true
        firstSubview.bottomAnchor.constraint(equalTo: firstView.bottomAnchor).isActive = true
        firstSubview.leadingAnchor.constraint(equalTo: firstView.leadingAnchor).isActive = true
        firstSubview.heightAnchor.constraint(equalToConstant: 50).isActive = true
        firstSubview.widthAnchor.constraint(equalToConstant: 50).isActive = true
        
        secondView.addSubview(secondSubview)
        
        secondSubview.topAnchor.constraint(equalTo: secondView.topAnchor).isActive = true
        secondSubview.bottomAnchor.constraint(equalTo: secondView.bottomAnchor).isActive = true
        secondSubview.leadingAnchor.constraint(equalTo: secondView.leadingAnchor, constant: 10).isActive = true
        secondSubview.heightAnchor.constraint(equalToConstant: 30).isActive = true
        secondSubview.widthAnchor.constraint(equalToConstant: 30).isActive = true
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
                
        let firstCenter1 = firstSubview.convert(firstSubview.center, to: view)
        let secondCenter1 = secondSubview.convert(secondSubview.center, to: view)
        
        print(firstCenter1, secondCenter1)
    }
}

打印结果是(45.0, 84.0) (55.0, 134.0),然而,正如我预期的那样,我应该打印出(45.0, 84.0) (45.0, 134.0).因为secondSubview的亲属center.x就是字面上的45.0firstSubview.

我混合并匹配了S的参数和接球手,但没有运气.即使我得到center.x,center.y也给出了错误的值.

我错过了什么吗?

推荐答案

center属性返回parent's坐标系中的一个点.

中心点在其SuperView的坐标系中以点为单位指定.设置此属性会相应地更新frame属性中矩形的原点.

因此,您不应该对子视图调用convert.您应该拨打firstViewsecondView:

let firstCenter1 = firstView.convert(firstSubview.center, to: view)
let secondCenter1 = secondView.convert(secondSubview.center, to: view)

Swift相关问答推荐

计算Vision OS相机与Vision OS中3D模型之间的距离

SWIFT计划计时器方法在时间间隔后未被调用

与视图交互使工具栏&;标题消失

Swift 运算符中 inout 的行为

是否可以限制 ExpressibleByIntegerLiteral struct 的初始值?

我如何在 swift 中的 viewdidload 之前进行委托?

在 init(from decoder: Decoder) 方法中访问原始 JSON 数据

如何使一个 Reality Composer 场景中的分组对象可拖动?

为 ObservedObject 和 State 对象配置预览

格式化大货币数字

如何在 SWIFTUI 中旋转修剪?

Swift并发:为什么不在其他后台线程上执行任务

如何从另一个 swift 文件中调用函数

Vapor 4 身份验证问题 [用户未通过身份验证]

创建 ViewModel 时 iOS 模拟器上的黑屏

响应 UITextField (UIViewRepresentable) 中的特定按键

Xcode:方法参数的代码完成

如何防止 UITableViewCell 移动(Swift 5)

即使设置为从不也可以访问 iOS11 照片库

在 Swift 中将计时器标签格式化为小时:分钟:秒