2011-09-21 32 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){那麼你的代碼}它不起作用沒有發生在屏幕上 –