Created
November 28, 2012 14:33
-
-
Save jem-computer/4161661 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// JGHypnosisView.m | |
// Hypnosister | |
// | |
// Created by Jon Gold on 25/11/2012. | |
// Copyright (c) 2012 Jon Gold. All rights reserved. | |
// | |
#import "JGHypnosisView.h" | |
#import <CoreMotion/CoreMotion.h> | |
@implementation JGHypnosisView | |
CGFloat RadiansToDegrees(CGFloat radians) | |
{ | |
return radians * 180 / M_PI; | |
}; | |
- (id)initWithFrame:(CGRect)frame | |
{ | |
self = [super initWithFrame:frame]; | |
if (self) { | |
[self setBackgroundColor:[UIColor clearColor]]; | |
[self setCircleColor:[self generateColorWithHue:0.8 andBrightness:1.0 andAlpha:1.0]]; | |
[self setBackgroundColor:[self generateColorWithHue:0.8 andBrightness:1.0 andAlpha:0.8]]; | |
[self makeMotion]; | |
} | |
return self; | |
} | |
- (void)drawRect:(CGRect)rect | |
{ | |
CGContextRef ctx = UIGraphicsGetCurrentContext(); | |
CGRect bounds = [self bounds]; | |
// CGPoint center; | |
center.x = bounds.origin.x + bounds.size.width / 2.0; | |
center.y = bounds.origin.y + bounds.size.height / 2.0; | |
float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0; | |
CGContextSetLineWidth(ctx, 10); | |
[[self circleColor] setStroke]; | |
for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20) { | |
CGContextAddArc(ctx, center.x, center.y, currentRadius, 0.0, M_PI * 2.0, YES); | |
CGContextStrokePath(ctx); | |
} | |
// BNR Tutorial stuff - leave it in for a second | |
// | |
// NSString *text = @"You are getting sleepy"; | |
// UIFont *font = [UIFont boldSystemFontOfSize:20]; | |
// CGRect textRect; | |
// textRect.size = [text sizeWithFont:font]; | |
// textRect.origin.x = center.x - textRect.size.width / 2.0; | |
// textRect.origin.y = center.y - textRect.size.height / 2.0; | |
// [[UIColor blackColor] setFill]; | |
// CGSize offset = CGSizeMake(0, 1); | |
// CGColorRef color = [[UIColor colorWithWhite:0 alpha:0.5] CGColor]; | |
// | |
// CGContextSetShadowWithColor(ctx, offset, 2.0, color); | |
// | |
// [text drawInRect:textRect withFont:font]; | |
} | |
- (BOOL)canBecomeFirstResponder | |
{ | |
return YES; | |
} | |
- (void)makeMotion | |
{ | |
motionManager = [[CMMotionManager alloc] init]; | |
motionManager.deviceMotionUpdateInterval = 1.0/10; | |
// NSOperationQueue *queueyQueue = [[NSOperationQueue alloc] init]; | |
if (motionManager.isDeviceMotionAvailable) { | |
// [motionManager startDeviceMotionUpdates]; | |
[motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] | |
withHandler:^(CMDeviceMotion *motion, NSError *error) { | |
CMAttitude *currentAttitude = motion.attitude; | |
// float yaw = roundf(RadiansToDegrees(currentAttitude.yaw)); | |
// [_yawLabel setText:[NSString stringWithFormat:@"Yaw: %f", yaw]]; | |
// [self.yawLabel setNeedsDisplay]; | |
float roll = 30 + MAX(-30, MIN(30, RadiansToDegrees(currentAttitude.roll))); | |
float pitch = 15 + MAX(-15, MIN(15, RadiansToDegrees(currentAttitude.pitch))); | |
// float yawPos = [self positionIn3D:yaw]; | |
// float rollPos = [self positionIn3D:roll]; | |
// [_posIn360Label setText:[NSString stringWithFormat:@"Hue: %i \nPitch: %i", (int) roundf(roll), (int) roundf(pitch)]]; | |
[self setCircleColor:[self generateColorWithHue:roll/60 andBrightness:pitch/30 andAlpha:1.0]]; | |
[self setBackgroundColor:[self generateColorWithHue:roll/60 andBrightness:pitch/30 andAlpha:0.8]]; | |
[self setNeedsDisplay]; | |
}]; | |
} else { | |
NSLog(@"No can doosville, babydoll"); | |
} | |
} | |
- (float)positionIn3D:(float)f | |
{ | |
float position = f; | |
if (position < 0) | |
{ | |
position = 360 + position; | |
} | |
return position; | |
} | |
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event | |
{ | |
UITouch *touch = [touches anyObject]; | |
CGPoint point = [touch locationInView:self]; | |
float widthPercent = point.x/self.bounds.size.width; | |
float heightPercent = point.y/self.bounds.size.height; | |
[self setCircleColor:[self generateColorWithHue:widthPercent andBrightness:heightPercent andAlpha:1.0]]; | |
[self setBackgroundColor:[self generateColorWithHue:widthPercent andBrightness:heightPercent andAlpha:0.8]]; | |
[self setNeedsDisplay]; | |
} | |
- (UIColor *)generateColorWithHue:(float)hue | |
andBrightness:(float)brightness | |
andAlpha:(float)alpha | |
{ | |
brightness = MAX(0.5, brightness); | |
hue = (360*hue)/360; | |
return [UIColor colorWithHue:hue saturation:0.93 brightness:brightness alpha:alpha]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment