2012-07-12 11 views
5

回転にはUIViewAnimationCurveを使用し、位置の変更には別のUIViewAnimationCurveを使用できます。これは可能ですか?同じアニメーションで2つの異なるUIViewAnimationCurvesを使用する

たとえば(擬似コードで)。

// Begin animations 

// Set rotation animation curve to EaseInOut 

// Set position animation curve to Linear 

// Make some changes to position 

// Make some change to the angle of rotation 

// Commit the animations 

EDIT:(CAAnimationGroupを使うアプローチは、以下の提案) - しかし、アニメーションが開始されていない2つの独立したCABasicAnimationsとCAAnimationGroupを使うを作成しました。何か案は?

CABasicAnimation *postionAnimation = [CABasicAnimation animationWithKeyPath:@"position"]; 
postionAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; 
postionAnimation.toValue = [NSValue valueWithCGPoint:[self transformPointToWorldSpaceFromViewSpace:self.spriteView.position]]; 
postionAnimation.delegate = self; 

CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.z"]; 
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
rotationAnimation.toValue = [NSNumber numberWithFloat:self.spriteView.angle -= M_PI_2]; 
rotationAnimation.delegate = self; 

CAAnimationGroup *animationsGroup = [CAAnimationGroup animation]; 
animationsGroup.duration = self.clockSpeed; 
animationsGroup.animations = [NSArray arrayWithObjects:postionAnimation, rotationAnimation, nil]; 

// Perform the animation 
[self.spriteView.layer addAnimation:animationsGroup forKey:nil]; 

答えて

3

あなたの最善の策は、animateWithDurationへの複数の呼び出しを使用することです:遅延:オプション:アニメーション:完了を:.各呼び出しで異なるアニメーションカーブを使用すると、それらは並行して実行されます。または、異なる遅延値を使用して、それらを順番に実行させることができます。

コードは次のようになります。あなたがCAAnimationGroupsを使用している場合

[UIView animateWithDuration: .5 
    delay: 0 
    options: UIViewAnimationOptionCurveEaseInOut 
    animations: ^{ 
    view1.center = CGPointMake(x, y); 
    } 
    completion: ^{ 
    //code that runs when this animation finishes 
    } 
]; 

[UIView animateWithDuration: .5 
    delay: .5 
    options: UIViewAnimationOptionCurveLinear 
    animations: ^{ 
    view2.center = CGPointMake(x2, y2); 
    } 
    completion: ^{ 
    //code that runs when this animation finishes 
    } 
]; 

、いくつかの問題があります。

まず、アニメーショングループは、グループ全体のアニメーションカーブを決定します。

第2に、グループにアニメーションを追加した場合、個々のアニメーションの代理人は取得できません。と呼ばれる。アニメーショングループのデリゲートメソッドだけが呼び出されます。

+0

これは素晴らしいアドバイスです.Duncanに感謝します。明日はそれを行こう。 Dave –

+0

こんにちは。私が持っている唯一の質問は、ブロック内で自分のメソッド[self transformPointToViewSpaceFromWorldSpace:self.spriteView.position]を呼び出すことができますか?それは動作し、楽器はどんなリークも表示しませんが、私はブロック内で自己を使うことについて漠然と覚えています。もう一度ありがとう、Dave。 –

4

2つの異なるアニメーションを作成してみてください。 UIViewのメソッドでは、アニメーションのさまざまなプロパティに対して異なるアニメーションカーブを設定することはできません。 また、CAAnimationsを楽しんで、2つのCABasicAnimationを設定するCAAnimationGroupを作成することもできます。

+0

ありがとうiSofTom、このアプローチ(問題の編集を参照してください)を試みましたが、代理人の方法に従って私のアニメーションは開始していません。何か案は? –

0

OK、最後に解決しました。 iSofTomの方に感謝します(あなたの答えに投票しました)。個々のアニメーションの代理人を設定しましたが、グループは設定しませんでした。

CABasicAnimation *postionAnimation = [CABasicAnimation animationWithKeyPath:@"position"]; 
postionAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; 
postionAnimation.fromValue = [NSValue valueWithCGPoint:self.spriteView.center]; 
postionAnimation.toValue = [NSValue valueWithCGPoint:[self transformPointToViewSpaceFromWorldSpace:self.spriteView.position]]; 

CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
rotationAnimation.fromValue = [NSNumber numberWithFloat:self.spriteView.angle]; 
rotationAnimation.toValue = [NSNumber numberWithFloat:self.spriteView.rotation]; 

CAAnimationGroup *animationsGroup = [CAAnimationGroup animation]; 
animationsGroup.duration = self.clockSpeed; 
animationsGroup.animations = [NSArray arrayWithObjects:postionAnimation, rotationAnimation, nil]; 
animationsGroup.delegate = self; 
// Perform the animation 
[self.spriteView.layer addAnimation:animationsGroup forKey:nil]; 

- (void)animationDidStart:(CAAnimation *)theAnimation { 
radiansToDegrees(self.spriteView.rotation)); 
    self.spriteView.angle = self.spriteView.rotation; 

NSStringFromCGPoint(self.spriteView.position)); 
    self.spriteView.center = [self transformPointToViewSpaceFromWorldSpace:self.spriteView.position]; 
} 
関連する問題