Skip to content

Instantly share code, notes, and snippets.

@edom18
Created March 5, 2014 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edom18/9369263 to your computer and use it in GitHub Desktop.
Save edom18/9369263 to your computer and use it in GitHub Desktop.
AppleのCore Animationプログラミングガイドを読んだメモ ref: http://qiita.com/edo_m18/items/4309d01b67ee42c35b3c
// `CAEAGLLayer`を返す例
+ (Class) layerClass
{
return [CAEAGLLayer class];
}
- (void)displayLayer:(CALayer *)theLayer
{
// 状態プロパティの値を検査
if (self.displayYesImage) {
// 「Yes」画像を表示
theLayer.contents = [someHelperObject loadStateYesImage];
}
else {
// 「No」画像を表示
theLayer.contents = [someHelperObject loadStateNoImage];
}
}
- (void)resumeLayer:(CALayer *)layer
{
CFTimeInterval pausedTime = [layer timeOffset];
layer.speed = 1.0;
layer.timeOffset = 0.0;
layer.beginTime = 0.0;
CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
layer.beginTime = timeSincePause;
}
[CATransaction begin];
theLayer.zPosition = 200.0;
theLayer.opacity = 0.0;
[CATransaction commit];
[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:10.0f]
forKey:kCATransactionAnimationDuration];
CATransform3D perspective = CATransform3DIdentity;
perspective.m34 = -1.0 / eyePosition;
// 変換を親レイヤに適用
myParentLayer.sublayerTransform = perspective;
- (id<CAAction>)actionForLayer:(CALayer *)theLayer
forKey:(NSString *)theKey
{
CATransition *theAnimation = nil;
if ([theKey isEqualToString:@"contents"]) {
theAnimation = [[CATransition alloc] init];
theAnimation.duration = 1.0;
theAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
theAnimation.type = kCATransitionPush;
theAnimation.subtype = kCATransitionFromRight;
}
return theAnimation;
}
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue
forKey:kCATransactionDisableActions];
[aLayer removeFromSuperlayer];
[CATransaction commit];
- (void)drawLayer:(CALayer *)theLayer inContext:(CGContextRef)theContext
{
CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath, NULL, 15.0f, 15.0f);
CGPathAddCurveToPoint(thePath, NULL,
15.0f, 250.0f,
295.0f, 250.0f,
295.0f, 15.0f);
CGContextBeginPath(theContext);
CGContextAddPath(theContext, thePath);
CGContextSetLineWidth(theContext, 5);
CGContextStrokePath(theContext);
// パスを解放
CFRelease(thePath);
}
CABasicAnimation *fadeAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeAnim.fromValue = [NSNumber numberWithFloat:1.0];
fadeAnim.toValue = [NSNumber numberWithFloat:0.0];
fadeAnim.duration = 1.0;
[theLayer addAnimation:fadeAnim forKey:@"opacity"];
// レイヤの実プロパティを最終値に変更
theLayer.opacity = 0.0;
// 2つの弧(跳ね返りの軌跡)を表すパス(CGPath)を生成
CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath, NULL, 74.0, 74.0);
CGPathAddCurveToPoint(thePath, NULL,
74.0, 500.0,
320.0, 500.0,
320.0, 74.0);
CGPathAddCurveToPoint(thePath, NULL,
320.0, 500.0,
566.0, 500.0,
566.0, 74.0);
CAKeyframeAnimation *theAnimation;
// positionプロパティをキーパスとして指定し、アニメーションオブジェクトを生成
theAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
theAnimation.path = thePath;
theAnimation.duration = 5.0;
// アニメーションをレイヤに追加
[theLayer addAnimation:theAnimation forKey:@"position"];
// アニメーション1
CAKeyframeAnimation *widthAnim = [CAKeyframeAnimation animationWithKeyPath:@"borderWidth"];
NSArray *widthValues = [NSArray arrayWithObjects:@1.0, @10.0, @5.0, @30.0, @0.5, @15.0, @2.0, @50.0, @0.0, nil];
widthAnim.values = widthValues;
widthAnim.calculationMode = kCAAnimationPaced;
// アニメーション2
CAKeyframeAnimation *colorAnim = [CAKeyframeAnimation animationWithKeyPath:@"borderColor"];
NSArray* colorValues = [NSArray arrayWithObjects:(id)[UIColor greenColor].CGColor, (id)[UIColor redColor].CGColor, (id)[UIColor blueColor].CGColor, nil];
colorAnim.values = colorValues;
colorAnim.calculationMode = kCAAnimationPaced;
// アニメーショングループ
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = [NSArray arrayWithObjects:colorAnim, widthAnim, nil];
group.duration = 5.0;
[myLayer addAnimation:group forKey:@"BorderChanges"];
[UIView animateWithDuration:1.0 animations:^{
// 不透明度を暗黙に変更
myView.layer.opacity = 0.0;
// 位置を明示的に変更
CABasicAnimation *theAnim = [CABasicAnimation animationWithKeyPath:@"position"];
theAnim.fromValue = [NSValue valueWithCGPoint:myView.layer.position];
theAnim.toValue = [NSValue valueWithCGPoint:myNewPosition];
theAnim.duration = 3.0;
[myView.layer addAnimation:theAnim forKey:@"AnimateFrame"];
}];
CATransition *transition = [CATransition animation];
transition.startProgress = 0;
transition.endProgress = 1.0;
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
transition.duration = 1.0;
// 2つのレイヤに遷移アニメーションを追加
[myView1.layer addAnimation:transition forKey:@"transition"];
[myView2.layer addAnimation:transition forKey:@"transition"];
// 最後に各レイヤの表示/非表示を更新
myView1.hidden = YES;
myView2.hidden = NO;
CFTimeInterval localLayerTime = [myLayer convertTime:CACurrentMediaTime() fromLayer:nil];
- (void)pauseLayer:(CALayer *)layer
{
CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
layer.speed = 0.0;
layer.timeOffset = pausedTime;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment