简体   繁体   English

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

[英]How to move and rotate object in iphone

I am developing a small game in iphone... Game concept is place an object in top of the bar...Rotating a bar using accelerometer. 我正在用iPhone开发一个小游戏...游戏概念是将对象放置在条形图的顶部...使用加速度计旋转条形图。 When bar rotating , i need to move an object with respect to bar. 条旋转时,我需要相对于条移动一个对象。 How to implement this concept...Any examples or references? 如何实现这个概念...任何示例或参考?

Rotating both img: 旋转两个img:

barImg,objImg //UIImageView barImg,objImg // UIImageView

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

So for rotating 360 Degree with animation: 因此,通过动画旋转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"];

you can play with toValue for changing the angle. 您可以使用toValue更改角度。

For moving an object: 对于移动对象:

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

The above example moves the layer of the object over a path.but CGPathAddQuadCurveToPoint makes the path not diagonal rather circular path (curve). 上面的示例在路径上移动对象层。但是CGPathAddQuadCurveToPoint使路径不是对角线而是圆形路径(曲线)。

you can use CGPathAddPath if you do not want this effect. 如果您不希望这种效果,可以使用CGPathAddPath。

If you are new to iPhone I would start with layer tutorial to get the idea behind CALayer and then take a look CALayer animations 如果您不熟悉iPhone,我将从层教程开始,以了解CALayer的概念,然后看一下CALayer动画

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

CALayer Animation documentation of from Apple: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreAnimation_guide/Introduction/Introduction.html Apple的CALayer动画文档: https : //developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreAnimation_guide/Introduction/Introduction.html

You can also watch Session 424 (Core Animation in Practice, Part 1) and 425 (Core Animation in Practice, Part 2) of WWDC 2010: 您还可以观看WWDC 2010的Session 424(实践中的核心动画,第1部分)和425(实践中的核心动画,第2部分):
https://developer.apple.com/videos/archive/ https://developer.apple.com/videos/archive/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM