簡體   English   中英

CALayer 的屬性 isGeometryFlipped 在 macOS 上不起作用

[英]CALayer's property isGeometryFlipped doesn't work on macOS

我想將 NSView 的幾何形狀更改為左上角。 但結果不正確。(PS:結果在iOS平台上是正確的。)

我的代碼是這樣的&效果如下:

import Cocoa

class ViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.wantsLayer = true
        self.view.layer?.isGeometryFlipped = true

        let red = NSView(frame: NSRect(origin: .zero, size: CGSize(width: 400, height: 200)))
        red.wantsLayer = true
        red.layer?.backgroundColor = NSColor.red.cgColor

        let yellow = NSView(frame: NSRect(origin: .zero, size: CGSize(width: 200, height: 100)))
        yellow.wantsLayer = true
        yellow.layer?.backgroundColor = NSColor.yellow.cgColor

        red.addSubview(yellow)
        view.addSubview(red)
    }
}

在此處輸入圖片說明

為什么我設置屬性isGeometryFlipped =真觀的層,幾何不從左下角切換到左上角

這是因為 iOS 和 macOS 中的坐標系不一樣。 Point (x:0, y:0) in : - iOS : 左上角ios坐標系

  • macOS:在左下角macOS 坐標系

如果您想要紅色視圖top left的黃色視圖,請執行以下操作:

let yellow = NSView(frame: NSRect(origin: CGPoint(x: 0, y: 400), size: CGSize(width: 200, height: 100))) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM