Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Introduction to Core Animation

  • Loading branch information...
commit 9c165aac63c140251699b48e77700811bb9158aa 1 parent ad01aa1
Ryan Blunden authored
View
5 HypnoTime/HypnoTime.xcodeproj/project.pbxproj
@@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
+ A843C4B716A8808C00CCE106 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A843C4B616A8808C00CCE106 /* QuartzCore.framework */; };
BE3B4F38140D421800BD6933 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BE3B4F37140D421800BD6933 /* UIKit.framework */; };
BE3B4F3A140D421800BD6933 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BE3B4F39140D421800BD6933 /* Foundation.framework */; };
BE3B4F40140D421800BD6933 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = BE3B4F3E140D421800BD6933 /* InfoPlist.strings */; };
@@ -24,6 +25,7 @@
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
+ A843C4B616A8808C00CCE106 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
BE3B4F33140D421800BD6933 /* HypnoTime.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = HypnoTime.app; sourceTree = BUILT_PRODUCTS_DIR; };
BE3B4F37140D421800BD6933 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
BE3B4F39140D421800BD6933 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
@@ -52,6 +54,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
+ A843C4B716A8808C00CCE106 /* QuartzCore.framework in Frameworks */,
BE3B4F4D140D42A000BD6933 /* CoreGraphics.framework in Frameworks */,
BE3B4F38140D421800BD6933 /* UIKit.framework in Frameworks */,
BE3B4F3A140D421800BD6933 /* Foundation.framework in Frameworks */,
@@ -64,6 +67,7 @@
BE3B4F28140D421800BD6933 = {
isa = PBXGroup;
children = (
+ A843C4B616A8808C00CCE106 /* QuartzCore.framework */,
BE3B4F59140D557500BD6933 /* Hypno.png */,
BE3B4F5A140D557500BD6933 /* Hypno@2x.png */,
BE3B4F5B140D557500BD6933 /* Time.png */,
@@ -294,6 +298,7 @@
BE3B4F4B140D421800BD6933 /* Release */,
);
defaultConfigurationIsVisible = 0;
+ defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
View
BIN  HypnoTime/HypnoTime/Default-568h@2x.png
Deleted file not rendered
View
BIN  HypnoTime/HypnoTime/Default.png
Deleted file not rendered
View
BIN  HypnoTime/HypnoTime/Default@2x.png
Deleted file not rendered
View
BIN  HypnoTime/HypnoTime/Hypno.png
Deleted file not rendered
View
BIN  HypnoTime/HypnoTime/Hypno@2x.png
Deleted file not rendered
View
17 HypnoTime/HypnoTime/HypnoTimeAppDelegate.h
@@ -1,17 +0,0 @@
-//
-// HypnoTimeAppDelegate.h
-// HypnoTime
-//
-// Created by Ryan Blunden on 01/14/13.
-// Copyright (c) 2013 __MyCompanyName__. All rights reserved.
-//
-
-#import <UIKit/UIKit.h>
-#import "HypnosisViewController.h"
-#import "TimeViewController.h"
-
-@interface HypnoTimeAppDelegate : UIResponder <UIApplicationDelegate>
-
-@property (strong, nonatomic) UIWindow *window;
-
-@end
View
64 HypnoTime/HypnoTime/HypnoTimeAppDelegate.m
@@ -1,64 +0,0 @@
-//
-// HypnoTimeAppDelegate.m
-// HypnoTime
-//
-// Created by Ryan Blunden on 01/14/13.
-// Copyright (c) 2013 __MyCompanyName__. All rights reserved.
-//
-
-#import "HypnoTimeAppDelegate.h"
-#import "TimeViewController.h"
-
-@implementation HypnoTimeAppDelegate
-
-- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- // Override point for customization after application launch.
-
- HypnosisViewController *hvc = [[HypnosisViewController alloc] init];
-
- self.window.backgroundColor = [UIColor whiteColor];
-
- // This will get a pointer to an object that represents the app bundle
- NSBundle *appBundle = [NSBundle mainBundle];
-
- // Tell the TimeVC to look in the appBundle for the file TimeViewController.xib
- TimeViewController *tvc = [[TimeViewController alloc] init];
-
- UITabBarController *tabBarController = [[UITabBarController alloc] init];
- [tabBarController setViewControllers:@[hvc, tvc]];
-
- [[self window] setRootViewController:tabBarController];
-
- [self.window makeKeyAndVisible];
- return YES;
-}
-
-- (void)applicationWillResignActive:(UIApplication *)application {
- // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
- // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
-
-}
-
-- (void)applicationDidEnterBackground:(UIApplication *)application {
- // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
- // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
-
-}
-
-- (void)applicationWillEnterForeground:(UIApplication *)application {
- // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
-
-}
-
-- (void)applicationDidBecomeActive:(UIApplication *)application {
- // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
-
-}
-
-- (void)applicationWillTerminate:(UIApplication *)application {
- // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
-
-}
-
-@end
View
208 HypnoTime/HypnoTime/HypnosisView.m
@@ -6,94 +6,148 @@
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
+#import <QuartzCore/QuartzCore.h>
#import "HypnosisView.h"
+#import "QuartzCore/QuartzCore.h"
+
+@interface HypnosisView () {
+ CALayer *_boxLayer;
+}
+
+@end
@implementation HypnosisView
@synthesize circleColor;
-- (id)initWithFrame:(CGRect)frame
-{
- self = [super initWithFrame:frame];
- if(self) {
- [self setBackgroundColor:[UIColor clearColor]];
- [self setCircleColor:[UIColor lightGrayColor]];
- }
- return self;
+- (id)initWithFrame:(CGRect)frame {
+ self = [super initWithFrame:frame];
+ if (self) {
+ [self setBackgroundColor:[UIColor clearColor]];
+ [self setCircleColor:[UIColor lightGrayColor]];
+
+ // Create the new layer object
+ _boxLayer = [[CALayer alloc] init];
+
+ // Size
+ [_boxLayer setBounds:CGRectMake(0.0, 0.0, 85.0, 85.0)];
+
+ // Location
+ [_boxLayer setPosition:CGPointMake(160.0, 100.0)];
+
+ // Make half-transparent red the background color
+ UIColor *reddish = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.5];
+
+ // Get a CGClor object with the same color values
+ CGColorRef cgReddish = [reddish CGColor];
+ [_boxLayer setBackgroundColor:cgReddish];
+
+ // Create a UIImage
+ UIImage *layerImage = [UIImage imageNamed:@"Hypno.png"];
+
+ // Get the underlying CGImage
+ CGImageRef image = [layerImage CGImage];
+
+ // Put the CGImage on the layer
+ [_boxLayer setContents:(__bridge id)image];
+
+ // Inser the image a bit on each side
+ [_boxLayer setContentsRect:CGRectMake(-0.1, -0.1, 1.2, 1.2)];
+
+ // Let the image resize (without changing the aspect ratio) to fill the contentRect
+ [_boxLayer setContentsGravity:kCAGravityResizeAspect];
+
+
+ // Make it a sublayer of the view's layer
+ [[self layer] addSublayer:_boxLayer];
+ }
+ return self;
}
-- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
-{
- if(motion == UIEventSubtypeMotionShake) {
- NSLog(@"Device started shaking!");
- [self setCircleColor:[UIColor redColor]];
- }
+- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
+ if (motion == UIEventSubtypeMotionShake) {
+ NSLog(@"Device started shaking!");
+ [self setCircleColor:[UIColor redColor]];
+ }
}
-- (void)setCircleColor:(UIColor *)clr
-{
- circleColor = clr;
- [self setNeedsDisplay];
+- (void)setCircleColor:(UIColor *)clr {
+ circleColor = clr;
+ [self setNeedsDisplay];
}
-- (BOOL)canBecomeFirstResponder
-{
- return YES;
+- (BOOL)canBecomeFirstResponder {
+ return YES;
}
-- (void)drawRect:(CGRect)dirtyRect
-{
- CGContextRef ctx = UIGraphicsGetCurrentContext();
- CGRect bounds = [self bounds];
-
- // Figure out the center of the bounds rectangle
- CGPoint center;
- center.x = bounds.origin.x + bounds.size.width / 2.0;
- center.y = bounds.origin.y + bounds.size.height / 2.0;
-
- // The radius of the circle should be nearly as big as the view
- float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0;
-
- // The thickness of the line should be 10 points wide
- CGContextSetLineWidth(ctx, 10);
-
- [[self circleColor] setStroke];
-
- // Draw concentric circles from the outside in
- for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20) {
- CGContextAddArc(ctx, center.x, center.y, currentRadius, 0.0, M_PI * 2.0, YES);
-
- CGContextStrokePath(ctx);
- }
-
- // Create a string
- NSString *text = @"You are getting sleepy.";
-
- // Get a font to draw it in
- UIFont *font = [UIFont boldSystemFontOfSize:28];
-
- CGRect textRect;
-
- // How big is this string when drawn in this font?
- textRect.size = [text sizeWithFont:font];
-
- // Let's put that string in the center of the view
- textRect.origin.x = center.x - textRect.size.width / 2.0;
- textRect.origin.y = center.y - textRect.size.height / 2.0;
-
- // Set the fill color of the current context to black
- [[UIColor blackColor] setFill];
-
- // The shadow will move 4 points to the right and 3 points down from the text
- CGSize offset = CGSizeMake(4, 3);
-
- // The shadow will be dark gray in color
- CGColorRef color = [[UIColor darkGrayColor] CGColor];
-
- // Set the shadow of the context with these parameters,
- // all subsequent drawing will be shadowed
- CGContextSetShadowWithColor(ctx, offset, 2.0, color);
-
- // Draw the string
- [text drawInRect:textRect
- withFont:font];
+
+- (void)drawRect:(CGRect)dirtyRect {
+ CGContextRef ctx = UIGraphicsGetCurrentContext();
+ CGRect bounds = [self bounds];
+
+ // Figure out the center of the bounds rectangle
+ CGPoint center;
+ center.x = bounds.origin.x + bounds.size.width / 2.0;
+ center.y = bounds.origin.y + bounds.size.height / 2.0;
+
+ // The radius of the circle should be nearly as big as the view
+ float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0;
+
+ // The thickness of the line should be 10 points wide
+ CGContextSetLineWidth(ctx, 10);
+
+ [[self circleColor] setStroke];
+
+ // Draw concentric circles from the outside in
+ for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20) {
+ CGContextAddArc(ctx, center.x, center.y, currentRadius, 0.0, M_PI * 2.0, YES);
+
+ CGContextStrokePath(ctx);
+ }
+
+ // Create a string
+ NSString *text = @"You are getting sleepy.";
+
+ // Get a font to draw it in
+ UIFont *font = [UIFont boldSystemFontOfSize:28];
+
+ CGRect textRect;
+
+ // How big is this string when drawn in this font?
+ textRect.size = [text sizeWithFont:font];
+
+ // Let's put that string in the center of the view
+ textRect.origin.x = center.x - textRect.size.width / 2.0;
+ textRect.origin.y = center.y - textRect.size.height / 2.0;
+
+ // Set the fill color of the current context to black
+ [[UIColor blackColor] setFill];
+
+ // The shadow will move 4 points to the right and 3 points down from the text
+ CGSize offset = CGSizeMake(4, 3);
+
+ // The shadow will be dark gray in color
+ CGColorRef color = [[UIColor darkGrayColor] CGColor];
+
+ // Set the shadow of the context with these parameters,
+ // all subsequent drawing will be shadowed
+ CGContextSetShadowWithColor(ctx, offset, 2.0, color);
+
+ // Draw the string
+ [text drawInRect:textRect
+ withFont:font];
+}
+
+- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
+ UITouch *t = [touches anyObject];
+ CGPoint p = [t locationInView:self];
+ [_boxLayer setPosition:p];
+}
+
+- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
+ UITouch *t = [touches anyObject];
+ CGPoint p = [t locationInView:self];
+ [CATransaction begin];
+ [CATransaction setDisableActions:YES];
+ [_boxLayer setPosition:p];
+ [CATransaction commit];
}
@end
View
BIN  HypnoTime/HypnoTime/Time.png
Deleted file not rendered
View
BIN  HypnoTime/HypnoTime/Time@2x.png
Deleted file not rendered
Please sign in to comment.
Something went wrong with that request. Please try again.