Showing posts with label CATransaction. Show all posts
Showing posts with label CATransaction. Show all posts

Wednesday, June 12, 2013

CATransaction kCATransactionDisableActions example in Objective C (iOS).

CATransaction kCATransactionDisableActions

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;

Constants
kCATransactionAnimationDuration
Duration, in seconds, for animations triggered within the transaction group. The value for this key must be an instance of NSNumber.
kCATransactionDisableActions
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.
kCATransactionAnimationTimingFunction
An instance of CAMediaTimingFunction that overrides the timing function for all animations triggered within the transaction group.
kCATransactionCompletionBlock
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 kCATransactionDisableActions example.
- (void)popViewControllerMoveInFromTop {
[CATransaction begin];
CATransition *transition;
transition = [CATransition animation];
transition.type = kCATransitionMoveIn;
transition.subtype = kCATransitionFromTop;
transition.duration = 0.7;

[CATransaction setValue:(id)kCFBooleanTrue
                 forKey:kCATransactionDisableActions];

[self.view.layer addAnimation:transition forKey:nil];
[self  popViewControllerAnimated:NO];   
[CATransaction commit];
}

Example of [CATransaction kCATransactionDisableActions].
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue
                 forKey:kCATransactionDisableActions];
layer.content = someImageRef;
[CATransaction commit];

CATransaction kCATransactionDisableActions example.
You can temporarily disable layer actions when changing layer property values by setting the value of the transaction’s kCATransactionDisableActions to true. Any changes made during the scope of that transaction will not result in an animation occurring. Listing 2 shows an example that disables the fade animation that occurs when removing aLayer from a visible layer-tree.

[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue
                 forKey:kCATransactionDisableActions];
[aLayer removeFromSuperlayer];
[CATransaction commit];

End of CATransaction kCATransactionDisableActions example article.

CATransaction unlock example in Objective C (iOS).

CATransaction unlock

Relinquishes a previously acquired transaction lock.

+ (void)unlock

CATransaction unlock example.
- (void) updateUI
{
[CATransaction lock];
[CATransaction setValue:(id)kCFBooleanTrue
                 forKey:kCATransactionDisableActions];
[CATransaction begin];
myLayer.path = [UIBezierPath bezierPathWithOvalInRect:theRect].CGPath;
myLayer.bounds = theBounds;
[CATransaction commit];

[CATransaction flush];
[CATransaction setValue:(id)kCFBooleanFalse
                 forKey:kCATransactionDisableActions];
[CATransaction unlock];
}

Example of [CATransaction unlock].
In your touch handling method, wrap the animations in a transaction and lock:

[CATransaction lock];
[CATransaction begin];

// update the sublayers with new animations

[CATransaction commit];
[CATransaction unlock];

CATransaction unlock example.
CGPathRef newPath = CGPathCreateWithRect(aLayer.frame, nil);
[CATransaction lock];
[CATransaction begin];
[CATransaction setAnimationDuration:5.0f];
CABasicAnimation *ba = [CABasicAnimation animationWithKeyPath:@"path"];
ba.autoreverses = YES;
ba.fillMode = kCAFillModeForwards;
ba.repeatCount = HUGE_VALF;
ba.fromValue = (id)aLayer.path;
ba.toValue = (__bridge id)newPath;
[aLayer addAnimation:ba forKey:@"animatePath"];
[CATransaction commit];
[CATransaction unlock];

End of CATransaction unlock example article.

CATransaction lock example in Objective C (iOS).

CATransaction lock

Attempts to acquire a recursive spin-lock lock, ensuring that returned layer values are valid until unlocked.

+ (void)lock

Discussion of [CATransaction lock]
Core Animation uses a data model that promises not to corrupt the internal data structures when called from multiple threads concurrently, but not that data returned is still valid if the property was valid on another thread. By locking during a transaction you can ensure data that is read, modified, and set is correctly managed.

