MMMAnimator

Objective-C

@interface MMMAnimator : NSObject

Swift

class MMMAnimator : NSObject

Minimalistic animator object in the spirit of helpers defined in MMMAnimation.

You add animation items, which are basically a set of blocks that will be called every frame on the main run loop and when it’s done or cancelled.

It’s not for every case, it’s for those moments when you know the duration in advance and just need to animate a simple custom property and don’t want to subclass CALayer or mess with its multithreaded delegates.

The animator object does not take care of interpolation of values nor time curves, the normalized time passed into update blocks can be transformed and values can be interpolated using simple helpers in MMMAnimation.

  • Undocumented

    Declaration

    Objective-C

    + (instancetype)shared;

    Swift

    class func shared() -> Self
  • Schedules a new animation item.

    The updateBlock is called on every update cycle within the animation’s duration. It is guaranteed to be called with zero time even if cancelled before the next run loop cycle. The update block is also guaranteed to be called with time being 1 unless is cancelled earlier.

    The doneBlock is called after the animation finishes or is cancelled.

    The repeatCount parameter can be set to 0 to mean infinite repeat count.

    In case repeatCount is different from 1, then autoreverse influences the way the time changes when passed to the updateBlock: if YES, then it’ll grow from 0 to 1 and then from 1 to 0 on the next repeat, changing back to from 0 to 1 after this, etc; if NO, then it’ll always from from 0 to 1 on every repeat step.

    The animation will start on the next cycle of the refresh timer and will have the timestamp of this cycle as its actual start time, so there is no need in explicit transactions: all animation added on the same run loop cycle are guaranteed to be run in sync.

    Keep the object returned. The animation stops when the reference to this object is released.

    Declaration

    Objective-C

    - (nonnull MMMAnimationHandle *)
        addAnimationWithDuration:(CGFloat)duration
                     updateBlock:(nonnull MMMAnimatorUpdateBlock)updateBlock
                       doneBlock:(nullable MMMAnimatorDoneBlock)doneBlock;

    Swift

    func addAnimation(duration: CGFloat, update updateBlock: @escaping MMMAnimatorUpdateBlock, completion doneBlock: MMMAnimatorDoneBlock? = nil) -> MMMAnimationHandle
  • Undocumented

    Declaration

    Objective-C

    - (MMMAnimationHandle *)addAnimationWithDuration:(CGFloat)duration
    	repeatCount:(NSInteger)repeatCount
    	autoreverse:(BOOL)autoreverse
    	updateBlock:(MMMAnimatorUpdateBlock)updateBlock
    	doneBlock:(nullable MMMAnimatorDoneBlock)doneBlock
    	NS_SWIFT_NAME(addAnimation(duration:repeatCount:autoreverse:update:completion:));

    Swift

    func addAnimation(duration: CGFloat, repeatCount: Int, autoreverse: Bool, update updateBlock: @escaping MMMAnimatorUpdateBlock, completion doneBlock: MMMAnimatorDoneBlock? = nil) -> MMMAnimationHandle
  • Despite the +shared method defined above you can still create own instances of this class.

    Declaration

    Objective-C

    - (nonnull id)init;

    Swift

    init()
  • For unit tests only: will synchronously run all the animations already in the animator and the ones added within the given block in the specified number of steps, executing the given block after each step. This is used in view-based tests for those views that run all their animations using MMMAnimator.

    The idea is that an animated action is triggered in the animationsBlock (e.g. hideAnimated:YES) and then the stepBlock is called in the very beginning and in exactly numberOfSteps - 1 moments afterwards. The moments will be selected, so they are spaced equally and the last one is exactly at the end of the longest animation item.

    Declaration

    Objective-C

    - (void)_testRunInNumberOfSteps:(NSInteger)numberOfSteps
                         animations:(nonnull void (^)(void))animationsBlock
                        forEachStep:(nonnull void (^)(NSInteger))stepBlock;

    Swift

    func _testRun(numberOfSteps: Int, animations animationsBlock: () -> Void, stepBlock: (Int) -> Void)