Cocoa: what are the differences between CATransaction and NSAnimationContext


I can animately move a layer-hosing view using code:

[CATransaction setAnimationDuration:2];
self.imageLayer.position = CGPointMake(0, 0);

and I can do it by following code as well:

 [NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
    [context setDuration:2];
    self.imageLayer.position = CGPointMake(0, 0);
} completionHandler:nil];

I want to know what's the difference between CATransaction and NSAnimationContext, which one I should use to animate view? why can I set the duration in NSAnimationContext and CATransaction?

CATransaction works on Core Animation layer animations. CATransaction is cross-platform between iOS and Mac OS.

NSAnimationContext works with an NSAnimationContext. It's Mac OS specific.

(The NS animation stuff is Mac-only, and the UIView animation stuff is iOS only.)

I work in iOS more than Mac OS these days, and I always look for cross-platform ways to do things.

CAAnimation, CALayer, CAAnimationGroup, etc, are nearly identical between Mac and iOS. There are some differences (e.g. quicktime layers are not supported in iOS, Core Image support is more limited in iOS, etc.) but CA stuff is more alike than different.