Skip to content

Instantly share code, notes, and snippets.

@chrisallick
Created September 12, 2013 21:37
Show Gist options
  • Save chrisallick/6544187 to your computer and use it in GitHub Desktop.
Save chrisallick/6544187 to your computer and use it in GitHub Desktop.
//
// MenuView.m
// GoodyVibrations
//
// Created by chrisallick on 3/22/13.
// Copyright (c) 2013 chrisallick. All rights reserved.
//
#import "MenuView.h"
#import "PRTween.h"
#import "GAI.h"
#import <QuartzCore/QuartzCore.h>
@implementation MenuView {
BOOL keyboard;
}
@synthesize start, menuViewDelegate, nameTextField, roomTextField, logo, info;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
[tracker sendView:@"/MenuView"];
keyboard = NO;
[self setBackgroundColor:[UIColor clearColor]];
UIImage *logoImage = [UIImage imageNamed:@"logo.png"];
if( IS_IPHONE_5 ) {
logo = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width/2-logoImage.size.width/4, 201/2.0, logoImage.size.width/2, logoImage.size.height/2)];
} else {
logo = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width/2-logoImage.size.width/4, 96/2.0, logoImage.size.width/2, logoImage.size.height/2)];
}
[logo setImage:logoImage];
[self addSubview:logo];
if( IS_IPHONE_5 ) {
nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(self.frame.size.width/2-528.0/4,543.0/2,528.0/2,109/2)];
} else {
nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(self.frame.size.width/2-528.0/4,379.0/2,528.0/2,109/2)];
}
[nameTextField setDelegate:self];
[nameTextField setReturnKeyType:UIReturnKeyDone];
[nameTextField setFont:[UIFont fontWithName:@"HittheRoad" size:28.0]];
[nameTextField setTextColor:[UIColor colorWithRed:0.266 green:0.670 blue:0.760 alpha:1.0]];
[nameTextField setAutocapitalizationType:UITextAutocapitalizationTypeAllCharacters];
[nameTextField setAutocorrectionType:UITextAutocorrectionTypeNo];
[nameTextField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[nameTextField setTextAlignment:NSTextAlignmentCenter];
[nameTextField setTag:1];
[nameTextField setBorderStyle:UITextBorderStyleNone];
[nameTextField.layer setBorderWidth:4.5];
[nameTextField.layer setBorderColor:[UIColor colorWithRed:0.266 green:0.670 blue:0.760 alpha:1.0].CGColor];
[nameTextField setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:@"NAME" attributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:0.266 green:0.670 blue:0.760 alpha:1.0]}]];
[self addSubview:nameTextField];
if( IS_IPHONE_5 ) {
roomTextField = [[UITextField alloc] initWithFrame:CGRectMake(self.frame.size.width/2-528.0/4,702.0/2,528.0/2,109/2)];
} else {
roomTextField = [[UITextField alloc] initWithFrame:CGRectMake(self.frame.size.width/2-528.0/4,538.0/2,528.0/2,109/2)];
}
[roomTextField setDelegate:self];
[roomTextField setTag:2];
[roomTextField setFont:[UIFont fontWithName:@"HittheRoad" size:28.0]];
[roomTextField setTextColor:[UIColor colorWithRed:0.266 green:0.670 blue:0.760 alpha:1.0]];
[roomTextField setTextAlignment:NSTextAlignmentCenter];
[roomTextField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[roomTextField setAutocapitalizationType:UITextAutocapitalizationTypeAllCharacters];
[roomTextField setAutocorrectionType:UITextAutocorrectionTypeNo];
[roomTextField setReturnKeyType:UIReturnKeyDone];
[roomTextField setBorderStyle:UITextBorderStyleNone];
[roomTextField.layer setBorderWidth:4.5];
[roomTextField.layer setBorderColor:[UIColor colorWithRed:0.266 green:0.670 blue:0.760 alpha:1.0].CGColor];
[roomTextField setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:@"ROOM" attributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:0.266 green:0.670 blue:0.760 alpha:1.0]}]];
[self addSubview:roomTextField];
start = [UIButton buttonWithType:UIButtonTypeCustom];
[start setImage:[UIImage imageNamed:@"start.png"] forState:UIControlStateNormal];
[start setAdjustsImageWhenHighlighted:NO];
if( IS_IPHONE_5 ) {
[start setFrame:CGRectMake(self.frame.size.width/2-528/4, 859/2, 528/2, 109/2 )];
} else {
[start setFrame:CGRectMake(self.frame.size.width/2-528/4, 695/2, 528/2, 109/2 )];
}
[start setTag:1];
[start addTarget:self action:@selector(onTouchUp:) forControlEvents:UIControlEventTouchUpOutside];
[start addTarget:self action:@selector(onTouchDown:) forControlEvents:UIControlEventTouchDown];
[start addTarget:self action:@selector(onTap:) forControlEvents:UIControlEventTouchUpInside];
[start addTarget:self action:@selector(onTouchUp:) forControlEvents:UIControlEventTouchDragExit];
[start setAlpha:0.0];
[self addSubview:start];
UIImage *infoImage = [UIImage imageNamed:@"info.png"];
info = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width/2-infoImage.size.width/4, self.bounds.size.height-infoImage.size.height/2-15, infoImage.size.width/2, infoImage.size.height/2)];
[info setAdjustsImageWhenHighlighted:NO];
[info setImage:infoImage forState:UIControlStateNormal];
[info setTag:2];
[info addTarget:self action:@selector(onTouchUp:) forControlEvents:UIControlEventTouchUpOutside];
[info addTarget:self action:@selector(onTouchDown:) forControlEvents:UIControlEventTouchDown];
[info addTarget:self action:@selector(onTap:) forControlEvents:UIControlEventTouchUpInside];
[info addTarget:self action:@selector(onTouchUp:) forControlEvents:UIControlEventTouchDragExit];
[self addSubview:info];
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"click" ofType:@"m4a"];
click = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundPath] error:nil];
[click setVolume: 0.6];
[click prepareToPlay];
}
return self;
}
// prevent spaces in rooms
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if( [string isEqualToString:@" "] ){
return NO;
}
return YES;
}
-(void) showStartButton {
[UIView beginAnimations:@"animationOn" context:NULL];
[UIView setAnimationDuration:.150];
[start setAlpha:1.0];
[UIView commitAnimations];
}
-(void) hideStartButton {
[UIView beginAnimations:@"animationOn" context:NULL];
[UIView setAnimationDuration:.150];
[start setAlpha:0.0];
[UIView commitAnimations];
}
-(void)onTouchUp:(UIButton *)sender {
[UIView beginAnimations:@"animationOn" context:NULL];
[UIView setAnimationDuration:.150];
[sender setTransform:CGAffineTransformIdentity];
[UIView commitAnimations];
}
-(void)onTouchDown:(UIButton *)sender {
[UIView beginAnimations:@"animationOn" context:NULL];
[UIView setAnimationDuration:.150];
[sender setTransform:CGAffineTransformMakeScale(0.85, 0.85)];
[UIView commitAnimations];
}
-(void)playClick {
[click play];
}
-(void)onTap:(UIButton *)sender {
[self playClick];
[UIView beginAnimations:@"animationOn" context:NULL];
[UIView setAnimationDuration:.150];
[sender setTransform:CGAffineTransformIdentity];
[UIView commitAnimations];
[self endEditing:YES];
if( [sender tag] == 1 ) {
[UIView animateWithDuration: 0.2
delay: 0.0
options: UIViewAnimationCurveEaseIn
animations:^{
[self setAlpha:0.0];
}
completion:^(BOOL finished) {
[self setHidden:YES];
[menuViewDelegate startGame];
}];
} else if( [sender tag] == 2 ) {
[UIView animateWithDuration: 0.2
delay: 0.0
options: UIViewAnimationCurveEaseIn
animations:^{
[self setAlpha:0.0];
}
completion:^(BOOL finished) {
[self setHidden:YES];
[menuViewDelegate loadInfo];
}];
}
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[textField setPlaceholder:nil];
[textField setAlpha:1.0];
[self hideStartButton];
if( !keyboard ) {
keyboard = YES;
[UIView animateWithDuration: 0.2
delay: 0.0
options: UIViewAnimationCurveEaseIn
animations:^{
if( IS_IPHONE_5 ) {
[nameTextField setFrame:CGRectMake(nameTextField.frame.origin.x,230.0/2,nameTextField.frame.size.width,nameTextField.frame.size.height)];
[roomTextField setFrame:CGRectMake(roomTextField.frame.origin.x,388.0/2,roomTextField.frame.size.width,roomTextField.frame.size.height)];
} else {
[nameTextField setFrame:CGRectMake(nameTextField.frame.origin.x,130.0/2,nameTextField.frame.size.width,nameTextField.frame.size.height)];
[roomTextField setFrame:CGRectMake(roomTextField.frame.origin.x,288.0/2,roomTextField.frame.size.width,roomTextField.frame.size.height)];
}
[start setAlpha:0.0];
[logo setAlpha:0.0];
}
completion:^(BOOL finished) {
}];
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
if( [textField tag] == 1 ) {
nameTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"NAME" attributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:0.266 green:0.670 blue:0.760 alpha:1.0]}];
} else {
roomTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"ROOM" attributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:0.266 green:0.670 blue:0.760 alpha:1.0]}];
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
UITextField *name = (UITextField*)[self viewWithTag:1];
UITextField *room = (UITextField*)[self viewWithTag:2];
if( keyboard ) {
keyboard = NO;
[UIView animateWithDuration: 0.2
delay: 0.0
options: UIViewAnimationCurveEaseIn
animations:^{
if( IS_IPHONE_5 ) {
[nameTextField setFrame:CGRectMake(nameTextField.frame.origin.x,543.0/2,nameTextField.frame.size.width,nameTextField.frame.size.height)];
[roomTextField setFrame:CGRectMake(roomTextField.frame.origin.x,702.0/2,roomTextField.frame.size.width,roomTextField.frame.size.height)];
} else {
[nameTextField setFrame:CGRectMake(nameTextField.frame.origin.x,379.0/2,nameTextField.frame.size.width,nameTextField.frame.size.height)];
[roomTextField setFrame:CGRectMake(roomTextField.frame.origin.x,538.0/2,roomTextField.frame.size.width,roomTextField.frame.size.height)];
}
[logo setAlpha:1.0];
}
completion:^(BOOL finished) {
if( [name.text length] != 0 && [room.text length] != 0 ) {
[[NSUserDefaults standardUserDefaults] setObject:name.text forKey:@"username"];
[[NSUserDefaults standardUserDefaults] setObject:room.text forKey:@"roomname"];
[self showStartButton];
} else {
[self hideStartButton];
}
}];
}
[textField resignFirstResponder];
return YES;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment