Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.

How to use nsview.layer? #78

Open
paulrouget opened this issue Mar 17, 2017 · 6 comments
Open

How to use nsview.layer? #78

paulrouget opened this issue Mar 17, 2017 · 6 comments

Comments

@paulrouget
Copy link

paulrouget commented Mar 17, 2017

I'm trying to animate a Cocoa nsview via its layer. But with no luck. What would it take to make it work?

I'm trying for example to set the opacity of a layer. All the conditions to enable core animation are met.

This code crashes:

let content_view: id = msg_send![nswindow, contentView];
let layer: id = msg_send![content_view, layer];
let transform: CGAffineTransform = msg_send![layer, affineTransform];
msg_send![layer, setAffineTransform:transform];

And this code behaves in a strange way:

let content_view: id = msg_send![nswindow, contentView];
let layer: id = msg_send![content_view, layer];
let opacity: CGFloat = msg_send![layer, opacity];
// opacity is equal to 0.
let opacity: CGFloat = 0.5;
msg_send![layer, setOpacity:opacity];
// view disappear
let opacity: CGFloat = msg_send![layer, opacity];
// opacity is still equal to 0.
@metajack
Copy link
Contributor

cc @pcwalton

@oluseyi
Copy link

oluseyi commented Apr 26, 2017

@paulrouget Did you call setWantLayer:YES on the NSView? Unlike on iOS, macOS views are not CALayer-backed by default.

@paulrouget
Copy link
Author

Did you call setWantLayer:YES on the NSView? Unlike on iOS, macOS views are not CALayer-backed by default.

Yes.

@oluseyi
Copy link

oluseyi commented Apr 26, 2017

For the first snippet, any chance you got a stack trace?

For the second snippet, did you call setNeedsDisplay:YES, or displayIfNeeded if you wanted to evaluate attributes in the same runloop?

Sorry if the questions seem obvious, just eliminating the low-hanging fruit.

@pcwalton
Copy link
Contributor

You might have to do something special to the NSWindow in order to get Core Animation to work. I think Interface Builder does this for you if you use nibs, but if you're setting things up manually there may be something magic you have to invoke.

@paulrouget
Copy link
Author

@pcwalton: Interface Builder does this for you if you use nibs

I use nibs.

@oluseyi: For the first snippet, any chance you got a stack trace?
For the second snippet, did you call setNeedsDisplay:YES, or displayIfNeeded if you wanted to evaluate attributes in the same runloop?

I'll try to get a backtrace and try these later.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants