让 uiimageView 翻转另一个图像

发布于 12-05 19:38 字数 1299 浏览 0 评论 0原文

这是我的问题。 我想为一张图像制作动画,我想让它围绕另一张图像旋转,但我不知道该怎么做。我只是想让它转动(不是旋转,而是像绕一圈一样转动的运动)。请问我该怎么做?


这是我的代码:

-(void){
CALayer *orbit1 = [CALayer layer];
orbit1.bounds = CGRectMake(0, 0, 200, 200);
orbit1.position = centre.center;
orbit1.cornerRadius = 100;
orbit1.borderColor= [UIColor redColor].CGColor;
orbit1.borderWidth = 1.5;

planet1 = [CALayer layer];
planet1.bounds = CGRectMake(0, 0, 44.0, 20.0);
planet1.position = CGPointMake(160, 25);
planet1.contents = (id)([UIImage imageNamed:@"bouletest_05.png"].CGImage);
[orbit1 addSublayer:planet1];

CABasicAnimation *anim1 = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
anim1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
anim1.fromValue = [NSNumber numberWithFloat:0];
anim1.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
anim1.repeatCount = HUGE_VALF;
anim1.duration = 10.0;
[orbit1 addAnimation:anim1 forKey:@"transform"];

[self.view.layer addSublayer:orbit1];

}

然后:

-(void)creation{

imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"abouffer_03.png"]];
[[self view] addSubview:imageView2];
imageView2.center=planet1.center;
}

看看最后一行:imageView2.center=planet1.center;问题就在这里

here is my question.
I would like to animate an image, I would like to make it turn around another image, but I don't know how to do that. I just want that it turns( not a rotation but a movement of turning like it is turning around a circle).how can I do this please ?


here is my code :

-(void){
CALayer *orbit1 = [CALayer layer];
orbit1.bounds = CGRectMake(0, 0, 200, 200);
orbit1.position = centre.center;
orbit1.cornerRadius = 100;
orbit1.borderColor= [UIColor redColor].CGColor;
orbit1.borderWidth = 1.5;

planet1 = [CALayer layer];
planet1.bounds = CGRectMake(0, 0, 44.0, 20.0);
planet1.position = CGPointMake(160, 25);
planet1.contents = (id)([UIImage imageNamed:@"bouletest_05.png"].CGImage);
[orbit1 addSublayer:planet1];

CABasicAnimation *anim1 = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
anim1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
anim1.fromValue = [NSNumber numberWithFloat:0];
anim1.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
anim1.repeatCount = HUGE_VALF;
anim1.duration = 10.0;
[orbit1 addAnimation:anim1 forKey:@"transform"];

[self.view.layer addSublayer:orbit1];

}

and then :

-(void)creation{

imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"abouffer_03.png"]];
[[self view] addSubview:imageView2];
imageView2.center=planet1.center;
}

look at the last line: imageView2.center=planet1.center; the problem is here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风轻花落早2024-12-12 19:38:12

检查这篇文章:http://nachbaur.com/blog/core-animation-part-4 。在那里您将找到如何沿着任意 UIBezierPath 为视图设置动画的示例。

因此,在您的情况下,您需要围绕“锚”图像创建一个圆形路径:

const CGFloat radius = 100.0f;
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(center.x-radius, center.y-radius, 2*radius, 2*radius)];

并将带有该路径的 CAKeyFrameAnimation 添加到要移动的图像视图。

Check this post: http://nachbaur.com/blog/core-animation-part-4. There you'll find an example of how to animate view along arbitrary UIBezierPath.

So in your case you'll need to create a circle path around your 'anchor' image:

const CGFloat radius = 100.0f;
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(center.x-radius, center.y-radius, 2*radius, 2*radius)];

and add CAKeyFrameAnimation with that path to the image view you want to move.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文