CATransaction lock example.
- (void) updateUI
{
[CATransaction lock];
[CATransaction setValue:(id)kCFBooleanTrue
                 forKey:kCATransactionDisableActions];
[CATransaction begin];
myLayer.path = [UIBezierPath bezierPathWithOvalInRect:theRect].CGPath;
myLayer.bounds = theBounds;
[CATransaction commit];

[CATransaction flush];
[CATransaction setValue:(id)kCFBooleanFalse
                 forKey:kCATransactionDisableActions];
[CATransaction unlock];
}

Example of [CATransaction lock].
In your touch handling method, wrap the animations in a transaction and lock:

[CATransaction lock];
[CATransaction begin];

// update the sublayers with new animations

[CATransaction commit];
[CATransaction unlock];

CATransaction lock example.
CGPathRef newPath = CGPathCreateWithRect(aLayer.frame, nil);
[CATransaction lock];
[CATransaction begin];
[CATransaction setAnimationDuration:5.0f];
CABasicAnimation *ba = [CABasicAnimation animationWithKeyPath:@"path"];
ba.autoreverses = YES;
ba.fillMode = kCAFillModeForwards;
ba.repeatCount = HUGE_VALF;
ba.fromValue = (id)aLayer.path;
ba.toValue = (__bridge id)newPath;
[aLayer addAnimation:ba forKey:@"animatePath"];
[CATransaction commit];
[CATransaction unlock];

End of CATransaction lock example article.

CATransaction setDisableActions example in Objective C (iOS).

CATransaction setDisableActions

Sets whether actions triggered as a result of property changes made within this transaction group are suppressed.

+ (void)setDisableActions:(BOOL)flag

Parameters
flag
YES, if actions should be disabled.

Discussion of [CATransaction setDisableActions]
This is a convenience method that invokes setValue:forKey: with an NSNumber containing a YES for the kCATransactionDisableActions key.

CATransaction setDisableActions example.
[CATransaction begin];
[CATransaction setAnimationDuration: 1.0/30.0];
[CATransaction setDisableActions: TRUE];
//Put layer changes you want to take place without animation here.
[CATransaction commit];

Example of [CATransaction setDisableActions].
- (void)carouselDidEndScrollingAnimation:(iCarousel *)carousel {
    BOOL previousDisableActions = [CATransaction disableActions];
    [CATransaction setDisableActions:NO];

    // Begin animation here.

    [CATransaction setDisableActions:previousDisableActions];
}

CATransaction setDisableActions example.
[CATransaction setCompletionBlock:^{
    //Readjust button frame for touch area

    CGRect frameRect = self.frame;
    frameRect.origin.x = frameRect.origin.x - offset;
    frameRect.size.width = frameRect.size.width + offset;
    self.frame = frameRect;

    [CATransaction setDisableActions:YES];
    for(CALayer *layer in self.layer.sublayers){
        CGRect rect = layer.frame;
        rect.origin.x = rect.origin.x+offset;
        layer.frame = rect;
    }
    [CATransaction commit];

}];

End of CATransaction setDisableActions example article.

CATransaction disableActions example in Objective C (iOS).

CATransaction disableActions

Returns whether actions triggered as a result of property changes made within this transaction group are suppressed.

+ (BOOL)disableActions

Return Value
YES if actions are disabled.

Discussion of [CATransaction disableActions]
This is a convenience method that returns the boolValue for the valueForKey: value returned by the kCATransactionDisableActions key.

CATransaction disableActions example.
-(void)flagsChanged:(NSEvent *)theEvent
{
    CALayer* layer = self.layer;
    [CATransaction begin];
    CATransaction.disableActions = YES;
    layer.speed = (theEvent.modifierFlags & NSShiftKeyMask) ? 0.1 : 1;
    [CATransaction commit];
}

Example of [CATransaction disableActions].
- (void)carouselDidEndScrollingAnimation:(iCarousel *)carousel {
    BOOL previousDisableActions = [CATransaction disableActions];
    [CATransaction setDisableActions:NO];

    // Begin animation here.

    [CATransaction setDisableActions:previousDisableActions];
}

End of CATransaction disableActions example article.

CATransaction setCompletionBlock example in Objective C (iOS).

CATransaction setCompletionBlock

Sets the completion block object.

+ (void)setCompletionBlock:(void (^)(void))block

Parameters of [CATransaction setCompletionBlock]
block
A block object called when animations for this transaction group are completed.
The block object takes no parameters and returns no value.

