6

iOSでサブクラスをkCATransitionPush使用してどのように実装しますか?CAAnimation

CAAnimation *animation;
// How do you create an animation that does the same than:
// CATransition *animation = [CATransition animation];
// [animation setType:kCATransitionPush];        
[self.view.layer addAnimation:animation forKey:nil];

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];        
[self.view addSubview:change];
[UIView commitAnimations];

UIViewアニメーション使用できることは承知していますが、ゼロからの移行を実装できれば、CoreAnimationをよりよく理解するのに役立ちます。kCATransitionPush

4

2 に答える 2

11

2つのレイヤーでアニメーションを同時に実行するには、各レイヤーに適切なCAAnimationGroupを追加する必要があります。

[nextView.layer addAnimation:nextViewAnimation forKey:nil];
[currentView.layer addAnimation:currentViewAnimation forKey:nil];

nextViewAnimationは次のようになります。

CAAnimationGroup *nextViewAnimation = [CAAnimationGroup animation];
NSMutableArray *nextAnimations = [NSMutableArray array];

[nextAnimations addObject:[self opacityAnimation:YES]];  // fade in

CGPoint fromPoint = CGPointMake(forward ? nextView.center.x + nextView.frame.size.width : nextView.center.x - nextView.frame.size.width, nextView.center.y);
[nextAnimations addObject:[self positionAnimationFromPoint:fromPoint toPoint:nextView.center]];  // traslation in

nextViewAnimation.animations = nextAnimations;

およびcurrentViewAnimation:

CAAnimationGroup *currentViewAnimation = [CAAnimationGroup animation];
NSMutableArray *currentAnimations = [NSMutableArray array];
[currentSceneAnimations addObject:[self opacityAnimation:NO]]; // fade out

CGPoint toPoint = CGPointMake(forward ? currentView.center.x - currentView.frame.size.width : currentView.center.x + currentView.frame.size.width, currentView.center.y);
    [currentAnimations addObject:[self positionAnimationFromPoint:currentView.center toPoint:toPoint]];  // traslation out

currentViewAnimation.animations = currentAnimations;

これらのメソッドは、基本的なアニメーションを作成します。

- (CABasicAnimation *)opacityAnimation:(BOOL)fadeIn {
CABasicAnimation *a = [CABasicAnimation animationWithKeyPath:@"opacity"];
a.fromValue = [NSNumber numberWithFloat:fadeIn ? 0.0 : 1.0];
a.toValue = [NSNumber numberWithFloat:fadeIn ? 1.0 : 0.0];
return a;
}

- (CABasicAnimation *)positionAnimationFromPoint:(CGPoint)fromPoint toPoint:(CGPoint)toPoint {
CABasicAnimation *a = [CABasicAnimation animationWithKeyPath:@"position"];
a.fromValue = [NSValue valueWithCGPoint:fromPoint];
a.toValue = [NSValue valueWithCGPoint:toPoint];
return a;
}

ブールフォワードを使用すると、「左から」または「右から」の遷移をシミュレートできます。

于 2011-10-24T15:27:31.053 に答える
4

デフォルトkCATransitionPushには確かにフェードが含まれています。独自のトランジションを複製するには、CABasicAnimation最初にプッシュアニメーションがどのように機能するかを理解する必要があります。テストせずにこれを行うので、これはオフになっている可能性がありますが、サブレイヤーBがサブレイヤーAに置き換わるアニメーションを正しく覚えている場合は、次のように機能します。

  • レイヤーAが元の位置から右に移動します
  • レイヤーBがAの右から元の位置に移動します
  • アニメーションレイヤーBの前半は、不透明度を0から1にアニメーション化します。
  • アニメーションレイヤーAの後半では、不透明度を1から0までアニメーション化します(もちろん、ここでは任意の方向にに置き換えることができます)。

CABasicAnimation1つのプロパティのアニメーションのみをサポートしているためCAAnimationGroup、4つの異なるアニメーションを制御するを作成する必要があります。

次のように、位置と不透明度のアニメーションを作成します。

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];
anim.fromValue = [NSValue valueWithCGPoint:startPoint];
anim.toValue = [NSValue valueWithCGPoint:endPoint];

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"opacity"];
anim.fromValue = [NSNumber numberWithFloat:0.0];
anim.toValue = [NSNumber numberWithFloat:1.0];
于 2011-04-12T22:52:16.413 に答える