gpt4 book ai didi

objective-c - 如何在 NSImageView 中实现 NSImage 的连续旋转?

转载 作者:太空狗 更新时间:2023-10-30 03:28:12 25 4
gpt4 key购买 nike

future 的观众:

我已经设法完成了这个旋转动画,并且可以在这个问题上找到带有描述的代码。 NSImage rotation in NSView is not working

在您继续之前,请投票 Duncan C的回答。当我设法从他的回答中实现这种轮换时。


我有这样一张图片,

enter image description here

我想在不同的线程上继续旋转此同步图标。现在我尝试使用 Quartz Composer 并将动画添加到 QCView,但它的效果非常疯狂,而且速度也很慢。

问题:

如何以极少的处理开销连续旋转此图像?

努力

我阅读了 CoreAnimation 和 Quartz2D 文档,但未能找到使其工作的方法。到目前为止我唯一知道的是,我必须使用

  • CALayer
  • CAImageRef
  • 仿射变换
  • NSAnimationContext

现在,我不期待代码,但理解伪代码会很棒!

最佳答案

让对象旋转超过 180 度实际上有点棘手。问题是你为结束旋转指定了一个变换矩阵,而系统决定向另一个方向旋转。

我所做的是创建一个小于 180 度的 CABasicAnimation,设置为 additive ,并具有重复计数。动画中的每一步都使对象更加生动。

以下代码取自 iOS 应用程序,但该技术在 Mac OS 中是相同的。

  CABasicAnimation* rotate =  [CABasicAnimation animationWithKeyPath: @"transform.rotation.z"];
rotate.removedOnCompletion = FALSE;
rotate.fillMode = kCAFillModeForwards;

//Do a series of 5 quarter turns for a total of a 1.25 turns
//(2PI is a full turn, so pi/2 is a quarter turn)
[rotate setToValue: [NSNumber numberWithFloat: -M_PI / 2]];
rotate.repeatCount = 11;

rotate.duration = duration/2;
rotate.beginTime = start;
rotate.cumulative = TRUE;
rotate.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

CAAnimation 对象在层上运行,因此对于 Mac OS,您需要在界面构建器中设置“wants layer”属性,然后将动画添加到 View 的层。

要使您的 View 永远旋转,您需要将重复计数设置为某个非常大的数字,例如 1e100。

创建动画后,您可以使用如下代码将其添加到 View 层:

[myView.layer addAnimation: rotate forKey: @"rotateAnimation"];

这就是它的全部内容。

关于objective-c - 如何在 NSImageView 中实现 NSImage 的连续旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10826288/

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