Mac Developer Library

Developer

AppKit Framework Reference NSAnimationContext Class Reference

Options
Deployment Target:

On This Page
Language:

NSAnimationContext

NSAnimationContext is analogous to CATransaction and are similar in overall concept to NSGraphicsContext. Each thread maintains its own stack of nestable NSAnimationContext instances, with each new instance initialized as a copy of the instance below (so, inheriting its current properties).

Multiple NSAnimationContext instances can be nested, allowing a given block of code to initiate animations using its own specified duration without affecting animations initiated by surrounding code.

  • [NSAnimationContext beginGrouping];
  • // Animate enclosed operations with a duration of 1 second
  • [[NSAnimationContext currentContext] setDuration:1.0];
  • [[aView animator] setFrame:newFrame];
  • ...
  • [NSAnimationContext beginGrouping];
  • // Animate alpha fades with half-second duration
  • [[NSAnimationContext currentContext] setDuration:0.5];
  • [[aView animator] setAlphaValue:0.75];
  • [[bView animator] setAlphaValue:0.75];
  • [NSAnimationContext endGrouping];
  • ...
  • // Will animate with a duration of 1 second
  • [[bView animator] setFrame:secondFrame];
  • [NSAnimationContext endGrouping];

Inheritance


Conforms To


Import Statement


Swift

import AppKit

Objective-C

@import AppKit;

Availability


Available in OS X v10.5 and later.
  • Creates a new animation grouping.

    Declaration

    Swift

    class func beginGrouping()

    Objective-C

    + (void)beginGrouping

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.5 and later.

  • Ends the current animation grouping.

    Declaration

    Swift

    class func endGrouping()

    Objective-C

    + (void)endGrouping

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.5 and later.

  • Returns the current animation context.

    Declaration

    Swift

    class func currentContext() -> NSAnimationContext

    Objective-C

    + (NSAnimationContext *)currentContext

    Return Value

    The current animation context.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.5 and later.

  • A completion Block that is called when the animations in the grouping are completed.

    Declaration

    Swift

    var completionHandler: (() -> Void)?

    Objective-C

    @property(copy) void (^completionHandler)(void)

    Discussion

    If set to a non-nil value, a context’s completionHandler is guaranteed to be called on the main thread as soon as all animations subsequently added to the current NSAnimationContext grouping have completed or been cancelled.

    This method drives the underlying CATransactioncompletionBlock property, although the Application Kit may assign a different, intermediary completionBlock to the current CATransaction.

    The completion handler waits for all animations to which the handler applies, independent of whether they are evaluated by the Application Kit or delegated to Core Animation for evaluation in the render tree before firing.

    If no animations are added before the current grouping is ended—or the completionHandler is set to a different value—the handler will be invoked immediately.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.7 and later.

  • Allows you to specify a completion block body after the set of animation actions whose completion will trigger the completion block.

    Declaration

    Swift

    class func runAnimationGroup(_ changes: (NSAnimationContext!) -> Void, completionHandler completionHandler: (() -> Void)?)

    Objective-C

    + (void)runAnimationGroup:(void (^)(NSAnimationContext *context))changes completionHandler:(void (^)(void))completionHandler

    Parameters

    changes

    A block object containing animations for this transaction group.

    The context parameter passes the thread’s current NSAnimationContext to the Block as a convenience, so code within the Block that wants to change or query properties of the current context does not have to call currentContext.

    The block object returns no value.

    completionHandler

    A Block object called when animations for this transaction group are completed.

    The Block object takes no parameters and returns no value.

    Discussion

    Using this method allows you to more naturally group animations and an completion Block.

    An example use is as follows. Using this method you would write the following code fragment:

    • [NSAnimationContext runAnimationGroup:^(NSAnimationContext *context){
    • // Start some animations.
    • [[myView animator] setFrameSize:newViewSize];
    • [[myWindow animator] setFrame:newWindowFrame display:YES];
    • } completionHandler:^{
    • // This block will be invoked when all of the animations
    • // started above have completed or been cancelled.
    • NSLog(@"All done!");
    • }];

    The above code is semantically equivalent to the following:

    • [NSAnimationContext beginGrouping];
    • [NSAnimationContext setCompletionHandler:^{
    • // This block will be invoked when all of the animations
    • // started below have completed or been cancelled.
    • NSLog(@"All done!");
    • }];
    • // Start some animations.
    • [[myView animator] setFrameSize:newViewSize];
    • [[myWindow animator] setFrame:newWindowFrame display:YES];
    • [NSAnimationContext endGrouping];

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.7 and later.

  • duration duration Property

    The duration used by animations created as a result of setting new values for an animatable property.

    Declaration

    Swift

    var duration: NSTimeInterval

    Objective-C

    @property NSTimeInterval duration

    Discussion

    Any animations that occur as a result of setting the values of animatable properties in the current context will run for this duration.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.5 and later.

  • The timing function used for all animations within this animation proxy group.

    Declaration

    Swift

    var timingFunction: CAMediaTimingFunction?

    Objective-C

    @property(strong) CAMediaTimingFunction *timingFunction

    Discussion

    The NSAnimationContext timing function is analogous to the CATransaction setAnimationTimingFunction: method.

    Animations initiated through the “animator” proxy syntax, that do not have an explicitly specified timing functions, will inherit the enclosing NSAnimationContext instance’s timingFunction if it is not nil (which is the default).

    As with the existing duration property, changing a timing function causes the same change in the underlying CATransaction instance’s animationTimingFunction.

    Also as with the duration property, you may change the timingFunction any number of times within a given NSAnimationContext beginGrouping and endGrouping block. Changes to the timingFunction will apply to any animations that are subsequently initiated in that NSAnimationContext grouping, until the timingFunction is possibly changed again.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.7 and later.

  • Determine if animations are enabled or not for animations that occur as a result of another property change.

    Declaration

    Swift

    var allowsImplicitAnimation: Bool

    Objective-C

    @property BOOL allowsImplicitAnimation

    Discussion

    Using the animator proxy will automatically set allowsImplicitAnimation to YEStrue. When YEStrue, other properties can implicitly animate along with the initially changed property.

    For instance, calling [[view animator] setFrame:frame] will allow subviews to also animate their frame positions. When the value is NOfalse the behavior is diabled.

    The default value is NOfalse.

    This is only applicable when layer backed on OS v10.8 and later.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.8 and later.