2012-10-12 47 views
0

我正在iPhone上開發一個小遊戲...遊戲概念是將一個對象置於酒吧頂部...使用加速度計旋轉酒吧。當酒吧旋轉時,我需要相對於酒吧移動一個物體。如何實現這個概念...任何示例或參考?如何在iPhone中移動和旋轉對象

旋既IMG:

barImg,objImg //的UIImageView

barImg.transform = CGAffineTransformMakeRotation(Ypos); 
objImg.transform=CGAffineTransformMakeRotation(Ypos); 

回答

0

所以動畫

CABasicAnimation *rotationAnimation; 
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 
rotationAnimation.toValue = [NSNumber numberWithFloat:-M_PI * 2.0]; // full rotation*/ * rotations * duration ]; 
rotationAnimation.duration = 1; 
rotationAnimation.cumulative = YES; 
rotationAnimation.repeatCount = MAXFLOAT; 

[rotatingTelIamgeview.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; 

旋轉360度,你可以用toValue發揮改變角度。

對於移動物體

CGPoint startPoint = CGPointMake(lvMessageInputBar.animationBubbleImageView.layer.frame.origin.x+lvMessageInputBar.animationBubbleImageView.layer.frame.size.width/2 
           , lvMessageInputBar.animationBubbleImageView.layer.frame.origin.y +lvMessageInputBar.animationBubbleImageView.layer.frame.size.height/2); 
CGPoint endPoint = CGPointMake(endPoint.origin.x+Endpoint.size.width/2, bubleRect.origin.y+bubleRect.size.height/2); 
CGPoint middlePoint = // any point between start and end corrdinates  
CGMutablePathRef path = CGPathCreateMutable(); 
CGPathMoveToPoint(path, NULL, startPoint.x, startPoint.y); 
CGPathAddQuadCurveToPoint(path, NULL,middlePoint.x, middlePoint.y,endPoint.x,endPoint.y); 

CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
pathAnimation.delegate = self; 
pathAnimation.removedOnCompletion = NO; 
pathAnimation.path = path; 
[pathAnimation setCalculationMode:kCAAnimationCubic]; 
[pathAnimation setFillMode:kCAFillModeForwards]; 
pathAnimation.duration = 0.3; 

[lvMessageInputBar.animationBubbleImageView.layer addAnimation:pathAnimation forKey:nil]; 

上面ecample在path.but CGPathAddQuadCurveToPoint使得路徑不對角線而圓形路徑(曲線)移動物體的裏邊反層。

你可以使用CGPathAddPath如果你不想要這個效果

更新:

如果你是新的IPhone我將與層教程開始落後CALayer的想法,再看看CALayer的動畫

CALayers介紹: http://www.raywenderlich.com/2502/introduction-to-calayers-tutorial

從蘋果的CALayer的動畫文件: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreAnimation_guide/Introduction/Introduction.html

您還可以觀看會話424的WWDC 2010這是關於核心動畫425: https://deimos.apple.com/WebObjects/Core.woa/BrowsePrivately/adc.apple.com.4088182973.04088182975.4092394252?i=2079986024

https://deimos.apple.com/WebObjects/Core.woa/BrowsePrivately/adc.apple.com.4088182973.04088182975.4092394254?i=1624423095

+0

感謝答案..但我沒有得到什麼bubleRect(其爲對象? ),位置等。PLZ用命令更新你的答案?在哪裏使用酒吧和對象.. – JohnRaja

+0

我的意思是終點與它actullay –

+0

我新來的iPhone ..任何參考鏈接.. – JohnRaja