【发布时间】:2016-07-24 19:30:59
【问题描述】:
我着手改造一个 NSImageView。我最初的尝试是
self.imageView.wantsLayer = YES;
self.imageView.layer.transform = CATransform3DMakeRotation(1,1,1,1);
不幸的是,我注意到转换只发生有时(可能每 5 次运行一次)。添加一个 NSLog 以确认在某些运行中 self.imageView.layer 为空。整个项目的状态如下图所示。
这是一个非常简单的 200x200 NSImageView,带有一个到生成的 NSViewController 的出口。一些实验表明设置 wantDisplay 不能解决问题,但是将转换放在 NSTimer 上使其每次都能正常工作。我很想解释为什么会发生这种情况(我认为这是由于某种竞争条件)。
我在 macOS 10.12 上使用 Xcode 8,但我怀疑这是问题的原因。
更新
在 Interface Builder 中删除 wantsLayer 并疯狂启用 Core Animation Layers 并没有解决问题。
也没有尝试对其进行动画处理(我不确定我希望得到什么)
// 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];
【问题讨论】:
标签: macos cocoa core-animation calayer nsview