In order to get snapshot tests working with animations I need to be able to scrub through CAAnimation added to a layer during - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
I set up a new app to isolate the case, it appears that animation, while being added to the layer, is not having any effect on the layer itself.
Moving the same logic into dispatch_async(dispatch_get_main_queue(), … works as exepcted.
Any ideas what might be the problem?
#import "AppDelegate.h"
@implementation AppDelegate
{
UIWindow *_window;
}
#define BROKEN
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIWindow *const window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
window.rootViewController = [UIViewController new];
window.rootViewController.view.backgroundColor = [UIColor whiteColor];
[window makeKeyAndVisible];
_window = window;
const CGRect bounds = (CGRect){ {0, 0}, {200, 200} };
CAShapeLayer *const shapeLayer = [CAShapeLayer new];
shapeLayer.frame = bounds;
shapeLayer.path = CGPathCreateWithEllipseInRect(bounds, NULL);
shapeLayer.fillColor = [UIColor redColor].CGColor;
[_window.layer addSublayer:shapeLayer];
CABasicAnimation *const anim = [CABasicAnimation animationWithKeyPath:@"fillColor"];
anim.fromValue = (__bridge id)[UIColor blackColor].CGColor;
anim.toValue = (__bridge id)[UIColor whiteColor].CGColor;
anim.duration = 2;
anim.repeatCount = HUGE_VALF;
[shapeLayer addAnimation:anim forKey:@"xxxx"];
shapeLayer.speed = 0;
#ifdef BROKEN
shapeLayer.timeOffset = 1;
#else
dispatch_async(dispatch_get_main_queue(), ^{
shapeLayer.timeOffset = 1;
});
#endif
return YES;
}
@end