使时钟指针动起来.(回转) [英] Animating a hand of a clock. (Rotation)

查看:97
本文介绍了使时钟指针动起来.(回转)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我将时钟作为UIImageView.那是完美的时钟不会动.我对此感到满意.

So I have my clock as a UIImageView. Thats perfect the clock doesnt move. I am happy with that.

我非常喜欢把时钟作为另一个UIImageView,它放在时钟ImageView的顶部.

I am so have the hand of my clock as another UIImageView and it is placed on top of the clock ImageView.

我想让时钟指针在10秒钟内旋转360度,但棘手的部分是我想使其不旋转在指针20到20的点上ImageView的TopLeft的原点上时针ImageView.

I want to get my Hand of the clock to rotate 360degrees over 10 seconds, but the tricky part is that I want to make it rotate not on the origin at TopLeft of the clockhand ImageView put at a point 20,20 of the clockhand ImageView.

到目前为止,这是我的代码,它可以将时针旋转360度(10秒),但是可以围绕ImageView的左上角旋转.

Here is my code so far, it rotates the clock hand 360degrees over 10seconds, but it rotates it around the topleft of the ImageView.

                CABasicAnimation *rotate;
            rotate = [CABasicAnimation 
animationWithKeyPath:@"transform.rotation"];
            rotate.fromValue = [NSNumber numberWithFloat:0];
            rotate.toValue = [NSNumber numberWithFloat:6.283185307];
            rotate.duration = 10.00;
            rotate.repeatCount = 1;
            needleImageView.layer.anchorPoint = CGPointMake(0.5, 0.5);
            [needleImageView.layer addAnimation:rotate forKey:@"10"];

任何能给我一些提示的人都可以使它绕needleImageView的20,20点旋转吗?

Anybody able to gimme some hints about getting this to rotate around the point 20,20 of needleImageView ?

谢谢,-代码

推荐答案

默认情况下,将相对于中心应用变换.您需要更改图层的anchorPoint才能对其进行更改.检查

By default the transform will be applied with respect to the center. You need to change the layer's anchorPoint to change it. Check link text for details information on it.

重要的一点是,如果更改了anchorPoint,则视图的位置也将更改,因为图层的position属性是相对于锚点的.因此,您需要在更改anchotPoint之后设置旧框架.

One important thing is if you change anchorPoint then the position of the view will also change, as the position property of layer is respect to the anchor point. So you need to set the old frame after changing anchotPoint.

您需要执行以下操作.


// your image is 100 x 100 pixels and you want to make (20, 20) as the transformation center
CGRect oldFrame = myView.frame;
myView.layer.anchorPoint = CGPointMake(0.2, 0.2);
myView.frame = oldFrame

顺便说一句,我还没有填写代码,只是键入了一下,所以可能会有一些错字.

BTW, I have not complied the code, just typed so there might be some typo.

这篇关于使时钟指针动起来.(回转)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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