旋转持有 NSImageView 的 NSView
我正在创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个问题的答案是 NSView/NSImageView 没有支持层来执行动画。
您可以这样设置:
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:
您只是定义动画,实际上并没有调用执行动画的动画块。您需要调用以下之间定义的块:
才能实际运行动画。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:
To actually run the animation.Edit01: My bad. I didn't pay attention to the tags and added the answer for the iPhone API.