【问题标题】:Layer backed NSView problems in viewDidLoadviewDidLoad 中层支持的 NSView 问题
【发布时间】: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


    【解决方案1】:

    在尝试了 allowImplicitAnimation 之后,我意识到我可能过早地尝试制作动画。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      • 1970-01-01
      • 2018-08-17
      • 2013-06-16
      • 1970-01-01
      • 2013-02-04
      • 2015-05-22
      相关资源
      最近更新 更多