如何旋转针(声级计)的原点? [英] How to rotate a needle (for a sound level meter) about it origin?

查看:139
本文介绍了如何旋转针(声级计)的原点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是我到目前为止的情况。我已经将UIImageView添加到Interface Builder并将其设置为针的一个.png图像(Needle.png),我还将它连接到Xcode中相应的IBOutlet。

Hi so this is my situation up to now. I've added a UIImageView to Interface Builder and set it to a .png image of a needle (Needle.png), I've also connected it to the corresponding IBOutlet in Xcode.

在我的viewDidLoad:方法中我设置了锚点,

In my viewDidLoad: method I set the anchor point,

- (void)viewDidLoad {
    [super viewDidLoad];

    [needle.layer  setAnchorPoint:CGPointMake( 0.5, 1 )];
}

我还在Interface Builder中创建了一个按钮并将其连接到Xcode中的IBAction,此按钮执行动画块,

And I've also created a button in Interface Builder and connected it to an IBAction in Xcode, this button performs an animation block,

- (IBAction)animate {

 [UIView beginAnimations:@"rotate" context:nil];
 [UIView setAnimationDuration:1.0];
 self.needle.transform = CGAffineTransformMakeRotation(10 * M_PI / 180);
 [UIView commitAnimations];

}

所以当我按下动画按钮时,我的针图像会旋转逆时针10°,这是一个开始。但我真正想要的是让针根据不断变化的值不断地将自身旋转到正确的角度。

So when I press the animate button my needle image rotates by 10° counter-clockwise, so thats a start. But what I really want to do it to have the needle constantly rotating itself to the correct angle based off a constantly changing value.

例如,如果我有一个分贝值,给了我房间里的当前分贝水平,我想要那个针反映那个值(即10°== 10dB),并且由于这个值不断变化,我希望我的针顺时针或逆时针旋转。

For example if I have a decibel value that gives me the current decibel level in the room, I'd like that needle to reflect that value (i.e. 10° == 10dB), and since this value is constantly changing I'd want my needle to rotate clockwise or counter-clockwise accordingly.

有谁知道我怎么能这样做?如果有人可以发布一些源代码来帮助我,我也很感激。

Does anyone know how I could do that? I'd also appreciate it if someone could post some source code to help me out.

推荐答案


但我真正想要的是
让针头不断旋转
本身到正确的角度,基于
a不断变化的价值。

But what I really want to do it to have the needle constantly rotating itself to the correct angle based off a constantly changing value.

当不断变化的值发生变化时,更改旋转值。

Change the rotation value when the "constantly changing value" changes.

对此值的变化有什么刺激?放置代码以更改那里的旋转。

What is the stimulus to the change of this value? Place the code to change the rotation there.

如果值是KVO - 您可以使用该机制在KVO更改时更改旋转。

If the value is a KVO - you could use that mechanism to change the rotation when the KVO changes.

此外 - 听起来您可能想要在动画块中进行更改,以使针的移动更加流畅。比如说,值只会每秒更改一次 - 在动画块中进行更改,使其在一秒钟内更改。它们的针运动将不那么生涩。

Also - sounds like you may want to do the change in an animation block, to make the needle movements more fluid. Say for example the value only changes once a second - make the change in an animation block that makes it change over the course of a second. They movement of the needle will be less "jerky" that way.

预先警告,如果你正在做动画,设置setAnimationBeginsFromCurrentState - 所以如果你改变了值(并且重新-animate)在现有动画的中途,它将做正确的事情。

Be forewarned that if you are doing animation, set the setAnimationBeginsFromCurrentState - so if you change values (and re-animate) halfway through an existing animation, it will "do the right thing".

这篇关于如何旋转针(声级计)的原点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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