让 uiimageView 翻转另一个图像
这是我的问题。 我想为一张图像制作动画,我想让它围绕另一张图像旋转,但我不知道该怎么做。我只是想让它转动(不是旋转,而是像绕一圈一样转动的运动)。请问我该怎么做?
这是我的代码:
-(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
检查这篇文章:http://nachbaur.com/blog/core-animation-part-4 。在那里您将找到如何沿着任意 UIBezierPath 为视图设置动画的示例。
因此,在您的情况下,您需要围绕“锚”图像创建一个圆形路径:
并将带有该路径的 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:
and add CAKeyFrameAnimation with that path to the image view you want to move.