Discussion of [CATransaction setCompletionBlock]
The 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 setCompletionBlock example.
static void setX(UIView *view, CGFloat x)
{
    CGRect frame = view.frame;
    frame.origin.x = x;
    view.frame = frame;
}

- (IBAction)startAnimation:(id)sender {
    label.text = @"Animation starting!";
    setX(redView, 0);
    setX(blueView, 0);
    [CATransaction begin]; {
        [CATransaction setCompletionBlock:^{
            label.text = @"Animation complete!";
        }];
        [UIView animateWithDuration:1 animations:^{
            setX(redView, 300);
        }];
        [UIView animateWithDuration:2 animations:^{
            setX(blueView, 300);
        }];
    } [CATransaction commit];
}

Example of [CATransaction setCompletionBlock].
[CATransaction begin];
[CATransaction setAnimationDuration:0.5];
[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[CATransaction setCompletionBlock:^{
    NSLog(@"blabla");
}];

// 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];

CATransaction setCompletionBlock example.
[CATransaction begin]; {

    [CATransaction setCompletionBlock:^{
        // This block runs after any animations created before the call to
        // [CATransaction commit] below.  Specifically, if
        // doMethodOneWhichHasAnimation starts any animations, this block
        // will not run until those animations are finished.

        SchemeView *schemeView = [[SchemeView alloc] init];
            [self.navigationController pushViewController:schemeView animated:YES];
            [schemeView release];
    }];

    // You don't need to modify `doMethodOneWhichHasAnimation`.  Its animations are
    // automatically part of the current transaction.
    [otherClass doMethodOneWhichHasAnimation];

} [CATransaction commit];

End of CATransaction setCompletionBlock example article.

CATransaction commit example in Objective C (iOS).

CATransaction commit

Commit all changes made during the current transaction.

+ (void)commit
Special Considerations
Raises an exception if no current transaction exists.

CATransaction commit example.
[CATransaction begin];
[topLayer addAnimation:topAnimation forKey:@"flip"];
[bottomLayer addAnimation:bottomAnimation forKey:@"flip"];
[CATransaction commit];

Example of [CATransaction commit].
.h

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

+ (void)transactionWithDuration:(NSTimeInterval)duration
                     animations:(void (^)(void))animations;
{
    [CATransaction begin];
    [CATransaction setValue:[NSNumber numberWithFloat:duration] forKey:kCATransactionAnimationDuration];
    animations();
    [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
                                                        :low.y
                                                        :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 commit example.
[CATransaction begin];
[CATransaction setAnimationDuration:0.5];
[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[CATransaction setCompletionBlock:^{
    NSLog(@"blabla");
}];

// 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 commit example article.

CATransaction begin example in Objective C (iOS).

CATransaction begin

Begin a new transaction for the current thread.

+ (void)begin

Discussion of [CATransaction begin]
The transaction is nested within the thread’s current transaction, if there is one.

CATransaction begin example.
[CATransaction begin];
[topLayer addAnimation:topAnimation forKey:@"flip"];
[bottomLayer addAnimation:bottomAnimation forKey:@"flip"];
[CATransaction commit];

Example of [CATransaction begin].
.h

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

+ (void)transactionWithDuration:(NSTimeInterval)duration
                     animations:(void (^)(void))animations;
{
    [CATransaction begin];
    [CATransaction setValue:[NSNumber numberWithFloat:duration] forKey:kCATransactionAnimationDuration];
    animations();
    [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
                                                        :low.y
                                                        :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 begin example.
[CATransaction begin];
[CATransaction setAnimationDuration:0.5];
[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[CATransaction setCompletionBlock:^{
    NSLog(@"blabla");
}];

// 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 begin example article.

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

Parameters
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].
.h

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

+ (void)transactionWithDuration:(NSTimeInterval)duration
                     animations:(void (^)(void))animations;
{
    [CATransaction begin];
    [CATransaction setValue:[NSNumber numberWithFloat:duration] forKey:kCATransactionAnimationDuration];
    animations();
    [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
                                                        :low.y
                                                        :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:^{
    NSLog(@"blabla");
}];

// 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.

CATransaction animationTimingFunction example in Objective C (iOS).

CATransaction animationTimingFunction

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

+ (CAMediaTimingFunction *)animationTimingFunction

Return Value
An instance of CAMediaTimingFunction.

Discussion of [CATransaction animationTimingFunction]
This is a convenience method that returns the CAMediaTimingFunction for the valueForKey: value returned by the kCATransactionAnimationTimingFunction key.

CATransaction animationTimingFunction example.
[CATransaction begin];
CATransaction.animationTimingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
CATransaction.animationDuration = (viewBounds.size.width + box.frame.size.width) / pixelsPerSecond;
// and the layer should cross the viewport to outside of it, too
box.position = CGPointMake(-box.frame.size.width, y);
[CATransaction commit];

Example of [CATransaction animationTimingFunction].
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
animation.duration = [CATransaction animationDuration];
animation.timingFunction = [CATransaction animationTimingFunction];
animation.fromValue = (id)oldPath;
animation.toValue = (id)path;
[self addAnimation:animation forKey:@"pathAnimation"];

End of CATransaction animationTimingFunction example article.

CATransaction setAnimationDuration example in Objective C (iOS).

CATransaction setAnimationDuration

Sets the animation duration used by all animations within this transaction group.

+ (void)setAnimationDuration:(CFTimeInterval)duration

Parameters
duration
An interval of time used as the duration.

Discussion of [CATransaction setAnimationDuration]
You can also set the animation duration for a specific transaction object by calling the setValue:forKey: method of that object and specifying the kCATransactionAnimationDuration key.

CATransaction setAnimationDuration example.
UIView *oldView = [[self subviews] objectAtIndex:0];
UIView *newView = [[self subviews] objectAtIndex:1];

[UIView beginAnimations:@"swapViews" context:nil];
[UIView setAnimationDuration:1];
    oldView.alpha = 0;
    newView.alpha = 1;
[UIView commitAnimations];

Example of [CATransaction setAnimationDuration].
[CATransaction begin];
[CATransaction setAnimationDuration:0.5];
[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[CATransaction setCompletionBlock:^{
    NSLog(@"blabla");
}];

// 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];

CATransaction setAnimationDuration example.
- (void)viewDidLoad {
    [super viewDidLoad];

    //Modifying base layer
    self.view.layer.backgroundColor = [UIColor orangeColor].CGColor;
    self.view.layer.cornerRadius = 20.0;
    self.view.layer.frame = CGRectInset(self.view.layer.frame, 20, 20);

    // Adding layer
    mylayer = [CALayer layer]; //mylayer declared in .h file
    mylayer.bounds = CGRectMake(0, 0, 100, 100);
    mylayer.position = CGPointMake(100, 100); //In parent coordinate
    mylayer.backgroundColor = [UIColor redColor].CGColor;
    mylayer.contents = (id)[UIImage imageNamed:@"glasses"].CGImage;   
    [self.view.layer addSublayer:mylayer];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    [CATransaction begin]; {
        [CATransaction setAnimationDuration:2];
        mylayer.position=CGPointMake(200.0,200.0);
        mylayer.zPosition=50.0;
        mylayer.opacity=0.5;
    } [CATransaction commit];
}

End of CATransaction setAnimationDuration example article.

CATransaction animationDuration example in Objective C (iOS).

CATransaction animationDuration

Returns the animation duration used by all animations within this transaction group.

+ (CFTimeInterval)animationDuration

Return Value
An interval of time used as the duration.

Discussion of [CATransaction animationDuration]
You can retrieve the animation duration for a specific transaction by calling the valueForKey: method of the transaction object and asking for the kCATransactionAnimationDuration key.

CATransaction animationDuration example.
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
animation.duration = [CATransaction animationDuration];
animation.timingFunction = [CATransaction animationTimingFunction];
animation.fromValue = (id)oldPath;
animation.toValue = (id)path;
[self addAnimation:animation forKey:@"pathAnimation"];

Example of [CATransaction animationDuration].
Find out the animation duration of the current animation block
[CATransaction animationDuration] is what you're looking for


End of CATransaction animationDuration example article.