CocoaHeads Paris - Amadour Griffais - CATransaction: What the flush?!

Post on 25-Jun-2015

3.489 views 0 download

Tags:

description

Slides of the short talk I gave at the January 2014 session of CocoaHeads Paris. On Core Animation and CATransaction.

Transcript of CocoaHeads Paris - Amadour Griffais - CATransaction: What the flush?!

CATransactionWhat the flush ?!

Amadour GriffaisCocoaHeads Paris

9 janvier 2014

Core Animation

• crée et manipule des objets graphiques

• les affiche

• les anime

Core Animation

Core Animation

Core Animation

Core Animation

Core Animation

CALayer

CAAnimation

Ab

Model

Core Animation

CALayer

CAAnimation

Ab

Model Render server

C++

Core Animation

CALayer

CAAnimation

Ab

Model Render server

C++

Core Animation

CALayer

CAAnimation

Ab

Model

Core Animation

CALayer

CAAnimation

Ab

Model

CATransaction

Core Animation Programming GuideExplicit Transactions Let You Change Animation Parameters

[CATransaction begin];[CATransaction setAnimationDuration:0.5];layer.position = CGPoint(100,100);[CATransaction commit];

CATransaction

Model

Ab

CATransaction

Model

Ab0.5s

CATransaction

Core Animation Programming GuideExplicit Transactions Let You Change Animation Parameters

[CATransaction begin];[CATransaction setAnimationDuration:0.5];layer.position = CGPoint(100,100);[CATransaction commit];

Disable Actions Temporarily Using the CATransaction Class

[CATransaction begin];[CATransaction setDisableActions:YES];layer.position = CGPoint(100,100);[CATransaction commit];

CATransaction

Model

Ab

CATransaction

Model

Ab

CATransaction Class Reference

CATransaction Class Reference

Model Render server

Model Render server

Ab

[CATransaction flush];

Model Render server

Ab

[CATransaction flush];

Model Render server

AbAb

[CATransaction flush];

• Commit de la transaction implicite

• Englobe toutes les transaction explicites

• Envoie les modifications au render server

[CATransaction flush];

• A chaque tour de runloop

• Provoque layout si nécessaire

[CATransaction flush];

• A chaque tour de runloop

• Provoque layout si nécessaire

• Provoque display si nécessaire

[CATransaction flush];

• Equivalent à [CATransaction commit];

• Attend la fin des transaction explicites

• Définit le beginTime des animations !

[CATransaction begin];[CATransaction setAnimationDuration:0.5];

CAAnimation* anim = [CABasicAnimation animationWithKeyPath:@"opacity"];[layer addAnimation:anim forKey:@"opacity"];//<CABasicAnimation:0xc028980; duration = 0.5; keyPath = opacity>

[CATransaction commit];//<CABasicAnimation:0xc028980; duration = 0.5; keyPath = opacity>

[CATransaction flush];//<CABasicAnimation:0xc028980; beginTimeMode = absolute;// beginTime = 11797.4; duration = 0.5; keyPath = opacity>

En pratique

Déclencher une animation d’attente avant de bloquer le main thread

[activityIndicator startAnimating];[self doReallyLongStuff];

En pratique

Déclencher une animation d’attente avant de bloquer le main thread

[activityIndicator startAnimating];[CATransaction flush];[self doReallyLongStuff];

En pratique

Appliquer des animations implicites à des layers qui viennent d’être ajoutés

CALayer* layer = [CALayer layer];layer.frame = CGRectMake(0, 0, 200, 200);layer.backgroundColor = [[UIColor redColor] CGColor];[self.view.layer addSublayer:layer];layer.frame = CGRectMake(100, 100, 200, 200);

En pratique

Appliquer des animations implicites à des layers qui viennent d’être ajoutés

CALayer* layer = [CALayer layer];layer.frame = CGRectMake(0, 0, 200, 200);layer.backgroundColor = [[UIColor redColor] CGColor];[self.view.layer addSublayer:layer];[CATransaction flush];layer.frame = CGRectMake(100, 100, 200, 200);

En pratique

Faire un rendu dans un contexte en background et nettoyer derrière soi

CALayer* layer = ...;dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ UIGraphicsBeginImageContext(layer.bounds.size); CGContextRef context = UIGraphicsGetCurrentContext(); [layer renderInContext:context]; UIImage* image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); dispatch_sync(dispatch_get_main_queue(), ^{ [self useImage:image]; });});

En pratique

Faire un rendu dans un contexte en background et nettoyer derrière soi

CALayer* layer = ...;dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ UIGraphicsBeginImageContext(layer.bounds.size); CGContextRef context = UIGraphicsGetCurrentContext(); [layer renderInContext:context]; [CATransaction flush]; UIImage* image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); dispatch_sync(dispatch_get_main_queue(), ^{ [self useImage:image]; });});

En pratique ?Faire un rendu dans un contexte en background et nettoyer derrière soi

CALayer* layer = ...;dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ [CATransaction lock]; UIGraphicsBeginImageContext(layer.bounds.size); CGContextRef context = UIGraphicsGetCurrentContext(); [layer renderInContext:context]; [CATransaction unlock]; [CATransaction flush]; UIImage* image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); dispatch_sync(dispatch_get_main_queue(), ^{ [self useImage:image]; });});

Questions ?

• Ressources

• WWDC videos

• Core Animation Programming Guide

@amadour