viewDidLoad 中图层支持的 NSView 问题

Layer backed NSView problems in viewDidLoad

我着手改造一个NSImageView。我最初的尝试是

self.imageView.wantsLayer = YES;
self.imageView.layer.transform = CATransform3DMakeRotation(1,1,1,1);

不幸的是,我注意到转换仅有时发生(可能每 5 次运行一次)。在确认在某些运行 self.imageView.layer 之间添加 NSLog 为空。整个项目的状态如下图所示。

这是一个非常简单的 200x200 NSImageView,带有生成的 NSViewController 的出口。一些实验表明设置 wantsDisplay 并不能解决问题,但是将转换放在 NSTimer 上可以使其每次都有效。我很想解释为什么会发生这种情况(我认为这是由于某些竞争条件)。

我在 macOS 10.12 上使用 Xcode 8,但我怀疑这是问题的原因。

更新

删除 wantsLayer 并疯狂启用 Interface Builder 中的核心动画层并没有解决问题。

也没有尝试对其进行动画处理(我不确定我希望得到什么)

// Sometimes works.. doesn't animate
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
    context.duration = 1;
    self.imageView.animator.layer.transform = CATransform3DMakeRotation(1,1,1,1);
} completionHandler:^{
    NSLog(@"Done");
}];

// Animates but only sometimes
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(1,1,1,1)];
animation.duration = 1;
[self.imageView.layer addAnimation:animation forKey:nil];

在尝试使用 allowsImplicitAnimation 之后,我意识到我可能过早尝试制作动画。

将转换代码移至 viewDidAppear 使其每次都能正常工作。

- (void)viewDidAppear {
    [super viewDidAppear];
    self.imageView.animator.layer.transform = CATransform3DMakeRotation(1,1,1,1);
}