0

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

旋转两个img:

barImg,objImg //UIImageView

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

1 回答 1

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 介绍: https ://www.raywenderlich.com/3096-calayers-tutorial-for-ios-introduction-to-calayers

来自 Apple 的 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/

于 2012-10-12T06:48:56.500 回答