Skip to content

Commit

Permalink
Added a sample for animating custom layer properties
Browse files Browse the repository at this point in the history
  • Loading branch information
neror committed Nov 11, 2010
1 parent f157f72 commit 9fff27e
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CA360.xcodeproj/project.pbxproj
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -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 = "<group>"; };
DFC6219F117399D700F5B5FD /* BitmapFontCounter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; name = BitmapFontCounter.m; path = "Classes/Fun Stuff/BitmapFontCounter.m"; sourceTree = "<group>"; };
DFC621E611745A1F00F5B5FD /* NumberStrip.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = NumberStrip.png; sourceTree = "<group>"; };
DFEE3F45128C94CA0010106E /* CustomPropertyAnimation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CustomPropertyAnimation.h; path = Classes/Samples/CustomPropertyAnimation.h; sourceTree = "<group>"; };
DFEE3F46128C94CA0010106E /* CustomPropertyAnimation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CustomPropertyAnimation.m; path = Classes/Samples/CustomPropertyAnimation.m; 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; lineEnding = 0; name = ImageContent.m; path = Classes/Samples/ImageContent.m; sourceTree = "<group>"; };
DFF70C451039D9E60052EE6A /* FTSLogo.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = FTSLogo.png; sourceTree = "<group>"; };
Expand Down Expand Up @@ -251,6 +254,8 @@
DFF70D9D103A104C0052EE6A /* LayerActions.m */,
DFF70DA0103A10870052EE6A /* AnimationTransactions.h */,
DFF70DA1103A10870052EE6A /* AnimationTransactions.m */,
DFEE3F45128C94CA0010106E /* CustomPropertyAnimation.h */,
DFEE3F46128C94CA0010106E /* CustomPropertyAnimation.m */,
);
name = Animation;
sourceTree = "<group>";
Expand Down Expand Up @@ -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;
};
Expand Down
33 changes: 33 additions & 0 deletions 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 <UIKit/UIKit.h>

@class CustomLayer;

@interface CustomPropertyAnimation : UIViewController {
CustomLayer *customLayer_;
}

@end
120 changes: 120 additions & 0 deletions 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 <QuartzCore/QuartzCore.h>

@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
3 changes: 2 additions & 1 deletion Classes/Samples/SampleManager.m
Expand Up @@ -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)

Expand All @@ -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];

Expand Down

0 comments on commit 9fff27e

Please sign in to comment.