旋转持有 NSImageView 的 NSView

发布于 2024-08-21 13:47:31 字数 531 浏览 1 评论 0原文

我正在创建一个 NSView,它在其 drawRect 方法中创建并添加一个 NSImageView 作为子视图。

我想旋转这个 NSImageView (circleView) 或 [self]。因此,在另一种方法中,我试图这样做:

-(void)startAnimation {
CABasicAnimation* spinAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
spinAnimation.toValue = [NSNumber numberWithFloat:5*2*M_PI];
spinAnimation.duration = 2;
[circleView.layer addAnimation:spinAnimation forKey:@"spinAnimation"];
}

但是,调用该方法时,此代码不会执行任何操作。我做错了什么吗?我尝试了 self.layer 和 CircleView.layer 但似乎都不起作用......

I am creating an NSView, which in its drawRect method creates and adds an NSImageView as a subview.

I would like to rotate this NSImageView (circleView), or [self]. So in another method, I am trying to do that:

-(void)startAnimation {
CABasicAnimation* spinAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
spinAnimation.toValue = [NSNumber numberWithFloat:5*2*M_PI];
spinAnimation.duration = 2;
[circleView.layer addAnimation:spinAnimation forKey:@"spinAnimation"];
}

However, this code doesn't do anything when the method is called. Am I doing something wrong? I tried self.layer and circleView.layer but neither seem to work...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅笑依然 2024-08-28 13:47:31

这个问题的答案是 NSView/NSImageView 没有支持层来执行动画。

您可以这样设置:

[NSView setWantsLayer:YES];

The answer to this issue is that the NSView/NSImageView didn't have a backing layer to do the animation with.

You set this with:

[NSView setWantsLayer:YES];
浪推晚风 2024-08-28 13:47:31

您只是定义动画,实际上并没有调用执行动画的动画块。

您需要调用以下之间定义的块:

+ beginAnimations:context:
+ commitAnimations

才能实际运行动画。

Edit01:我的错。我没有注意标签并添加了iPhone API的答案。

Your're just defining the animation, you're not actually calling the animation block that performs the animation.

You need to call a block defined between:

+ beginAnimations:context:
+ commitAnimations

To actually run the animation.

Edit01: My bad. I didn't pay attention to the tags and added the answer for the iPhone API.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文