Last active
August 29, 2015 14:16
-
-
Save maltzsama/f26bf7e9b3d00cda1b1e 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
| // | |
| // LoginViewController.swift | |
| // ITSoftin | |
| // | |
| // Created by demetrius albuquerque on 08/12/14. | |
| // Copyright (c) 2014 Snowman Labs. All rights reserved. | |
| // | |
| import UIKit | |
| import Foundation | |
| import QuartzCore | |
| /** | |
| * `LoginViewController` controller of Login screen | |
| */ | |
| class LoginViewController: UIViewController, UITextFieldDelegate { | |
| @IBOutlet weak var userTxt: UITextField! | |
| @IBOutlet weak var passTxt: UITextField! | |
| @IBOutlet weak var blueView: UIView! | |
| @IBOutlet weak var enterBtn: UIButton! | |
| @IBOutlet weak var enterConstrain: NSLayoutConstraint! | |
| /** | |
| someconfiguration about view. | |
| */ | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| //Added tapGestureRecognizer to dismiss keyboard | |
| let tap = UITapGestureRecognizer(target: self, action: "dismissKeyboard") | |
| self.view.addGestureRecognizer(tap); | |
| blueView.layer.cornerRadius = 3; | |
| userTxt.layer.cornerRadius = 3; | |
| passTxt.layer.cornerRadius = 3; | |
| userTxt.delegate = self | |
| passTxt.delegate = self | |
| NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWasShown:"), name:UIKeyboardWillShowNotification, object: nil); | |
| NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil); | |
| } | |
| override func didReceiveMemoryWarning() { | |
| super.didReceiveMemoryWarning() | |
| // Dispose of any resources that can be recreated. | |
| } | |
| /** | |
| Add a gesture reconize when you press out of a UITextField | |
| :param: gestureRecognizer gestureRecognizer Gesture Recognizers to Simplify Event Handling | |
| :param: otherGestureRecognizer otherGestureRecognizer that must fail before the receiving recognizer can begin | |
| :returns: return true | |
| */ | |
| func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool { | |
| return true | |
| } | |
| /** | |
| Check if field have a valid value and give a different color if the result is true or false | |
| :param: sender sender UITextField | |
| :returns: return true or false after check if contains a valid value from sender | |
| */ | |
| func checkFields(sender: UITextField) -> Bool{ | |
| sender.layer.borderWidth = 1.0; | |
| if sender.text.isEmpty { | |
| sender.layer.borderColor = UIColor.redColor().CGColor; | |
| return true; | |
| } | |
| sender.layer.borderColor = UIColor.whiteColor().CGColor; | |
| return false; | |
| } | |
| /** | |
| Try login after clicked, if login is ok, go to MainView. Anotherway give a error message. | |
| :param: sender sender UIButton | |
| */ | |
| @IBAction func pressedButton(sender: NSObject) { | |
| if (checkFields(passTxt) && checkFields(userTxt)){ | |
| return; | |
| } | |
| if (checkFields(passTxt)){ | |
| return; | |
| } | |
| if(checkFields(userTxt)){ | |
| return; | |
| } | |
| UserFetcher.doLogin(userTxt.text, passwd: passTxt.text) { (result : NSDictionary) in | |
| if result["status"] as String == "fail" || result.count <= 0{ | |
| UIAlertView(title: "Erro", message: result["message"] as? String, delegate: nil, cancelButtonTitle: "OK").show() | |
| }else{ | |
| User.saveUser(self.userTxt.text, token: result["token"] as String) | |
| self.dismissViewControllerAnimated(false, completion: nil); | |
| ClientCollection.sharedInstance | |
| } | |
| } | |
| } | |
| /** | |
| change cursor to next textField | |
| :param: textField textField focus textField | |
| :returns: return false when became the next textField focused and true when enter button is pressed | |
| */ | |
| func textFieldShouldReturn(textField: UITextField) -> Bool { | |
| if textField.isEqual(userTxt){ | |
| passTxt.becomeFirstResponder(); | |
| return false; | |
| } | |
| self.pressedButton(self); | |
| return true; | |
| } | |
| /** | |
| selector to dismiss keyboard after some place of screen is touched | |
| */ | |
| func dismissKeyboard(){ | |
| userTxt.resignFirstResponder(); | |
| passTxt.resignFirstResponder(); | |
| } | |
| /** | |
| keyboardWasShow change value of contrain to move components on the screen | |
| :param: notification notification UIKeyboardWillShowNotification | |
| */ | |
| func keyboardWasShown(notification: NSNotification) { | |
| var info = notification.userInfo! | |
| var keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as NSValue).CGRectValue() | |
| UIView.animateWithDuration(0.1, animations: { () -> Void in | |
| self.enterConstrain.constant = keyboardFrame.size.height + 20 | |
| }) | |
| } | |
| /** | |
| keyboardWillHide move down the contrain to get original position | |
| :param: notification notification UIKeyboardWillHideNotification | |
| */ | |
| func keyboardWillHide(notification: NSNotification) { | |
| var info = notification.userInfo! | |
| var keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as NSValue).CGRectValue() | |
| UIView.animateWithDuration(0.1, animations: { () -> Void in | |
| self.enterConstrain.constant = self.enterConstrain.constant - keyboardFrame.size.height + 20 | |
| }) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment