Wednesday, June 12, 2013

CATransaction kCATransactionAnimationDuration example in Objective C (iOS).

CATransaction kCATransactionAnimationDuration

Transaction properties
These constants define the property keys used by valueForKey: and setValue:forKey:.

NSString * const kCATransactionAnimationDuration;
NSString * const kCATransactionDisableActions;
NSString * const kCATransactionAnimationTimingFunction;
NSString * const kCATransactionCompletionBlock;

Duration, in seconds, for animations triggered within the transaction group. The value for this key must be an instance of NSNumber.
If YES, implicit actions for property changes made within the transaction group are suppressed. The value for this key must be an instance of NSNumber.
An instance of CAMediaTimingFunction that overrides the timing function for all animations triggered within the transaction group.
A completion block object that is guaranteed to be called (on the main thread) as soon as all animations subsequently added by this transaction group have completed (or have been removed.) If no animations are added before the current transaction group is committed (or the completion block is set to a different value,) the block will be invoked immediately.

CATransaction kCATransactionAnimationDuration example.
NSArray* path = [board caclulatePath:s];
    [CATransaction begin];
    [CATransaction setValue:[NSNumber numberWithFloat:([path count] * laserSpeed)] forKey:kCATransactionAnimationDuration];
    float numBetweenPoints = (float)((float)s.frame.size.height / (float)10) * 2;
    float delay = (laserSpeed / numBetweenPoints);
    for (int i = 0; i < [path count]; i++)
        [self performSelector:@selector(kickoffLaserAnimationWithPath:) withObject:path afterDelay:(delay*i)];

    [CATransaction commit];

Example of [CATransaction kCATransactionAnimationDuration].

+ (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 kCATransactionAnimationDuration example.
CAKeyframeAnimation * animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animation.path = path;
animation.duration = grids * gridWidth / [self speed];
animation.fillMode = kCAFillModeForwards;
animation.removedOnCompletion = NO;
animation.delegate = self;
animation.calculationMode = kCAAnimationLinear;
[self addAnimation:animation forKey:@"movement"];
self.position = destination;

[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:_turn_duration]
eyes.position = eyeDestination;
pupils.position = pupilDestination;
[CATransaction commit];


End of CATransaction kCATransactionAnimationDuration example article.