Skip to content

Instantly share code, notes, and snippets.

@burtlo
Created June 6, 2013 04:10
Show Gist options
  • Save burtlo/5719253 to your computer and use it in GitHub Desktop.
Save burtlo/5719253 to your computer and use it in GitHub Desktop.
Rubymotion vs Objective-C
class RegisterUserScreen < PM::Screen
include ScreenElements
title "Provide Your Info"
def on_load
register_keyboard_events
tap = UITapGestureRecognizer.alloc.initWithTarget(self, action: :keyboard_did_hide)
self.view.addGestureRecognizer(tap)
end
def on_disappear
unregister_keyboard_events
end
def will_appear
view.styleId = "mainWindow"
add button class: 'next'
add UILabel.new, {
text: "Register",
resize: [:left, :right, :top, :bottom, :width, :height],
styleId: 'title'
}
add UILabel.new, {
text: "Enter your info",
resize: [:left, :right, :top, :bottom, :width, :height],
styleId: 'sub-title'
}
@first_name = add UITextField.new,
placeholder: 'First Name',
styleId: 'first_name'
@last_name = add UITextField.new,
placeholder: 'Last Name',
styleId: 'last_name'
@phone_number = add UITextField.new,
placeholder: '(555) 123-1234',
keyboardType: UIKeyboardTypeNumberPad,
styleId: 'phone_number'
end
def register_keyboard_events
NSNotificationCenter.defaultCenter.addObserver(self,selector: :keyboard_did_show, name: :UIKeyboardDidShowNotification, object: nil)
NSNotificationCenter.defaultCenter.addObserver(self,selector: :keyboard_did_hide, name: :UIKeyboardDidHideNotification, object: nil)
end
def unregister_keyboard_events
NSNotificationCenter.defaultCenter.removeObserver(self)
end
def keyboard_did_show
up_position = CGRectMake(0,-80,320,460)
UIView.animateWithDuration(0.2,animations: lambda { self.view.setFrame(up_position) },completion: lambda {|finished| })
end
def keyboard_did_hide
@first_name.resignFirstResponder
@last_name.resignFirstResponder
@phone_number.resignFirstResponder
original_position = CGRectMake(0,0,320,460)
UIView.animateWithDuration(0.2,animations: lambda { self.view.setFrame(original_position) },completion: lambda {|finished| })
end
end
//
// TSFPersonalInfoViewController.m
// Trail Safe
//
// Created by Franklin Webber on 6/1/13.
// Copyright (c) 2013 Trail Safe. All rights reserved.
//
#import "TSFPersonalInfoViewController.h"
#import "TSFUser.h"
#import "TSFServiceProvider.h"
@interface TSFPersonalInfoViewController ()
@end
@implementation TSFPersonalInfoViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(dismissKeyboardAnimated)];
[self.view addGestureRecognizer:tap];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self registerKeyboardEvents];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[self unregisterKeyboardEvents];
}
#pragma mark - Keyboard Events
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
- (void)keyboardDidShow:(NSNotification *)notification {
__block UIView *mainView = self.view;
[UIView animateWithDuration:0.2 animations:^{
[mainView setFrame:CGRectMake(0,-130,320,460)];
}];
}
-(void)keyboardDidHide:(NSNotification *)notification {
[self dismissKeyboardAnimated];
}
- (void)dismissKeyboardAnimated {
[[self phoneField] resignFirstResponder];
[[self nameField] resignFirstResponder];
__block UIView *mainView = self.view;
[UIView animateWithDuration:0.2 animations:^{
[mainView setFrame:CGRectMake(0,0,320,460)];
}];
}
- (void)registerKeyboardEvents {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
}
- (void)unregisterKeyboardEvents {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment