Any reason to use NSViewAnimation over CoreAnimation techniques? - cocoa

I'm working on a Snow Leopard app and I'm doing some view animations (swapping them) and I've seen some older examples using NSViewAnimation. I'm wondering if these are completely obsoleted by Core Animation?
It seems fairly simple for me to use
[[myView animator] setFrame:newSwapFrame];
But I'm just wondering if I'm missing something, or if NSViewAnimation is still relevant (other than for backwards compatibility).

Generally you should move to Core Animation for 10.5+ code. NSViewAnimation is fairly primitive and doesn't make as efficient use of the hardware. I can't think of any advantages of staying with NSViewAnimation other than 10.4 compatibility.

There are some cases where Core Animation is not suitable. For example, you can't put a WebView in a layer-backed view without funky things happening.
Also, text fields in layer-backed views don't use subpixel anti-aliasing, which can be a deal breaker.
If you're not using layer-backed views then your example of just using the animatable property support is definitely easier than using NSAnimation.

If you need to get window screenshot later (as example, to make transition animation between two window states), the Core Animated views (view with CAAnimation) may not been grabbed.
But CAAnimation is always working fast, and NSAnimation may slowing if CPU is too busy.

Related

Frame-by-frame animation in MacOs

I'm looking for a way to do frame-by-frame programmatically drawn animations in a MacOs application (not keyframe property animation). I have tried drawing to CALayers using the drawLayer:inContext: delegate method, calling setNeedsDisplay to draw each frame, however I'm getting poor performance doing it this way. Is there a recommended way to do this type of animation in Cocoa?
A good way to do entirely custom animations is by using CADisplayLink (iOS) or CVDisplayLink (macOS). CVDisplayLink is basically a timer that fires as often as the display refreshes.
You can then calculate your own timing functions based on the values you get off CVDisplayLink. The API is still C so it is a bit cumbersome to use, especially in Swift, but once you get how it functions it works like a charm.
I have only had good experiences with CVDisplayLink, especially with layers. They are really performant. I was able to animate 1000+ layers CVDisplayLink driven at 60fps without any problems.
If you need any help in using the API, feel free to ask!
Alternative:
If you want to use a more modern API, I can recommend SpriteKit. There are some nice animation APIs as well. And they perform really good. Apple uses it itself to draw more complex views (like the Memory Debugger in Xcode).

Forcing OSX NSScroller thumb to draw in highlighted form without actually mouse tracking

I'm drawing an NSScroller into a bitmap. I need to capture it with the thumb highlighted (I'm using cacheDisplayInRect:toBitmapImageRep:, but I've tried the separate draw methods into a GC created on the bitmap). I've tried everything I can think of, including setting various values in the (private) _sFlags2 and sFlags NSScroller ivars before the draw call. I can't send it events because the scroller isn't actually live.
Eventually I need this to work on 10.6+, but all of my testing so far has been on 10.7+ (which is where the new style scrollbars came in), and I haven't checked 10.6 yet because I'm also using alignmentRectForFrame: and haven't faked that out for 10.6 yet.
After some quality time spent with Hopper! I found the following solution:
[[scrollbar scrollerImp] setShouldDrawRolloverState: YES];
Undocumented, but I'm a happy camper!

Cocoa GUI - effects, polish

I'm creating an in-car control screen (will be run from a Mac Mini) and am looking for some libraries or code samples for "effects". For example, I might want the name of the current track playing to fly in from the right. I might want screens to fade or slide up, etc.
I am aware that I can manually write these effects in Objective-C.
I am hoping there is a library like scriptaculous for JavaScript that allows me to easily manipulate an existing TextView, ImageView, etc.
A framework or otherwise is preferred. I'm working in native cocoa. I don't mind if the library costs $.
Thanks,
Rick
Have a look at Core Image and Core Animation, both of which will allow you to add visual effects. Core Image, as its name implies, works with images only but can do fancy transitions. You can "fake" UI animations with it though by rendering a view to an image, swapping the image in in place or over the top of the view and then running a transition to another view.
Core Animation works directly with Cocoa Views and does have some transitions available. Both APIs (especially Core Animation) are fairly complex and have a learning curve.

Core Animation - Sheet-like Window Sliding

How difficult would it be to use core animation to make an NSView slide in an out of view like a sheet? Generally speaking, what would be involved in accomplishing this? I've been reading through the CA documentation, but it's been hard for me to pinpoint which parts are relevant to what I want to do since I have no experience with the framework.
Any tips at all would be much appreciated.
Thanks.
Since you're talking of a NSView, you're probably using Cocoa's animation support, not CA directly. In this case, you just need to set the view's frame through the view's animator object:
[theView setFrame:offscreenFrame];
[[theView animator] setFrame:finalFrame];
Unfortunately, Cocoa view animation interacts badly with the more advanced features of CA, like setting an easing. You might have more luck using NSViewAnimation instead, which is not Core Animation-backed and allows for a little more flexibility.

Is a keyframed transition possible in Core Animation?

I know that keyframed transitions are possible in Core Animation via setting the path property on the CAAnimation instance. However, CATransition does not seem to have this functionality. Does anyone know any other ways to control the transition apart from setting the timing function?
The answer seems to be no. If you want to do this sort of thing, you have to add the CAAnimation yourself rather than depending on transitions. The transitions probably depend on some deep workings of CoreAnimation, because they don't work the same way normal animations do (they don't move the object in question, they control how the new content of the object replaces the old).

Resources