2011-09-21 76 views
0

这是我的问题。 我想制作一张图片的动画,我想让它绕过另一张图片,但我不知道该怎么做。我只想让它变成(不是旋转,而是旋转的动作,就像它绕着一个圆圈旋转一样)。我怎么能这样做?mak a uiimage查看其他图片


这里是我的代码:

-(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;问题就在这里

+2

检查[石英2D节目指南(http://developer.apple.com/library/mac/documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_affine/dq_affine.html#//apple_ref/doc/uid/TP30001066-CH204 -SW1)。它很好地解释了它。 – ender

回答

3

查看帖子: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。

+0

对不起,但我有一个小问题,当我想添加CAKeyFrameAnimation与图像视图的路径我不知道该怎么办请帮助我 –

+0

@Valentin,跟随博客文章链接 - 它显示了如何添加动画。那里描述的是什么具体问题? – Vladimir

+0

是的,我从来没有使用bezierPAth等......当我写在xcode - (void){那么你的代码}它不起作用没有发生在屏幕上 –