2013-05-09 189 views
0

我有一個地球的圖像,我想圍繞Y軸連續旋轉。我也想把它模擬成3D旋轉。我發現了一些使用CABasicLayer和CALayer的代碼,但它們圍繞Z軸旋轉圖像。如何繞Y軸旋轉圖像(球體)?

回答

0

試試這個代碼:

CABasicAnimation *rotateAnimation = [CABasicAnimation animation]; 
rotateAnimation.keyPath = @"transform.rotation.z"; 
rotateAnimation.fromValue = [NSNumber numberWithFloat:DegreesToRadians(0)]; 
rotateAnimation.toValue = [NSNumber numberWithFloat:DegreesToRadians(360)]; 
rotateAnimation.duration = 10; 
rotateAnimation.removedOnCompletion = NO; 
// leaves presentation layer in final state; preventing snap-back to original state 
rotateAnimation.fillMode = kCAFillModeForwards; 
rotateAnimation.repeatCount = 99; 
rotateAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 

// add the animation to the selection layer. This causes it to begin animating 
[imageView.layer addAnimation:rotateAnimation forKey:@"rotateAnimation"]; 
+0

Xcode在第2行和最後一行的程序中表示意外的@。 – Geek 2013-05-09 10:33:00

+0

查看編輯答案 – Anil 2013-05-09 10:34:51

+0

感謝您的回答。但它並不完全符合我的要求。我希望圖像(這是一個地球儀)像一個真正的地球儀一樣旋轉。可能是我需要從不同的角度來看地球的許多圖像。但那不是我主要關心的問題。有沒有什麼辦法像這樣旋轉? – Geek 2013-05-09 10:43:30

0

我最終使用,是因爲我需要的地球儀的不同意見。所有圖像都是從不同角度拍攝的地球視圖。這裏只有5張圖像,但實際上我會使用大約36張圖像,因爲圖像將有10個不同的角度。因此,360/10 = 36.

UIImage *image = [UIImage imageNamed:@"globe.png"]; 
UIImage *image1 = [UIImage imageNamed:@"globe1.jpeg"]; 
UIImage *image2 = [UIImage imageNamed:@"globe2.jpeg"]; 
UIImage *image3 = [UIImage imageNamed:@"globe3.jpeg"]; 
UIImage *image4 = [UIImage imageNamed:@"globe4.jpeg"]; 

NSArray *imageArray = [NSArray arrayWithObjects:image, image1, image2, image3, image4, nil]; 
// set array of images, you want to cycle, to "animationImages" property. 
self.imageView.animationImages = imageArray; 
//duration of the animation-cycle 
self.imageView.animationDuration = 2.0; 
// 0 for endless repeation 
self.imageView.animationRepeatCount = 0; 
//starts the animation 
[self.imageView startAnimating];