UI Design Mobile Application Development in iOS School of EECS - - PowerPoint PPT Presentation

ui design
SMART_READER_LITE
LIVE PREVIEW

UI Design Mobile Application Development in iOS School of EECS - - PowerPoint PPT Presentation

UI Design Mobile Application Development in iOS School of EECS Washington State University Instructor: Larry Holder Mobile Application Development in iOS 1 Outline Model-View-Controller (MVC) design Timers Multi-threading Mobile


slide-1
SLIDE 1

UI Design

Mobile Application Development in iOS School of EECS Washington State University Instructor: Larry Holder

Mobile Application Development in iOS 1

slide-2
SLIDE 2

Outline

  • Model-View-Controller (MVC) design
  • Timers
  • Multi-threading

Mobile Application Development in iOS 2

slide-3
SLIDE 3

Model-View-Controller (MVC)

Mobile Application Development in iOS 3

class ViewController { IBAction … IBOutlet … } class Model { var value: Int … } Storyboard…

D a t a D e l e g a t e

slide-4
SLIDE 4

General Scenario

  • Want to modify view when some event

happens within model

  • E.g., if counter == 0, then display message

Mobile Application Development in iOS 4

slide-5
SLIDE 5

Bad Solution

Mobile Application Development in iOS 5

class MyViewController: UIViewController { @IBOutlet weak var myLabel: UILabel! var badModel = BadModel() badModel.label = myLabel badModel.decrement() } class BadModel { var label: UILabel! var counter: Int = 1 func decrement() { counter = counter – 1 if counter == 0 { label.text = "Time's up!" } } }

slide-6
SLIDE 6

Good (MVC) Solution

Mobile Application Development in iOS 6

protocol MyDelegate { func timesUp() } class MyViewController: UIViewController, MyDelegate { @IBOutlet weak var myLabel: UILabel! var goodModel = GoodModel() goodModel.delegate = self func timesUp() { myLabel.text = "Time's up!" // What if we want to make other changes? } }

slide-7
SLIDE 7

Good (MVC) Solution

Mobile Application Development in iOS 7

class GoodModel { var delegate: MyDelegate? var counter: Int = 0 func decrement() { counter = counter – 1 if counter == 0 { delegate?.timesUp() } } }

slide-8
SLIDE 8

MyTimer Example

Mobile Application Development in iOS 8

slide-9
SLIDE 9

Model: MyTimer Class

Mobile Application Development in iOS 9

protocol MyTimerDelegate { func timeChanged (time: Int) func timesUp () } class MyTimer { var delegate: MyTimerDelegate? var initialTime: Int = 60 var currentTime: Int = 60 func setInitialTime(_ initTime: Int) { initialTime = initTime } func start() { // todo } func stop() { // todo } func reset() { // todo } }

slide-10
SLIDE 10

SideBar: Multi-Threading

  • Running multiple tasks concurrently
  • Tasks assigned to threads
  • Threads assigned to cores

Mobile Application Development in iOS 10

A11: 6 cores, iPhone 8/X A12: 6 CPU cores, 4 GPU cores, 8 NPU cores, iPhone XS A13: 6 CPU cores, 4 GPU cores, 8 NPU cores, iPhone 11

slide-11
SLIDE 11

Multi-Threading in iOS

  • Manipulate “queues”, not threads
  • Grand Central Dispatch (GCD)

– iOS mechanism for sending tasks to different queues – You can create your own queues, and assign tasks to them – Threads take tasks from queues and run them on a core

  • Main queue used for UI and user interaction

– Don’t put intensive tasks on Main queue!

  • Also, UI changes not on Main queue “lost”

Mobile Application Development in iOS 11

DispatchQueue.main.async { // Do something easy // Or affecting UI } let myQueue = DispatchQueue.global() myQueue.async { // Do something in background // Not affecting UI }

slide-12
SLIDE 12

Timers

  • Timer class

– Wait specified time interval, then call function – Can repeat

  • Start immediately
  • Stop

Mobile Application Development in iOS 12

Timer.scheduledTimer(withTimeInterval interval: TimeInterval, repeats: Bool, block: @escaping (Timer) -> Void) -> Timer Timer.invalidate()

slide-13
SLIDE 13

Timers

  • Create
  • Create to fire later
  • Add to run loop

Mobile Application Development in iOS 13

init(timeInterval interval: TimeInterval, repeats: Bool, block: @escaping (Timer) -> Void) -> Timer init(fire: Date, timeInterval interval: TimeInterval, repeats: Bool, block: @escaping (Timer) -> Void) -> Timer RunLoop.current.add(timer, forMode: .default)

slide-14
SLIDE 14

Model: MyTimer Class

Mobile Application Development in iOS 14

var timer: Timer? func start() { // Start immediately timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: handleTick) } func handleTick (timer: Timer) { // todo } func stop() { timer?.invalidate() } func reset() { // todo }

slide-15
SLIDE 15

ViewController

Mobile Application Development in iOS 15

class ViewController: UIViewController, MyTimerDelegate { var myTimer = MyTimer() var initialTime: Int = 60 // todo: interface outlets and actions func timeChanged(time: Int) { // todo } func timesUp() { // todo }

  • verride func viewDidLoad() {

super.viewDidLoad() // Do any additional setup after loading the view. myTimer.delegate = self myTimer.setInitialTime(initialTime) // todo: further initializations } }

slide-16
SLIDE 16

Storyboard

Mobile Application Development in iOS 16

slide-17
SLIDE 17

ViewController

Mobile Application Development in iOS 17

// Interface outlets @IBOutlet weak var initialTimeLabel: UILabel! @IBOutlet weak var initialTimeSlider: UISlider! @IBOutlet weak var currentTimeLabel: UILabel! @IBOutlet weak var timesUpLabel: UILabel! @IBOutlet weak var startButton: UIButton! @IBOutlet weak var stopButton: UIButton! @IBOutlet weak var resetButton: UIButton!

slide-18
SLIDE 18

ViewController

Mobile Application Development in iOS 18

// Interface actions @IBAction func initialTimeSliderValueChanged(_ sender: UISlider) { // todo } @IBAction func startTapped(_ sender: UIButton) { startButton.isEnabled = false stopButton.isEnabled = true resetButton.isEnabled = false myTimer.start() } @IBAction func stopTapped(_ sender: UIButton) { // todo } @IBAction func resetTapped(_ sender: UIButton) { // todo }

slide-19
SLIDE 19

Resources

  • Grand Central Dispatch (GCD)

– developer.apple.com/documentation/dispatch

  • Timer

– developer.apple.com/documentation/foundation/timer

Mobile Application Development in iOS 19