简体   繁体   English

NSButton在cocoa mac应用程序中顺时针旋转动画

[英]NSButton rotate animation clockwise in cocoa mac application

I am having a cocoa mac application in objective c.我在目标 c 中有一个可可 mac 应用程序。

When I press my NSButton, I want to rotate it clockwise with animation for some seconds.当我按下我的 NSButton 时,我想用动画顺时针旋转它几秒钟。

I tried below code but not working.我试过下面的代码但没有工作。

CABasicAnimation *ani = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    ani.fromValue = [NSNumber numberWithFloat:0];
    ani.toValue = [NSNumber numberWithFloat:-M_PI*2];
    [btnScan.layer setAnchorPoint:CGPointMake(0.5, 0.5)];
    ani.duration = 2.0; // seconds
    ani.repeatCount = HUGE_VAL;
    [btnScan.layer addAnimation:ani forKey:nil];

I searched a lot but couldn't find a proper solution for this.我搜索了很多,但找不到合适的解决方案。

Can anyone help me on this, please?任何人都可以帮助我吗?

Add [btnScan setWantsLayer:YES];添加[btnScan setWantsLayer:YES]; before you do your animation.在你做动画之前。 NSView unlike UIView doesn't have a backing layer by default. NSView 与 UIView 不同,默认情况下没有支持层。

from Apple Docs :来自苹果文档

In iOS apps, Core Animation is always enabled and every view is backed by a layer.在 iOS 应用程序中,核心动画始终处于启用状态,并且每个视图都由一个层支持。 In OS X, apps must explicitly enable Core Animation support by doing the following:在 OS X 中,应用程序必须通过执行以下操作显式启用 Core Animation 支持:

  • Link against the QuartzCore framework.链接 QuartzCore 框架。 (iOS apps must link against this framework only if they use Core Animation interfaces explicitly.) (只有当 iOS 应用明确使用 Core Animation 接口时,它们才必须链接到这个框架。)

  • Enable layer support for one or more of your NSView objects by doing one of the following:通过执行以下操作之一,为一个或多个 NSView 对象启用层支持:

    • In your nib files, use the View Effects inspector to enable layer support for your views.在您的 nib 文件中,使用视图效果检查器为您的视图启用图层支持。 The inspector displays checkboxes for the selected view and its subviews.检查器显示选定视图及其子视图的复选框。 It is recommended that you enable layer support in the content view of your window whenever possible.建议您尽可能在窗口的内容视图中启用图层支持。

    • For views you create programmatically, call the view's setWantsLayer: method and pass a value of YES to indicate that the view should use layers.对于以编程方式创建的视图,调用视图的 setWantsLayer: 方法并传递 YES 值以指示视图应使用图层。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM