如何在NSImageView中实现NSImage的连续旋转? [英] How do I achieve continuous rotation of an NSImage inside NSImageView?

查看:350
本文介绍了如何在NSImageView中实现NSImage的连续旋转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法完成了旋转动画,并在问题上可以找到带有描述的代码. NSView中的NSImage旋转不起作用

I have managed to finish this rotation animation and code with description can be found on tho question. NSImage rotation in NSView is not working

在继续之前,请对 Duncan C 的答案进行投票.当我设法从他的答案中实现这一轮换时.

Before you proceed please up vote Duncan C 's answer. As I manage to achieve this rotation from his answer.

我有这样的图片

我想继续在另一个线程上旋转此同步图标.现在,我尝试使用Quartz作曲器,并将动画添加到QCView中,但效果非常疯狂,而且速度也很慢.

I want to keep rotating this sync icon, On a different thread. Now I tried using Quartz composer and add the animation to QCView but it is has very crazy effect and very slow too.

如何以较少的处理费用连续旋转此图像?

How do I rotate this image continuously with very less processing expense?

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

I read CoreAnimation, Quartz2D documentation but I failed to find the way to make it work. The only thing I know so far is, I have to use

  • CALayer
  • CAImageRef
  • AffineTransform
  • NSAnimationContext

现在,我不希望有代码,但是对伪代码的理解会很棒!

Now, I am not expecting code, but an understanding with pseudo code will be great!

推荐答案

让对象旋转180度以上实际上有些棘手.问题是您为结束旋转指定了一个转换矩阵,系统决定向另一个方向旋转.

Getting an object to rotate more than 180 degrees is actually a little bit tricky. The problem is that you specify a transformation matrix for the ending rotation, and the system decides to rotate in the other direction.

我要做的是创建一个小于180度的CABasicAnimation,将其设置为可加和重复次数.动画的每个步骤都会使对象更多地动起来.

What I've done is to create a CABasicAnimation of less than 180 degrees, set up to be additive , and with a repeat count. Each step in the animation animates the object more.

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

The following code is taken from an iOS application, but the technique is identical in 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"属性,然后将动画添加到视图的图层中.

CAAnimation objects operate on layers, so for Mac OS, you'll need to set the "wants layer" property in interface builder, and then add the animation to your view's layer.

要使视图永久旋转,可以将重复计数设置为非常大的数字,例如1e100.

To make your view rotate forever, you'd set repeat count to some very large number like 1e100.

创建动画后,可以使用以下代码将其添加到视图的图层中:

Once you've created your animation, you'd add it to your view's layer with code something like this:

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

这就是全部.

这篇关于如何在NSImageView中实现NSImage的连续旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