2016-07-24 73 views
1

我开始转换NSImageView。我最初的尝试是层支持的NSView问题viewDidLoad

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

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

Project state

这是一个令人难以置信的简单200x200的NSImageView具有出生成NSViewController。一些实验显示设置wantsDisplay不能解决问题,但将转换放在NSTimer上使其每次都能正常工作。我很乐意解释为什么会发生这种情况(我认为这是由于一些竞赛状况)。

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

更新

删除wantsLayer和疯狂让Interface Builder中的Core Animation层并没有解决问题。

Ticked 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]; 

回答

0

与allowsImplicitAnimation试验后,我意识到我可能试图过早动画。

将变换代码移动到viewDidAppear使其每次都能正常工作。

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