Created
November 27, 2011 21:36
-
-
Save Nub/1398189 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
| // | |
| // YRFrequenciesLCDView.h | |
| // frequenciesLCD | |
| // | |
| // Created by Zachry Thayer on 11/21/11. | |
| // Copyright (c) 2011 Zachry Thayer. All rights reserved. | |
| // | |
| #import <UIKit/UIKit.h> | |
| #define kYRFrequenciesLCDViewBackgroundImage @"lcd.png" | |
| #define kYRFrequenciesLCDViewBOverlayImage @"lcdScan.png" | |
| #define kYRFramePadding 2.f | |
| #define kYRFiniteBarXPadding 10.f | |
| #define kYRFiniteBarYPadding 20.f | |
| #define kYRFiniteBarYTextPadding 25.f | |
| #define kYRFiniteBarThickness 2.f | |
| #define kYRFiniteBarDetailTranslate -3.f | |
| #define kYRFiniteBarDetailThickness 1.f | |
| #define kYRFreqBarXPadding 0.f | |
| #define kYRFreqBarYPadding 54.f | |
| #define kYRFreqBarXTextPadding 10.f | |
| #define kYRFreqBarYTextPadding 36.f | |
| #define kYRFreqBarThickness 2.f | |
| #define kYRFreqBarDetailTranslate 3.f | |
| #define kYRFreqBarDetailThickness 1.f | |
| #define kYRlargeYOffset 2.f | |
| #define kYRlargeBlockWidth 2.f | |
| #define kYRlargeBlockThickness 2.f | |
| #define kYRFiniteNeedleWidth 1.5f | |
| #define kYRFiniteNeedleHeight 22.f | |
| #define kYRFreqNeedleWidth 1.5f | |
| #define kYRFreqNeedleHeight 20.f | |
| @interface YRFrequenciesLCDView : UIView | |
| @property (nonatomic) CGFloat frequency; | |
| @end |
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
| // | |
| // YRFrequenciesLCDView.m | |
| // frequenciesLCD | |
| // | |
| // Created by Zachry Thayer on 11/21/11. | |
| // Copyright (c) 2011 Zachry Thayer. All rights reserved. | |
| // | |
| #import "YRFrequenciesLCDView.h" | |
| NSInteger YRFrequencies[] = { | |
| 91, 94, 97, 100, 103, 106, 109 | |
| }; | |
| NSInteger YRFrequenciesCount = 7; | |
| NSInteger YRFreqSpread; | |
| #pragma mark Class Extension | |
| @interface YRFrequenciesLCDView () | |
| { | |
| CGFloat finiteNeedleX; | |
| CGFloat freqNeedleX; | |
| } | |
| @property (nonatomic, strong) UIImage *backgroundImage; | |
| @property (nonatomic, strong) UIImage *overlayImage; | |
| @property (nonatomic, strong) UIBezierPath *finiteBar; | |
| @property (nonatomic, strong) UIBezierPath *freqBar; | |
| @property (nonatomic, strong) UIBezierPath *largeBlock; | |
| @property (nonatomic, strong) UIBezierPath *finiteNeedle; | |
| @property (nonatomic, strong) UIBezierPath *freqNeedle; | |
| @property (nonatomic, strong) UIFont *finiteFont; | |
| @property (nonatomic, strong) UIFont *freqFont; | |
| - (void)initialize; | |
| @end | |
| #pragma mark - Class Implementation | |
| @implementation YRFrequenciesLCDView | |
| @synthesize frequency; | |
| @synthesize backgroundImage; | |
| @synthesize overlayImage; | |
| @synthesize finiteBar; | |
| @synthesize freqBar; | |
| @synthesize finiteNeedle; | |
| @synthesize freqNeedle; | |
| @synthesize largeBlock; | |
| @synthesize finiteFont; | |
| @synthesize freqFont; | |
| #pragma mark - Life Cycle | |
| - (id)init{ | |
| self = [super init]; | |
| if (self) | |
| { | |
| [self initialize]; | |
| } | |
| return self; | |
| } | |
| - (id)initWithCoder:(NSCoder *)aDecoder | |
| { | |
| self = [super initWithCoder:aDecoder]; | |
| if (self) { | |
| [self initialize]; | |
| } | |
| return self; | |
| } | |
| - (id)initWithFrame:(CGRect)frame { | |
| self = [super initWithFrame:frame]; | |
| if (self) | |
| { | |
| [self initialize]; | |
| } | |
| return self; | |
| } | |
| - (void)initialize | |
| { | |
| self.backgroundColor = [UIColor clearColor]; | |
| YRFreqSpread = YRFrequencies[YRFrequenciesCount-1] - YRFrequencies[0]; | |
| // Initialize objects | |
| [self backgroundImage]; | |
| [self overlayImage]; | |
| [self finiteBar]; | |
| [self freqBar]; | |
| [self largeBlock]; | |
| [self finiteNeedle]; | |
| [self freqNeedle]; | |
| [self finiteFont]; | |
| [self freqFont]; | |
| [self setFrequency:107.5f]; | |
| } | |
| - (void)dealloc{ | |
| backgroundImage = nil; | |
| finiteBar = nil; | |
| freqBar = nil; | |
| largeBlock = nil; | |
| finiteFont = nil; | |
| freqFont = nil; | |
| //[super dealloc]; | |
| } | |
| #pragma mark - Draw | |
| - (void)drawRect:(CGRect)rect | |
| { | |
| //Some variables | |
| CGSize size = self.bounds.size; | |
| CGContextRef context = UIGraphicsGetCurrentContext(); | |
| CGContextClearRect(context, self.bounds); | |
| //Snap grid for sharp pixels | |
| //CGContextTranslateCTM (context, 0.5f,0.5f); | |
| //Draw screen in current context | |
| [backgroundImage drawInRect:self.bounds]; | |
| CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]); | |
| CGContextSetAlpha(context, 0.5f); | |
| // Finite Freq bar | |
| [finiteBar stroke]; | |
| //Detail finite bar | |
| [finiteBar applyTransform:CGAffineTransformMakeTranslation(0, kYRFiniteBarDetailTranslate - 0.5f)]; | |
| [finiteBar setLineWidth:kYRFiniteBarDetailThickness]; | |
| [finiteBar stroke]; | |
| //Move Back intoplace | |
| [finiteBar applyTransform:CGAffineTransformIdentity]; | |
| // Frequency bar | |
| [freqBar stroke]; | |
| //Detail Freq bar | |
| [freqBar applyTransform:CGAffineTransformMakeTranslation(0, kYRFreqBarDetailTranslate + 0.5f)]; | |
| [freqBar setLineWidth:kYRFreqBarDetailThickness]; | |
| [freqBar stroke]; | |
| CGFloat finiteWidth = (size.width - (kYRFiniteBarXPadding) - (kYRFramePadding)) - (kYRFramePadding + kYRFiniteBarXPadding); | |
| [largeBlock applyTransform:CGAffineTransformMakeTranslation(0,kYRFiniteBarYPadding + kYRlargeYOffset)]; | |
| for (int i = 0; i < 11; i ++) { | |
| NSString *number = [NSString stringWithFormat:@"%i", i]; | |
| CGFloat x = kYRFramePadding + kYRFiniteBarXPadding + (finiteWidth/10.f * i); | |
| x = floorf(x); | |
| CGSize numberSize = [number sizeWithFont:finiteFont]; | |
| [number drawAtPoint:CGPointMake(x - (numberSize.width * 0.35f), kYRFiniteBarYTextPadding) withFont:finiteFont]; | |
| CGContextTranslateCTM(context, (i == 9)?x-kYRlargeBlockWidth:x, 0.f); | |
| //[largeBlock applyTransform:CGAffineTransformMakeTranslation(x,0)]; | |
| [largeBlock stroke]; | |
| CGContextTranslateCTM(context, -x, 0); | |
| } | |
| [largeBlock applyTransform:CGAffineTransformMakeTranslation(0,(kYRFiniteBarYPadding + kYRlargeYOffset) * -1.f)]; | |
| NSString *x = @"106"; | |
| CGSize xs = [x sizeWithFont:[UIFont fontWithName:@"Futura-CondensedMedium" size:8.f]]; | |
| CGFloat freqWidth = (size.width - (kYRFreqBarXTextPadding * 2) - (kYRFramePadding*2.f)) - (xs.width * 0.5f); | |
| [largeBlock applyTransform:CGAffineTransformMakeTranslation(0,kYRFreqBarYPadding - kYRlargeYOffset)]; | |
| for (int i = 0; i < YRFrequenciesCount; i ++) { | |
| NSString *number = [NSString stringWithFormat:@"%i", YRFrequencies[i]]; | |
| CGSize numberSize = [number sizeWithFont:freqFont]; | |
| CGFloat x = kYRFramePadding + kYRFreqBarXTextPadding + (freqWidth * (((float)YRFrequencies[i] - YRFrequencies[0])/(float)YRFreqSpread)); | |
| x = floorf(x); | |
| [number drawAtPoint:CGPointMake((x - (numberSize.width * 0.35f)), kYRFreqBarYTextPadding) withFont:freqFont]; | |
| CGContextTranslateCTM(context, x, 0); | |
| //[largeBlock applyTransform:CGAffineTransformMakeTranslation(x,0)]; | |
| [largeBlock stroke]; | |
| CGContextTranslateCTM(context, -x, 0); | |
| } | |
| [largeBlock applyTransform:CGAffineTransformMakeTranslation(0,(kYRFreqBarYPadding - kYRlargeYOffset) * -1.f)]; | |
| CGContextSetAlpha(context, 1.f); | |
| CGContextSetFillColorWithColor(context, [[UIColor colorWithRed:0.7f green:0 blue:0 alpha:1.f] CGColor]); | |
| CGRect overlayRect = self.bounds; | |
| overlayRect.size.width -= 3.f; | |
| overlayRect.size.height -= 2.f; | |
| overlayRect.origin.x += 3.5f; | |
| overlayRect.origin.y += 1.f; | |
| [overlayImage drawInRect:overlayRect]; | |
| [finiteNeedle applyTransform:CGAffineTransformMakeTranslation(finiteNeedleX, kYRFramePadding-0.5f)]; | |
| [finiteNeedle stroke]; | |
| [finiteNeedle fill]; | |
| [finiteNeedle applyTransform:CGAffineTransformMakeTranslation(-1.f * finiteNeedleX,-1.f * kYRFramePadding-0.5f)]; | |
| [freqNeedle applyTransform:CGAffineTransformMakeTranslation(freqNeedleX,size.height - (kYRFramePadding-0.5f))]; | |
| [freqNeedle stroke]; | |
| [freqNeedle fill]; | |
| [freqNeedle applyTransform:CGAffineTransformMakeTranslation(-1.f * freqNeedleX,-1.f * size.height - (kYRFramePadding-0.5f))]; | |
| // Render a glossy gradient on top! | |
| CGGradientRef glossGradient; | |
| CGColorSpaceRef rgbColorSpace; | |
| size_t num_locations = 2; | |
| CGFloat locations[2] = { | |
| 0.25, 1.0 | |
| }; | |
| CGFloat components[8] = { | |
| 1.0, 1.0, 1.0, 0.00f, // Start color | |
| 1.0, 1.0, 1.0, 0.50f // End color | |
| }; | |
| rgbColorSpace = CGColorSpaceCreateDeviceRGB(); | |
| glossGradient = CGGradientCreateWithColorComponents(rgbColorSpace, components, locations, num_locations); | |
| CGPoint topCenter = CGPointMake(finiteNeedleX, 0.0); | |
| CGPoint topQuarterBoundary = CGPointMake(finiteNeedleX+2.f,0.0); | |
| //CGContextSetBlendMode(context, kCGBlendModeOverlay); | |
| CGContextAddPath(context, [finiteNeedle CGPath]); | |
| CGContextAddPath(context, [freqNeedle CGPath]); | |
| CGContextClip(context); | |
| CGContextDrawLinearGradient(context, glossGradient, topCenter, topQuarterBoundary, 0); | |
| CGPoint topCenter2 = CGPointMake(freqNeedleX, 0.0); | |
| CGPoint topQuarterBoundary2 = CGPointMake(freqNeedleX+2.f,0.0); | |
| CGContextDrawLinearGradient(context, glossGradient, topCenter2, topQuarterBoundary2, 0); | |
| // CGContextSetBlendMode(context, kCGBlendModeNormal); | |
| CGContextClipToRect(context, rect); | |
| CGGradientRelease(glossGradient); | |
| } | |
| #pragma mark - Setters | |
| - (void)setFrame:(CGRect)frame | |
| { | |
| [super setFrame:frame]; | |
| //Recreate bars | |
| finiteBar = nil; | |
| freqBar = nil; | |
| [self finiteBar]; | |
| [self freqBar]; | |
| [self setNeedsDisplay]; | |
| } | |
| - (void)setFrequency:(CGFloat)newFrequency | |
| { | |
| CGSize size = self.bounds.size; | |
| frequency = newFrequency; | |
| NSString *x = @"106"; | |
| CGSize xs = [x sizeWithFont:[UIFont fontWithName:@"Futura-CondensedMedium" size:8.f]]; | |
| CGFloat freqWidth = (size.width - (kYRFreqBarXTextPadding * 2) - (kYRFramePadding*2.f)) - (xs.width * 0.5f); | |
| CGFloat finiteWidth = (size.width - (kYRFiniteBarXPadding) - (kYRFramePadding)) - (kYRFramePadding + kYRFiniteBarXPadding); | |
| freqNeedleX = kYRFreqNeedleWidth + kYRFramePadding + kYRFreqBarXTextPadding + (freqWidth * ((frequency - YRFrequencies[0])/YRFreqSpread)); | |
| finiteNeedleX = kYRFiniteNeedleWidth + kYRFramePadding + kYRFiniteBarXPadding + ((frequency - floorf(frequency))* finiteWidth); | |
| // Tell set to redraw | |
| [self setNeedsDisplay]; | |
| } | |
| #pragma mark - Getters | |
| - (UIImage*)backgroundImage | |
| { | |
| if (!backgroundImage) { | |
| backgroundImage = [[UIImage imageNamed:kYRFrequenciesLCDViewBackgroundImage] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)]; | |
| } | |
| return backgroundImage; | |
| } | |
| - (UIImage*)overlayImage | |
| { | |
| if (!overlayImage) { | |
| overlayImage = [[UIImage imageNamed:kYRFrequenciesLCDViewBOverlayImage] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)]; | |
| } | |
| return overlayImage; | |
| } | |
| - (UIBezierPath*)finiteBar | |
| { | |
| if (!finiteBar) | |
| { | |
| //Some variables | |
| CGSize size = self.bounds.size; | |
| finiteBar = [[UIBezierPath alloc] init]; | |
| [finiteBar moveToPoint:CGPointMake(kYRFramePadding + kYRFiniteBarXPadding, kYRFiniteBarYPadding)]; | |
| [finiteBar addLineToPoint:CGPointMake(size.width - (kYRFiniteBarXPadding) - (kYRFramePadding) , kYRFiniteBarYPadding)]; | |
| [finiteBar setLineWidth:kYRFiniteBarThickness]; | |
| } | |
| return finiteBar; | |
| } | |
| - (UIBezierPath*)freqBar | |
| { | |
| if (!freqBar) | |
| { | |
| //Some variables | |
| CGSize size = self.bounds.size; | |
| freqBar = [[UIBezierPath alloc] init]; | |
| [freqBar moveToPoint:CGPointMake(kYRFramePadding + kYRFreqBarXPadding, kYRFreqBarYPadding)]; | |
| [freqBar addLineToPoint:CGPointMake(size.width - (kYRFramePadding), kYRFreqBarYPadding)]; | |
| [freqBar setLineWidth:kYRFreqBarThickness]; | |
| } | |
| return freqBar; | |
| } | |
| - (UIBezierPath*)largeBlock | |
| { | |
| if (!largeBlock) { | |
| largeBlock = [[UIBezierPath alloc] init]; | |
| [largeBlock moveToPoint:CGPointMake(0,0)]; | |
| [largeBlock addLineToPoint:CGPointMake(kYRlargeBlockWidth, 0)]; | |
| [largeBlock setLineWidth:kYRlargeBlockThickness]; | |
| } | |
| return largeBlock; | |
| } | |
| - (UIBezierPath*)finiteNeedle | |
| { | |
| if (!finiteNeedle) { | |
| finiteNeedle = [[UIBezierPath alloc] init]; | |
| [finiteNeedle moveToPoint:CGPointMake(0,0)]; | |
| [finiteNeedle addLineToPoint:CGPointMake(kYRFiniteNeedleWidth, 0)]; | |
| [finiteNeedle addLineToPoint:CGPointMake(kYRFiniteNeedleWidth, kYRFiniteNeedleHeight)]; | |
| //[finiteNeedle addLineToPoint:CGPointMake(kYRFiniteNeedleWidth/2.f, kYRFiniteNeedleHeight+20.f)]; | |
| [finiteNeedle addLineToPoint:CGPointMake(0, kYRFiniteNeedleHeight)]; | |
| [finiteNeedle closePath]; | |
| // [finiteNeedle se | |
| [finiteNeedle setLineWidth:0.5f]; | |
| } | |
| return finiteNeedle; | |
| } | |
| - (UIBezierPath*)freqNeedle | |
| { | |
| if (!freqNeedle) { | |
| freqNeedle = [[UIBezierPath alloc] init]; | |
| [freqNeedle moveToPoint:CGPointMake(0,-kYRFreqNeedleHeight)]; | |
| // [freqNeedle moveToPoint:CGPointMake(kYRFreqNeedleWidth/2.f,-kYRFreqNeedleHeight-3.f)]; | |
| [freqNeedle addLineToPoint:CGPointMake(kYRFreqNeedleWidth,-kYRFreqNeedleHeight)]; | |
| [freqNeedle addLineToPoint:CGPointMake(kYRFreqNeedleWidth, 0)]; | |
| [freqNeedle addLineToPoint:CGPointMake(0, 0)]; | |
| [freqNeedle closePath]; | |
| // [finiteNeedle se | |
| [freqNeedle setLineWidth:0.5f]; | |
| } | |
| return freqNeedle; | |
| } | |
| - (UIFont*)finiteFont | |
| { | |
| if (!finiteFont) { | |
| finiteFont = [UIFont fontWithName:@"Futura-CondensedMedium" size:6.f]; | |
| } | |
| return finiteFont; | |
| } | |
| - (UIFont*)freqFont | |
| { | |
| if (!freqFont) { | |
| freqFont = [UIFont fontWithName:@"Futura-CondensedMedium" size:10.f]; | |
| } | |
| return freqFont; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment