import UIKit
import AVFoundation
class ViewController: UIViewController {
var player: AVAudioPlayer!
var tempSender: UIButton? = nil // save last used btn
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func keyPressed(_ sender: UIButton) {
if tempSender != nil {tempSender!.layer.opacity*=2} // revert opacity changes
tempSender=sender // save last clicked btn
sender.layer.opacity = sender.layer.opacity / 2 // decrease opacity of clicked btn
playSound(noteToPlay: sender.currentTitle!)
}
func playSound(noteToPlay: String) {
let url = Bundle.main.url(forResource: noteToPlay, withExtension: "wav")
player = try! AVAudioPlayer(contentsOf: url!)
player.play()
}
}
import UIKit
class ViewController: UIViewController {
let times: [String: Int] = ["soft":300, "medium":420,"hard":720]
var secondsRamaining = 60
var timer = Timer() // declare the timer
@IBAction func hardnessSelected(_ sender: UIButton) {
secondsRamaining = times[sender.currentTitle!.lowercased()]!
timer.invalidate() // kill the timer
// engage the timerevery 1 second in this case
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(fireTimer), userInfo: nil, repeats: true)
}
@objc func fireTimer() {
// this func will run every 1 second (assuming the timer has been engaged)
if secondsRamaining > 0 {
secondsRamaining-=1
print("\(secondsRamaining) remaining")
}
}
}
func greeter(msg message: String){
print(message)
}
greeter(msg: "hey")
set custom func with no parameter name :
func greeter(_ message: String){
print(message)
}
greeter("hey")
import UIKit
class SecondViewController: UIViewController {
// this is a 2nd activity
var data:Float = 0.0
override func viewDidLoad() {
super.viewDidLoad()
// func fires up when called by other activity
let label = UILabel() // create a label
label.text = String(format: "%.1f", data)
label.frame = CGRect(x: 0, y: 0, width: 100, height: 50)
//view.backgroundColor = UIColor.green
view.backgroundColor = .green
view.addSubview(label) // add label on this view controller
}
}
let secondVC = SecondViewController()
secondVC.data = BMI // example passing data to 2nd activity
self.present(secondVC, animated: true, completion: nil) // summon 2nd activity view controller
import UIKit
class ResultViewController: UIViewController {
var BMIResult :Float? = nil
@IBOutlet weak var lblBMI: UILabel!
@IBOutlet weak var lblAdvice: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
lblBMI.text = String(format: "%.1f", BMIResult!)
}
@IBAction func btnToMainActivity(_ sender: UIButton) {
self.dismiss(animated: true, completion: nil) // kill activity
// and thus go back to the summoning (prev) activity
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
import UIKit
class ViewController: UIViewController {
var BMIResult :Float? = nil
@IBOutlet weak var lblHeight: UILabel!
@IBOutlet weak var lblWeight: UILabel!
@IBOutlet weak var sliderHeight: UISlider!
@IBOutlet weak var sliderWeight: UISlider!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func heightSlideEvent(_ sender: UISlider) {
let temp = String(format: "%.2f", sender.value)
lblHeight.text = "\(temp)m"
}
@IBAction func weightSliderEvent(_ sender: UISlider) {
let temp : Int = Int(sender.value)
lblWeight.text = "\(temp)kg"
}
@IBAction func btnCalculate(_ sender: UIButton) {
let height = sliderHeight.value
let weight = sliderWeight.value
BMIResult = weight / pow(height,2)
self.performSegue(withIdentifier: "goToResult", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// this method runs right befor the segued activity is summoned
// refer to specific segue :
if segue.identifier == "goToResult"{
let destinationViewController = segue.destination as! ResultViewController // downcast to summoned activity view controller
destinationViewController.BMIResult = self.BMIResult // pass
//value from self to a value in the summoned activity
}
}
}