首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在iphone中移动和旋转对象

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

Stack Overflow用户
提问于 2012-10-12 14:26:00
回答 1查看 189关注 0票数 0

我正在用iphone开发一个小游戏...游戏的概念是用加速度计把一个物体放在bar...Rotating的顶部.当条形旋转时,我需要相对于条形移动一个物体。如何实现此concept...Any示例或参考?

旋转两个img:

barImg,objImg //UIImageView

代码语言:javascript
运行
复制
    barImg.transform = CGAffineTransformMakeRotation(Ypos);
 objImg.transform=CGAffineTransformMakeRotation(Ypos);
EN

回答 1

Stack Overflow用户

发布于 2012-10-12 14:48:57

因此,对于带有动画的360度旋转:

代码语言:javascript
运行
复制
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来改变角度。

要移动对象:

代码语言:javascript
运行
复制
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];

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

如果您不想要此效果,可以使用CGPathAddPath。

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

CALayers简介:https://www.raywenderlich.com/3096-calayers-tutorial-for-ios-introduction-to-calayers

苹果公司的CALayer动画文档:https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreAnimation_guide/Introduction/Introduction.html

您还可以观看WWDC 2010的Session 424 (核心动画实践,第1部分)和425 (核心动画实践,第2部分):

https://developer.apple.com/videos/archive/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12853415

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档