简体   繁体   中英

CALayer's property isGeometryFlipped doesn't work on macOS

I want to change the NSView's geometry to the top-left corner. But the result isn't correct.( PS: the result is correct on the iOS platform.)

my code like this & the effect is as follows:

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)
    }
}

在此处输入图片说明

Why I set the property isGeometryFlipped= true of View's layer, the geometry isn't change from bottom-left corner to the top-left corner ?

It's because coordinate system in iOS and macOS are not the same. Point (x:0, y:0) in : - iOS : at the top left ios坐标系

  • macOS : at the bottom left macOS 坐标系

if you want the yellow view at the top left of the red view,just do this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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