Permalink
Browse files

Added the keyframe animation example with Mario jumping action

  • Loading branch information...
1 parent 9f4004d commit 120e001a3110896c4984972293dee60b96b17ee5 @neror committed Sep 15, 2009
@@ -18,6 +18,7 @@
DF8015631038E1C400B48250 /* LayerTransforms.m in Sources */ = {isa = PBXBuildFile; fileRef = DF8015611038E1C400B48250 /* LayerTransforms.m */; };
DFAFBACF105EFF4200AE6FF1 /* LayerTransitions.m in Sources */ = {isa = PBXBuildFile; fileRef = DFAFBACE105EFF4200AE6FF1 /* LayerTransitions.m */; };
DFAFBB8D105F0DD800AE6FF1 /* FTUtils+NSObject.m in Sources */ = {isa = PBXBuildFile; fileRef = DFAFBB8C105F0DD800AE6FF1 /* FTUtils+NSObject.m */; };
+ DFB121621060279A00E7DB00 /* Mario.png in Resources */ = {isa = PBXBuildFile; fileRef = DFB121611060279A00E7DB00 /* Mario.png */; };
DFF70C321039D8BC0052EE6A /* ImageContent.m in Sources */ = {isa = PBXBuildFile; fileRef = DFF70C311039D8BC0052EE6A /* ImageContent.m */; };
DFF70C461039D9E60052EE6A /* FTSLogo.png in Resources */ = {isa = PBXBuildFile; fileRef = DFF70C451039D9E60052EE6A /* FTSLogo.png */; };
DFF70C4F1039DA0A0052EE6A /* MainWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = DFF70C4C1039DA0A0052EE6A /* MainWindow.xib */; };
@@ -60,6 +61,7 @@
DFAFBB8A105F0DD700AE6FF1 /* FTUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FTUtils.h; sourceTree = "<group>"; };
DFAFBB8B105F0DD800AE6FF1 /* FTUtils+NSObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "FTUtils+NSObject.h"; sourceTree = "<group>"; };
DFAFBB8C105F0DD800AE6FF1 /* FTUtils+NSObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "FTUtils+NSObject.m"; sourceTree = "<group>"; };
+ DFB121611060279A00E7DB00 /* Mario.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Mario.png; sourceTree = "<group>"; };
DFF70C301039D8BC0052EE6A /* ImageContent.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ImageContent.h; path = Classes/Samples/ImageContent.h; sourceTree = "<group>"; };
DFF70C311039D8BC0052EE6A /* ImageContent.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ImageContent.m; path = Classes/Samples/ImageContent.m; sourceTree = "<group>"; };
DFF70C451039D9E60052EE6A /* FTSLogo.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = FTSLogo.png; sourceTree = "<group>"; };
@@ -166,6 +168,7 @@
DFF70C441039D9E60052EE6A /* Resources */ = {
isa = PBXGroup;
children = (
+ DFB121611060279A00E7DB00 /* Mario.png */,
DFF70C451039D9E60052EE6A /* FTSLogo.png */,
DFF70C9C1039DE170052EE6A /* Ben.png */,
DFF70C9D1039DE170052EE6A /* Steve.png */,
@@ -320,6 +323,7 @@
DFF70C9E1039DE170052EE6A /* Ben.png in Resources */,
DFF70C9F1039DE170052EE6A /* Steve.png in Resources */,
DFF70D78103A0BB40052EE6A /* StarMask.png in Resources */,
+ DFB121621060279A00E7DB00 /* Mario.png in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -26,7 +26,10 @@
@interface KeyframeAnimation : UIViewController {
-
+ CALayer *platformLayer_;
+ CALayer *marioLayer_;
+
+ UIButton *jumpButton_;
}
@end
@@ -23,6 +23,10 @@ of this software and associated documentation files (the "Software"), to deal
*/
#import "KeyframeAnimation.h"
+#import <QuartzCore/QuartzCore.h>
+
+static const CGRect kMarioStandingSpriteCoords = { .5f, 0.f, .5f, 1.f };
+static const CGRect kMarioJumpingSpriteCoords = { 0.f, 0.f, .5f, 1.f };
@implementation KeyframeAnimation
@@ -40,6 +44,9 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
}
- (void)dealloc {
+ FTRELEASE(platformLayer_);
+ FTRELEASE(marioLayer_);
+ FTRELEASE(jumpButton_);
[super dealloc];
}
@@ -48,18 +55,94 @@ - (void)dealloc {
- (void)loadView {
UIView *myView = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
myView.backgroundColor = [UIColor grayColor];
+
+ jumpButton_ = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
+ jumpButton_.frame = CGRectMake(10.f, 10.f, 300.f, 44.f);
+ [jumpButton_ setTitle:@"Jump!" forState:UIControlStateNormal];
+ [jumpButton_ addTarget:self action:@selector(jump:) forControlEvents:UIControlEventTouchUpInside];
+ [myView addSubview:jumpButton_];
+
+ platformLayer_ = [[CALayer layer] retain];
+ marioLayer_ = [[CALayer layer] retain];
+
+ [myView.layer addSublayer:platformLayer_];
+ [myView.layer addSublayer:marioLayer_];
self.view = myView;
}
- (void)viewDidUnload {
+ FTRELEASE(platformLayer_);
+ FTRELEASE(marioLayer_);
+ FTRELEASE(jumpButton_);
}
#pragma mark View drawing
- (void)viewWillAppear:(BOOL)animated {
+ CGSize viewSize = self.view.bounds.size;
+
+ marioLayer_.backgroundColor = [[UIColor clearColor] CGColor];
+ marioLayer_.anchorPoint = CGPointMake(0.f, 1.f);
+ marioLayer_.bounds = CGRectMake(0.f, 0.f, 32.f, 64.f);
+ marioLayer_.position = CGPointMake(0.f, viewSize.height);
+ marioLayer_.contents = (id)[[UIImage imageNamed:@"Mario.png"] CGImage];
+ marioLayer_.contentsGravity = kCAGravityResizeAspect;
+ marioLayer_.contentsRect = kMarioStandingSpriteCoords;
+
+ platformLayer_.backgroundColor = [[UIColor brownColor] CGColor];
+ platformLayer_.anchorPoint = CGPointZero;
+ platformLayer_.frame = CGRectMake(160.f, 200.f, 160.f, 20.f);
+ platformLayer_.cornerRadius = 4.f;
+ [platformLayer_ setNeedsDisplay];
+ [platformLayer_ setNeedsDisplay];
}
- (void)viewDidAppear:(BOOL)animated {
}
+#pragma mark Event Handlers
+
+- (void)jump:(id)sender {
+ CGSize viewSize = self.view.bounds.size;
+ [marioLayer_ removeAnimationForKey:@"marioJump"];
+
+ CGMutablePathRef jumpPath = CGPathCreateMutable();
+ CGPathMoveToPoint(jumpPath, NULL, 0.f, viewSize.height);
+ CGPathAddCurveToPoint(jumpPath, NULL, 30.f, 140.f, 170.f, 140.f, 170.f, 200.f);
+
+ CAKeyframeAnimation *jumpAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
+ jumpAnimation.path = jumpPath;
+ jumpAnimation.duration = 1.f;
+ jumpAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
+ jumpAnimation.delegate = self;
+
+ CGPathRelease(jumpPath);
+
+ [marioLayer_ addAnimation:jumpAnimation forKey:@"marioJump"];
+
+}
+
+#pragma mark Animation delegate methods
+
+- (void)animationDidStart:(CAAnimation *)theAnimation {
+ [CATransaction begin];
+ {
+ [CATransaction setDisableActions:YES];
+ marioLayer_.contentsRect = kMarioJumpingSpriteCoords;
+ }
+ [CATransaction commit];
+}
+
+- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)finished {
+ [CATransaction begin];
+ {
+ [CATransaction setDisableActions:YES];
+ marioLayer_.contentsRect = kMarioStandingSpriteCoords;
+ if(finished) {
+ marioLayer_.position = CGPointMake(170.f, 200.f);
+ }
+ }
+ [CATransaction commit];
+}
+
@end
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 120e001

Please sign in to comment.