问题
So, I have the following code to show my NSWindow
:
[_window makeKeyAndOrderFront:self];
[NSAnimationContext beginGrouping];
[[_window animator] setAlphaValue:1.0];
[[_window animator] setFrame:NSMakeRect([[NSApp currentEvent] window].frame.origin.x - 102, [[NSApp currentEvent] window].frame.origin.y - 238, _window.frame.size.width, _window.frame.size.height) display:YES];
[NSAnimationContext endGrouping];
This code is called right after the user has clicked on the app's status bar icon, that's why I use the [[NSApp currentEvent] window].frame.origin.y/x
to get the location of the status bar icon.
This code runs perfectly but, sometimes, it's very laggy and "jumpy" and I don't know why.
Any ideas about this and how to fix it?
回答1:
The NSWindow
animator uses NSAnimation
, which means that it rapidly fires a timer to animate the frame of the window. At each frame of the animation, every view inside the window is redrawn. If you have large views with somewhat complex view hierarchies, the performance is quite bad and there's no real way to work around it.
I would recommend JNWAnimatableWindow as a substitute for the default NSWindow
animator, as it uses a Core Animation CALayer
to perform animations on and therefore is much smoother.
来源:https://stackoverflow.com/questions/14542509/why-is-nswindow-animator-setframe-very-laggy-sometimes