From 9fff27ef4731791c2054fa45a4f9cf683a32e0fc Mon Sep 17 00:00:00 2001 From: Nathan Eror Date: Thu, 11 Nov 2010 15:55:00 -0600 Subject: [PATCH] Added a sample for animating custom layer properties --- CA360.xcodeproj/project.pbxproj | 6 ++ Classes/Samples/CustomPropertyAnimation.h | 33 ++++++ Classes/Samples/CustomPropertyAnimation.m | 120 ++++++++++++++++++++++ Classes/Samples/SampleManager.m | 3 +- 4 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 Classes/Samples/CustomPropertyAnimation.h create mode 100644 Classes/Samples/CustomPropertyAnimation.m diff --git a/CA360.xcodeproj/project.pbxproj b/CA360.xcodeproj/project.pbxproj index af9f0aa..3a021e7 100755 --- a/CA360.xcodeproj/project.pbxproj +++ b/CA360.xcodeproj/project.pbxproj @@ -26,6 +26,7 @@ DFB121621060279A00E7DB00 /* Mario.png in Resources */ = {isa = PBXBuildFile; fileRef = DFB121611060279A00E7DB00 /* Mario.png */; }; DFC621A0117399D700F5B5FD /* BitmapFontCounter.m in Sources */ = {isa = PBXBuildFile; fileRef = DFC6219F117399D700F5B5FD /* BitmapFontCounter.m */; }; DFC621E711745A1F00F5B5FD /* NumberStrip.png in Resources */ = {isa = PBXBuildFile; fileRef = DFC621E611745A1F00F5B5FD /* NumberStrip.png */; }; + DFEE3F47128C94CA0010106E /* CustomPropertyAnimation.m in Sources */ = {isa = PBXBuildFile; fileRef = DFEE3F46128C94CA0010106E /* CustomPropertyAnimation.m */; }; 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 */; }; @@ -79,6 +80,8 @@ DFC6219E117399D700F5B5FD /* BitmapFontCounter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = BitmapFontCounter.h; path = "Classes/Fun Stuff/BitmapFontCounter.h"; sourceTree = ""; }; DFC6219F117399D700F5B5FD /* BitmapFontCounter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; name = BitmapFontCounter.m; path = "Classes/Fun Stuff/BitmapFontCounter.m"; sourceTree = ""; }; DFC621E611745A1F00F5B5FD /* NumberStrip.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = NumberStrip.png; sourceTree = ""; }; + DFEE3F45128C94CA0010106E /* CustomPropertyAnimation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CustomPropertyAnimation.h; path = Classes/Samples/CustomPropertyAnimation.h; sourceTree = ""; }; + DFEE3F46128C94CA0010106E /* CustomPropertyAnimation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CustomPropertyAnimation.m; path = Classes/Samples/CustomPropertyAnimation.m; sourceTree = ""; }; DFF70C301039D8BC0052EE6A /* ImageContent.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ImageContent.h; path = Classes/Samples/ImageContent.h; sourceTree = ""; }; DFF70C311039D8BC0052EE6A /* ImageContent.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; name = ImageContent.m; path = Classes/Samples/ImageContent.m; sourceTree = ""; }; DFF70C451039D9E60052EE6A /* FTSLogo.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = FTSLogo.png; sourceTree = ""; }; @@ -251,6 +254,8 @@ DFF70D9D103A104C0052EE6A /* LayerActions.m */, DFF70DA0103A10870052EE6A /* AnimationTransactions.h */, DFF70DA1103A10870052EE6A /* AnimationTransactions.m */, + DFEE3F45128C94CA0010106E /* CustomPropertyAnimation.h */, + DFEE3F46128C94CA0010106E /* CustomPropertyAnimation.m */, ); name = Animation; sourceTree = ""; @@ -395,6 +400,7 @@ DF6D1C3A11C19AF200025ED7 /* ShutterTransition.m in Sources */, DF5FC6C011C7FBD900130E6E /* AdvancedShapeLayers.m in Sources */, DF7A2F7D123D689400BDD60A /* TextLayers.m in Sources */, + DFEE3F47128C94CA0010106E /* CustomPropertyAnimation.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Classes/Samples/CustomPropertyAnimation.h b/Classes/Samples/CustomPropertyAnimation.h new file mode 100644 index 0000000..c8b9988 --- /dev/null +++ b/Classes/Samples/CustomPropertyAnimation.h @@ -0,0 +1,33 @@ +/* + The MIT License + + Copyright (c) 2010 Free Time Studios and Nathan Eror + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#import + +@class CustomLayer; + +@interface CustomPropertyAnimation : UIViewController { + CustomLayer *customLayer_; +} + +@end diff --git a/Classes/Samples/CustomPropertyAnimation.m b/Classes/Samples/CustomPropertyAnimation.m new file mode 100644 index 0000000..4aa2b90 --- /dev/null +++ b/Classes/Samples/CustomPropertyAnimation.m @@ -0,0 +1,120 @@ +/* + The MIT License + + Copyright (c) 2010 Free Time Studios and Nathan Eror + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#import "CustomPropertyAnimation.h" +#import + +@interface CustomLayer : CALayer { +} + +@property (assign) CGFloat margin; + +@end + +@implementation CustomLayer + +@synthesize margin; + ++ (BOOL)needsDisplayForKey:(NSString *)key { + if([key isEqualToString:@"margin"]) { + return YES; + } + return [super needsDisplayForKey:key]; +} + +- (void)drawInContext:(CGContextRef)ctx { + CGContextSaveGState(ctx); + CGContextAddRect(ctx, CGRectInset(self.bounds, self.margin, self.margin)); + CGContextSetStrokeColorWithColor(ctx, [[UIColor yellowColor] CGColor]); + CGContextSetLineWidth(ctx, 4.f); + CGContextDrawPath(ctx, kCGPathStroke); + CGContextRestoreGState(ctx); +} + +@end + + +@implementation CustomPropertyAnimation + ++ (NSString *)friendlyName { + return @"Custom Properties"; +} + +#pragma mark init and dealloc + +- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { + if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { + self.title = [[self class] friendlyName]; + } + return self; +} + +- (void)dealloc { + [super dealloc]; +} + +#pragma mark Load and unload the view + +- (void)loadView { + UIView *myView = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; + myView.backgroundColor = [UIColor whiteColor]; + + customLayer_ = [[CustomLayer alloc] init]; + [myView.layer addSublayer:customLayer_]; + + self.view = myView; +} + +#pragma mark View drawing + +- (void)viewWillAppear:(BOOL)animated { + customLayer_.backgroundColor = [[UIColor blueColor] CGColor]; + customLayer_.frame = CGRectInset(self.view.bounds, 50.f, 100.f); + customLayer_.position = self.view.center; + customLayer_.margin = 0.f; +} + +- (void)viewDidAppear:(BOOL)animated { + CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"margin"]; + anim.duration = 1.f; + anim.fromValue = [NSNumber numberWithFloat:5.f]; + anim.toValue = [NSNumber numberWithFloat:25.f]; + anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; + anim.repeatCount = CGFLOAT_MAX; + anim.autoreverses = YES; + + [customLayer_ addAnimation:anim forKey:@"margin"]; + + CABasicAnimation *anim2 = [CABasicAnimation animationWithKeyPath:@"cornerRadius"]; + anim2.duration = 1.f; + anim2.fromValue = [NSNumber numberWithFloat:0.f]; + anim2.toValue = [NSNumber numberWithFloat:20.f]; + anim2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; + anim2.repeatCount = CGFLOAT_MAX; + anim2.autoreverses = YES; + + [customLayer_ addAnimation:anim2 forKey:@"cornerRadius"]; +} + +@end diff --git a/Classes/Samples/SampleManager.m b/Classes/Samples/SampleManager.m index 6090201..efdcee5 100644 --- a/Classes/Samples/SampleManager.m +++ b/Classes/Samples/SampleManager.m @@ -42,6 +42,7 @@ of this software and associated documentation files (the "Software"), to deal #import "ShutterTransition.h" #import "AdvancedShapeLayers.h" #import "TextLayers.h" +#import "CustomPropertyAnimation.h" @interface UIViewController (ThisIsHereToAviodACompilerWarning) @@ -59,7 +60,7 @@ - (id)init { NSArray *drawing = [NSArray arrayWithObjects:[ImageContent class], [LayerDrawing class], [StyleProperties class], nil]; NSArray *animation = [NSArray arrayWithObjects:[BasicAnimation class], [AnimationGroups class], [AnimationTransactions class], [KeyframeAnimation class], [LayerActions class], - [LayerTransitions class], nil]; + [LayerTransitions class], [CustomPropertyAnimation class], nil]; NSArray *special = [NSArray arrayWithObjects:[ShapeLayers class], [AdvancedShapeLayers class], [GradientLayers class], [TextLayers class], nil]; NSArray *advanced = [NSArray arrayWithObjects:[BitmapFontCounter class], [ShutterTransition class], nil];