Last active
November 19, 2018 12:35
-
-
Save hilfritz/7a05027a9f8559913f46675ea0608a01 to your computer and use it in GitHub Desktop.
IOS study
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
//----------------------------------SHORTCUTS---------------------- | |
AUTORESIZE LABEL ACCORDING TO TEXT | |
- SELECT LABEL + press CMD + '=' | |
STRING CONCAT | |
//let passwordInput = "HI THERE" | |
//let usernameInput = "HELLO THERE | |
let passwordInput:String = passwordField.text ?? "" | |
let usernameInput:String = userNameField.text ?? "" | |
let age = 19 | |
print("userName: \(usernameInput) password: \(passwordInput) age:\(age)") | |
//SAME AS | |
print("userName:"+usernameInput+" password:"+passwordInput) | |
//SAME AS | |
print("userName: \(String(describing: usernameInput)) password: \(String(describing: passwordInput))") | |
CLOSING A PAGE | |
- self.dismiss() | |
CHANGINg UIImageView image | |
@IBOutlet weak var image: UIImageView! | |
//... | |
image.image = UIImage(named:"sample") //'image' sample is saved in Assets folder as 'sample.png' | |
image.image = sample //similar as above | |
//----------------------------------A---------------------- | |
//Sample playing a .wav file named "hustle-on.wav" inside the project | |
import AVFoundation | |
var player: AVAudioPlayer! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let path = Bundle.main.path(forResource: "hustle-on", ofType: "wav")! | |
let url = URL(fileURLWithPath: path) | |
do{ | |
player = try AVAudioPlayer(contentsOf: url) | |
player.prepareToPlay() | |
}catch let error as NSError{ | |
print(error.description) | |
} | |
} | |
//on button click play the wav file | |
@IBAction func onPowerButtonPress(_ sender: Any) { | |
player.play() | |
UIView.animate(withDuration: 2.3, animations: { | |
self.rocket.frame = CGRect(x: 0, y: 150, width: 240, height: 128) | |
}){(finished) in | |
self.hustleLbl.isHidden = false | |
self.statusLbl.isHidden = false | |
} | |
} | |
//----------------------------------A---------------------- | |
//----------------------------------A---------------------- | |
//HIDE KEYBOARD ON TOUCH OUTSIDE THE TEXTFIELD | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
addHideKeyboardOnTouchOutside() | |
} | |
func addHideKeyboardOnTouchOutside(){ | |
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard)) | |
view.addGestureRecognizer(tap) // Allows dismissal of keyboard on tap anywhere on screen besides the keyboard itself | |
} | |
@objc func dismissKeyboard() { | |
//Causes the view (or one of its embedded text fields) to resign the first responder status and drop into background | |
view.endEditing(true) | |
} | |
//----------------------------------A---------------------- | |
//----------------------------------A---------------------- | |
//OPENING A NEW PAGE | |
1. THE STORYBOARD NEEDS TO HAVE A 'STORYBOARDID' (ex. CalendarViewController is the classname and 'CalendarViewController' is also the | |
storyboard Id) | |
2. HERE IS THE CODE | |
let pageToOpenViewController = self.storyboard?.instatiateViewController(withIdentifier: "<paste-the-storyboard-id>" as! | |
<Class-name> | |
self.present(pageToOpenViewController, animated:true) | |
so it will be like this | |
let pageToOpenViewController = self.storyboard?.instatiateViewController(withIdentifier: "CalendarViewController" as! | |
CalendarViewController | |
self.present(pageToOpenViewController, animated:true) | |
//----------------------------------A---------------------- | |
//----------------------------------A---------------------- | |
//UPDATE THE VIEW INSIDE THE MAIN THREAD | |
DispatchQueue.main.async | |
{ | |
//update the ui views here | |
} | |
//----------------------------------A---------------------- | |
//----------------------------------A---------------------- | |
//CHANGE THE ROOT VIEW OF THE APP | |
let pageToMakeAsRootViewController = self.storyboard?.instatiateViewController(withIdentifier: "RootViewController" as! | |
RootViewController | |
let appDelegate = UIApplication.shared.delegate | |
appDelegate?.window??.rootViewController = homePage | |
//----------------------------------A---------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment