我正在完成第 4 版。Hillegass/Preble Cocoa 的书,并且遇到了我在核心动画中无法理解的障碍。
特别是,我有以下循环,它将图像列表(据说一次一张)呈现到图层托管视图中。预期的结果是我应该看到每张图像一次一张地飞出presentImage
。实际结果是视图保持空白,直到循环完成,然后图像一次全部飞出。在延迟期间,我看到多条日志消息表明多次调用presentImage
. 当这些日志消息停止时,表明循环完成,然后所有图像立即飞出。
// loop to present each image in list of URLs
// called at end of applicationDidFinishLaunching
NSTimeInterval t0 = [NSDate timeIntervalSinceReferenceDate];
for(id url in urls) {
NSImage *img = [[NSImage alloc] initWithContentsOfURL:url];
if(!img)
continue;
NSImage *thumbImg = [self thumbImageFromImage:img];
[self presentImage:thumbImg];
NSString *dt = [NSString stringWithFormat:@"%0.1fs",
[NSDate timeIntervalSinceReferenceDate]-t0];
NSLog(@"Presented %@ at %@ sec",url,dt);
}
该thumbImageFromImage
方法完全按照您的想法进行(创建较小的图像)。的代码presentImage
如下:
- (void)presentImage:(NSImage *)image
{
CGRect superlayerBounds = view.layer.bounds;
NSPoint center = NSMakePoint(CGRectGetMidX(superlayerBounds), CGRectGetMidY(superlayerBounds));
NSRect imageBounds = NSMakeRect(0, 0, image.size.width, image.size.height);
CGPoint randomPoint = CGPointMake(CGRectGetMaxX(superlayerBounds)*(double)random()/(double)RAND_MAX, CGRectGetMaxY(superlayerBounds)*(double)random()/(double)RAND_MAX);
CAMediaTimingFunction *tf = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
CABasicAnimation *posAnim = [CABasicAnimation animation];
posAnim.fromValue = [NSValue valueWithPoint:center];
posAnim.duration = 1.5;
posAnim.timingFunction = tf;
CABasicAnimation *bdsAnim = [CABasicAnimation animation];
bdsAnim.fromValue = [NSValue valueWithRect:NSZeroRect];
bdsAnim.duration = 1.5;
bdsAnim.timingFunction = tf;
CALayer *layer = [CALayer layer];
layer.contents = image;
layer.actions = [NSDictionary dictionaryWithObjectsAndKeys:posAnim,@"position", bdsAnim, @"bounds", nil];
[CATransaction begin];
[view.layer addSublayer:layer];
layer.position = randomPoint;
layer.bounds = NSRectToCGRect(imageBounds);
[CATransaction commit];
}
观察到的结果好像[CATransaction begin]
and[CATransaction commit]
调用将for
循环括起来,而不是对addSublayer
inside的调用presentImage
。事实上,如果我从内部删除[CATransaction begin]
and而是将循环括起来,我确实会观察到相同的一次性行为。实际上,如果我完全删除对的调用,我会观察到相同的一次性行为。[CATransaction commit]
presentImage
for
[CATransaction begin]
[CATransaction commit]