CALayerアニメーション
9188 ワード
CALayerアニメーション
『iosコアアニメーションの高度なテクニック』の本からのいくつかの例のまとめは、暗黙的なアニメーション 明示的なアニメーション バッファ-アニメーション速度変化関数 https://zsisme.gitbooks.io/ios-/content/chapter7/transactions.html
暗黙的なアニメーション
CALayerのアニメーション可能なプロパティを変更すると、すぐに画面に表示されません.逆に、以前の値から新しい値にスムーズに移行します.これらはすべてデフォルトの動作であり、追加の操作は必要ありません.
暗黙的なアニメーションlayerのbackgroundColorを変更することでカラーグラデーションのアニメーションを実現
トランザクションの完了block
view関連layerは暗黙的なアニメーションを無効にしました
デフォルトのviewで関連付けられたViewではlayerの暗黙的なアニメーションは無効になっています.簡単な方法はactionを設定して暗黙的なアニメーションの効果を達成することです.
レイヤカラーは、以前にスムーズに遷移したアニメーションではなく、新しい値に瞬時に切り替わります.
デフォルトの暗黙的なアニメーションの動作を設定できます.暗黙的なアニメーションをオフにすると、viewに関連付けられていないlayerでも暗黙的なアニメーションの動作はありません.
暗黙的なアニメーションの動作をカスタマイズする
view関連layerはactionを設定することで暗黙的なアニメーションを実現できます
明示的なアニメーション
明示的な呼び出し
カラー変換フレームアニメーション:
パスフレームアニメーション:
回転アニメーション:
アニメーショングループ
バッファ-アニメーション速度変化関数
アニメーション速度 Layer計時関数: ここではCAMediaTimingFunctionを作成する方法がいくつかありますが、最も簡単な方法は+timingFunctionWithName:を呼び出す構造方法です.ここでは、次の定数の1つを入力します. UIView計時関数 UImitのアニメーションでも、構文と定数が異なるにもかかわらず、UIViewアニメーションのバッファオプションを変更するには、optionsパラメータに次の定数の1つを追加します.
UIViewアニメーション指定タイマ関数オプション
タイミング関数とキーフレームアニメーション
CAKeyframeAnimationにはNSArrayタイプのtimingFunctionsプロパティがあり、アニメーションのステップごとに異なるタイミング関数を指定できます.ただし、指定した関数の個数は、各フレーム間のアニメーション速度を記述する関数であるため、keyframes配列の要素の個数に等しくなければなりません.
『iosコアアニメーションの高度なテクニック』の本からのいくつかの例のまとめは、
暗黙的なアニメーション
CALayerのアニメーション可能なプロパティを変更すると、すぐに画面に表示されません.逆に、以前の値から新しい値にスムーズに移行します.これらはすべてデフォルトの動作であり、追加の操作は必要ありません.
暗黙的なアニメーションlayerのbackgroundColorを変更することでカラーグラデーションのアニメーションを実現
//randomize the layer background color
CGFloat red = arc4random() / (CGFloat)INT_MAX;
CGFloat green = arc4random() / (CGFloat)INT_MAX;
CGFloat blue = arc4random() / (CGFloat)INT_MAX;
self.colorLayer.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0].CGColor; ```
####
`CATransaction`
-
-
-
```objc
//begin a new transaction
[CATransaction begin];
//set the animation duration to 1 second
[CATransaction setAnimationDuration:1.0];
//randomize the layer background color
CGFloat red = arc4random() / (CGFloat)INT_MAX;
CGFloat green = arc4random() / (CGFloat)INT_MAX;
CGFloat blue = arc4random() / (CGFloat)INT_MAX;
self.colorLayer.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0].CGColor;
//commit the transaction
[CATransaction commit];
トランザクションの完了block
//add the spin animation on completion
[CATransaction setCompletionBlock:^{
//rotate the layer 90 degrees
CGAffineTransform transform = self.colorLayer.affineTransform;
transform = CGAffineTransformRotate(transform, M_PI_2);
self.colorLayer.affineTransform = transform;
}];
view関連layerは暗黙的なアニメーションを無効にしました
デフォルトのviewで関連付けられたViewではlayerの暗黙的なアニメーションは無効になっています.簡単な方法はactionを設定して暗黙的なアニメーションの効果を達成することです.
//begin a new transaction
[CATransaction begin];
//set the animation duration to 1 second
[CATransaction setAnimationDuration:1.0];
//randomize the layer background color
CGFloat red = arc4random() / (CGFloat)INT_MAX;
CGFloat green = arc4random() / (CGFloat)INT_MAX;
CGFloat blue = arc4random() / (CGFloat)INT_MAX;
self.layerView.layer.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0].CGColor;
//commit the transaction
[CATransaction commit];
レイヤカラーは、以前にスムーズに遷移したアニメーションではなく、新しい値に瞬時に切り替わります.
デフォルトの暗黙的なアニメーションの動作を設定できます.暗黙的なアニメーションをオフにすると、viewに関連付けられていないlayerでも暗黙的なアニメーションの動作はありません.
[CATransaction setDisableActions:YES];
暗黙的なアニメーションの動作をカスタマイズする
view関連layerはactionを設定することで暗黙的なアニメーションを実現できます
//add a custom action
CATransition *transition = [CATransition animation];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
self.colorLayer.actions = @{@"backgroundColor": transition};
//add it to our view
[self.layerView.layer addSublayer:self.colorLayer];
明示的なアニメーション
明示的な呼び出し
CALayer
の方法addAnimation
によるアニメーション効果カラー変換フレームアニメーション:
//create a keyframe animation
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
animation.keyPath = @"backgroundColor";
animation.duration = 2.0;
animation.values = @[
(__bridge id)[UIColor blueColor].CGColor,
(__bridge id)[UIColor redColor].CGColor,
(__bridge id)[UIColor greenColor].CGColor,
(__bridge id)[UIColor blueColor].CGColor ];
//apply animation to layer
[self.colorLayer addAnimation:animation forKey:nil];
パスフレームアニメーション:
//create a path
UIBezierPath *bezierPath = [[UIBezierPath alloc] init];
[bezierPath moveToPoint:CGPointMake(0, 150)];
[bezierPath addCurveToPoint:CGPointMake(300, 150) controlPoint1:CGPointMake(75, 0) controlPoint2:CGPointMake(225, 300)];
//add the ship
CALayer *shipLayer = [CALayer layer];
shipLayer.frame = CGRectMake(0, 0, 64, 64);
shipLayer.position = CGPointMake(0, 150);
shipLayer.contents = (__bridge id)[UIImage imageNamed: @"Ship.png"].CGImage;
[self.containerView.layer addSublayer:shipLayer];
//create the keyframe animation
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
animation.keyPath = @"position";
animation.duration = 4.0;
animation.path = bezierPath.CGPath;
[shipLayer addAnimation:animation forKey:nil];
回転アニメーション:
//animate the ship rotation
CABasicAnimation *animation = [CABasicAnimation animation];
animation.keyPath = @"transform.rotation";
animation.duration = 2.0;
animation.byValue = @(M_PI * 2);
[shipLayer addAnimation:animation forKey:nil];
アニメーショングループ
CAAnimationGroup
は、これらのアニメーションを組み合わせることができます. //create a path
UIBezierPath *bezierPath = [[UIBezierPath alloc] init];
[bezierPath moveToPoint:CGPointMake(0, 150)];
[bezierPath addCurveToPoint:CGPointMake(300, 150) controlPoint1:CGPointMake(75, 0) controlPoint2:CGPointMake(225, 300)];
//draw the path using a CAShapeLayer
CAShapeLayer *pathLayer = [CAShapeLayer layer];
pathLayer.path = bezierPath.CGPath;
pathLayer.fillColor = [UIColor clearColor].CGColor;
pathLayer.strokeColor = [UIColor redColor].CGColor;
pathLayer.lineWidth = 3.0f;
[self.containerView.layer addSublayer:pathLayer];
//add a colored layer
CALayer *colorLayer = [CALayer layer];
colorLayer.frame = CGRectMake(0, 0, 64, 64);
colorLayer.position = CGPointMake(0, 150);
colorLayer.backgroundColor = [UIColor greenColor].CGColor;
[self.containerView.layer addSublayer:colorLayer];
//create the position animation
CAKeyframeAnimation *animation1 = [CAKeyframeAnimation animation];
animation1.keyPath = @"position";
animation1.path = bezierPath.CGPath;
animation1.rotationMode = kCAAnimationRotateAuto;
//create the color animation
CABasicAnimation *animation2 = [CABasicAnimation animation];
animation2.keyPath = @"backgroundColor";
animation2.toValue = (__bridge id)[UIColor redColor].CGColor;
//create group animation
CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];
groupAnimation.animations = @[animation1, animation2];
groupAnimation.duration = 4.0;
//add the animation to the color layer
[colorLayer addAnimation:groupAnimation forKey:nil];
バッファ-アニメーション速度変化関数
アニメーション速度
kCAMediaTimingFunctionLinear //
kCAMediaTimingFunctionEaseIn //
kCAMediaTimingFunctionEaseOut //
kCAMediaTimingFunctionEaseInEaseOut //
kCAMediaTimingFunctionDefault // , kCAMediaTimingFunctionEaseInEaseOut
//configure the transaction
[CATransaction begin];
[CATransaction setAnimationDuration:1.0];
[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
//set the position
self.colorLayer.position = [[touches anyObject] locationInView:self.view];
//commit transaction
[CATransaction commit];
UIViewAnimationOptionCurveEaseInOut
UIViewAnimationOptionCurveEaseIn
UIViewAnimationOptionCurveEaseOut
UIViewAnimationOptionCurveLinear
UIViewアニメーション指定タイマ関数オプション
//perform the animation
[UIView animateWithDuration:1.0 delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
//set the position
self.colorView.center = [[touches anyObject] locationInView:self.view];
}
completion:NULL];
タイミング関数とキーフレームアニメーション
CAKeyframeAnimationにはNSArrayタイプのtimingFunctionsプロパティがあり、アニメーションのステップごとに異なるタイミング関数を指定できます.ただし、指定した関数の個数は、各フレーム間のアニメーション速度を記述する関数であるため、keyframes配列の要素の個数に等しくなければなりません.
//create a keyframe animation
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
animation.keyPath = @"backgroundColor";
animation.duration = 2.0;
animation.values = @[
(__bridge id)[UIColor blueColor].CGColor,
(__bridge id)[UIColor redColor].CGColor,
(__bridge id)[UIColor greenColor].CGColor,
(__bridge id)[UIColor blueColor].CGColor ];
//add timing function
CAMediaTimingFunction *fn = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseIn];
animation.timingFunctions = @[fn, fn, fn];
//apply animation to layer
[self.colorLayer addAnimation:animation forKey:nil];