Wednesday, June 12, 2013

CATransaction setAnimationTimingFunction example in Objective C (iOS).

CATransaction setAnimationTimingFunction

Sets the timing function used for all animations within this transaction group.

+ (void)setAnimationTimingFunction:(CAMediaTimingFunction *)function

An instance of CAMediaTimingFunction.

Discussion of [CATransaction setAnimationTimingFunction]
This is a convenience method that sets the CAMediaTimingFunction for the valueForKey: value of the kCATransactionAnimationTimingFunction key.

CATransaction setAnimationTimingFunction example.
When animating the movement of a UIView within a begin / commit animation block, you can use the following method to set the animation timing curve:

[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
When animating a layer via CAAnimation, you can use the following to set the timing function there:

[animation setTimingFunction:kCAMediaTimingFunctionEaseInEaseOut];
Finally, when wrapping a series of animations in a CATransaction, you can use the following to set the coordinated timing function of all the animations:

[CATransaction setAnimationTimingFunction:kCAMediaTimingFunctionEaseInEaseOut];

Example of [CATransaction setAnimationTimingFunction].

+ (void)transactionWithDuration:(NSTimeInterval)duration
                     animations:(void (^)(void))animations;

+ (void)transactionWithDuration:(NSTimeInterval)duration
                     animations:(void (^)(void))animations;
    [CATransaction begin];
    [CATransaction setValue:[NSNumber numberWithFloat:duration] forKey:kCATransactionAnimationDuration];
    [CATransaction commit];
Usage with your code (assuming you made it a category on UIView)

[UIView transactionWithDuration:3 animations:^{

    CGPoint low  = CGPointMake(0.150, 0.000);
    CGPoint high = CGPointMake(0.500, 0.000);

    CAMediaTimingFunction* perfectIn =
        [CAMediaTimingFunction functionWithControlPoints:low.x
                                                        :1.0 - high.x
                                                        :1.0 - high.y];
    [CATransaction setAnimationTimingFunction: perfectIn];
    CABasicAnimation *fadeIn = [CABasicAnimation animationWithKeyPath:@"opacity"];
    fadeIn.fromValue = [NSNumber numberWithFloat:0];
    fadeIn.toValue = [NSNumber numberWithFloat:1.0];
    [viewB.layer addAnimation:fadeIn forKey:@"animateOpacity"];


CATransaction setAnimationTimingFunction example.
[CATransaction begin];
[CATransaction setAnimationDuration:0.5];
[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[CATransaction setCompletionBlock:^{

// Create the CABasicAnimation using your existing code
CABasicAnimation *myPropertyAnim = [CABasicAnimation animationWithKeyPath:@"myProperty"];
// TODO: Setup animation range
myPropertyAnim.toValue = newValue;

// The CATransaction does not observe arbitrary properties so this fails:
//myLayer.myProperty = newValue;

// Add the CAAnimation subclass during the CATransaction
[myLayer addAnimation:myPropertyAnim forKey:@"myKey"];

[CATransaction commit];

End of CATransaction setAnimationTimingFunction example article.