gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:40:42 24 4
gpt4 key购买 nike

我在 Objective c 中有一个 cocoa mac 应用程序。

当我按下 NSButton 时,我想用动画顺时针旋转它几秒钟。

我尝试了下面的代码但不起作用。

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

我进行了很多搜索,但找不到合适的解决方案。

有人可以帮我解决这个问题吗?

最佳答案

在制作动画之前添加[btnScan setWantsLayer:YES];。 NSView 与 UIView 不同,默认情况下没有支持层。

来自Apple Docs :

In iOS apps, Core Animation is always enabled and every view is backed by a layer. In OS X, apps must explicitly enable Core Animation support by doing the following:

  • Link against the QuartzCore framework. (iOS apps must link against this framework only if they use Core Animation interfaces explicitly.)

  • Enable layer support for one or more of your NSView objects by doing one of the following:

    • In your nib files, use the View Effects inspector to enable layer support for your views. 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.

关于objective-c - NSButton在cocoa mac应用程序中顺时针旋转动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31027439/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com