0

iPhone で小さなゲームを開発しています...ゲームのコンセプトは、バーの上にオブジェクトを配置することです...加速度計を使用してバーを回転させます。バーが回転するとき、バーに対してオブジェクトを移動する必要があります。この概念を実装する方法...例や参照はありますか?

両方の画像を回転させる:

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 のセッション 424 (Core Animation in Practice、パート 1) および 425 (Core Animation in Practice、パート 2) も視聴できます:
https://developer.apple.com/videos/archive/

于 2012-10-12T06:48:56.500 に答える