如何在iphone中移动和旋转对象

时间:2012-10-12 06:26:00

标签: iphone

我正在开发iphone中的小游戏......游戏概念是将一个对象放在栏顶部...使用加速度计旋转一个栏。当条形旋转时,我需要相对于条移动一个物体。如何实现这个概念......任何例子或参考?

旋转img:

barImg,objImg // UIImageView

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

1 个答案:

答案 0 :(得分:0)

所以用动画旋转360度

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"];

你可以使用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];

上面的示例将对象层移动到路径上。但是CGPathAddQuadCurveToPoint使路径不是对角线而是圆形路径(曲线)。

如果您不想要这种效果,可以使用CGPathAddPath

<强>更新:

如果您是iPhone新手,我会从图层教程开始,了解CALayer背后的想法,然后看看CALayer动画

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

来自Apple的CALayer动画文档: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreAnimation_guide/Introduction/Introduction.html

您还可以观看WWDC 2010的会话424和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

相关问题