Lecture 8 Demo Code: Cassini Multithreading

Similar documents
View Controller Lifecycle

Stanford CS193p. Developing Applications for ios. Spring CS193p. Spring 2016

Stanford CS193p. Developing Applications for ios. Winter CS193p. Winter 2017

Developing Applications for ios

Building the App - Part 5 - Adding a Link

App. Chapter 19 App. App (ViewController) App. Single View Application Single View Application View. (View Controller)

Stanford CS193p. Developing Applications for ios. Fall Stanford CS193p. Fall 2011

UI Design and Storyboarding

Introductory ios Development

Tables. Mobile Application Development in ios School of EECS Washington State University Instructor: Larry Holder

Stanford CS193p. Developing Applications for ios. Fall CS193p. Fall

Announcements. Today s Topics

Assignment IV: Smashtag Mentions

Mastering UIKit on tvos

Implementing UI Designs in Interface Builder

News- ipad: ios(swift) Application

ios Mobile Development

ios Application Development Lecture 5: Protocols, Extensions,TabBar an Scroll Views

CS193P - Lecture 8. iphone Application Development. Scroll Views & Table Views

Stanford CS193p. Developing Applications for ios. Winter CS193p! Winter 2015

Lecture 5 Demo Code: FaceIt MVC and Gestures

Assignment III: Graphing Calculator

Stanford CS193p. Developing Applications for ios. Spring CS193p. Spring 2016

Navigation and Segues

ITP 342 Mobile App Dev. Collection View

View Concepts. iphone Application Programming Lecture 4: User Interface Design. SDK provide many types of Views to show your content

Stanford CS193p. Developing Applications for ios. Fall CS193p. Fall

ios Mobile Development

ios Memory Deep Dive #WWDC18 Kyle Howarth, Software Engineer James Snee, Software Engineer Kris Markel, Software Engineer

Stanford CS193p. Developing Applications for ios. Spring Stanford CS193p. Spring 2012

View Controllers CPRE 388

Stanford CS193p. Developing Applications for ios. Fall CS193p. Fall

Intro to Development for ios. Dave Koziol Arbormoon Software, Inc.

OVERVIEW. Why learn ios programming? Share first-hand experience. Identify platform differences. Identify similarities with.net

Document Version Date: 1st March, 2015

Stream iphone Sensor Data to Adafruit IO

Stanford CS193p. Developing Applications for ios. Winter CS193p. Winter 2017

Today s Topics. Scroll views Table views. UITableViewController Table view cells. Displaying data Controlling appearance & behavior

Mobile Development - Lab 2

Assignment III: Graphing Calculator

Rx in the real world. 1 Rob Ciolli

Stanford CS193p. Developing Applications for ios. Fall CS193p. Fall

Advanced ios. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 20 11/01/2012

Stanford CS193p. Developing Applications for ios. Fall CS193p. Fall

MVC & Onwards. CS 442: Mobile App Development Michael Saelee

Stanford CS193p. Developing Applications for iphone 4, ipod Touch, & ipad Fall Stanford CS193p Fall 2010

Stanford CS193p. Developing Applications for ios. Fall CS193p. Fall

Social Pinboard: ios(swift) Application

LESSONS LEARNED. SWIFT. Dagna Bieda, 7th April 2016

User Interfaces. Lecture 15. Application Programming on Mac OS. Hamza Bennani September 4, 2018

Stanford CS193p. Developing Applications for ios. Winter CS193p! Winter 2015

Assignment III: Graphing Calculator

Stanford CS193p. Developing Applications for iphone 4, ipod Touch, & ipad Fall Stanford CS193p Fall 2010

Structuring an App Copyright 2013 Apple Inc. All Rights Reserved.

Stanford CS193p. Developing Applications for ios. Winter CS193p! Winter 2015

lecture 10 UI/UX and Programmatic Design cs : spring 2018

Collection Views Hands-On Challenges

CSC 581: Mobile App Development Spring 2018

Event Delivery: The Responder Chain

Building GUIs with UIKit. Kevin Cathey

Mastering Drag and Drop

Stanford CS193p. Developing Applications for ios. Winter CS193p. Winter 2017

ITP 342 Mobile App Dev. Connections

Mobile Application Programing: ios. Messaging

This book contains code samples available under the MIT License, printed below:

Building Mapping Apps for ios With Swift

Stanford CS193p. Developing Applications for ios. Fall Stanford CS193p. Fall 2011

Cocoa Touch Best Practices

Multimedia. Mobile Application Development in ios School of EECS Washington State University Instructor: Larry Holder

iphone Application Programming Lab 2: MVC and Delegation + A01 discussion

User Experience: Windows & Views

COMPLETE TUTORIAL COURSE. Learn to make tvos LE. apps with real-worldam S F

A Mad Libs app that you will navigate through 3 UIViewControllers to add text that will be shown in a story on the fourth UIViewController.

Advanced Notifications

Learn to make desktop LE

Enhancing your apps for the next dimension of touch

Designing iphone Applications

epicurious Anatomy of an ios app Robert Tolar Haining June 25, 2010 ConvergeSE

Data Delivery with Drag and Drop

ADVANCED M A. Learn SiriKit, imessage apps, rich notifications, and more. with real-world projects HACKING WITH SWIFT COMPLETE TUTORIAL COURSE

Intro to Native ios Development. Dave Koziol Arbormoon Software, Inc.

Widget Tour. iphone and ipod touch Development Fall 2009 Lecture 7

Collection Views. Dr. Sarah Abraham

ios Application Development Lecture 3: Unit 2

ITP 342 Mobile App Dev. Web View

Assignment III: Graphing Calculator

Settings. Mobile Application Development in ios School of EECS Washington State University Instructor: Larry Holder

Stanford CS193p. Developing Applications for ios Fall Stanford CS193p. Fall 2013

Mobile Application Programming. Swift Classes

ios Development - Xcode IDE

Stanford CS193p. Developing Applications for ios. Winter CS193p! Winter 2015

HACKING WITH SWIFT. Practical. ios 10 COMPLETE TUTORIAL COURSE. Learn to develop apps. for ios 10 by building MP. real-world projects E S

1 Build Your First App. The way to get started is to quit talking and begin doing. Walt Disney

ios Application Development Lecture 2: Seminar and Unit 1

ios Tic Tac Toe Game John Robinson at Rowan University

LEARNING ios APP DEVELOPMENT With Swift 3 & ios 10

Objective-C Primer. iphone Programmer s Association. Lorenzo Swank September 10, 2008

ios Application Development Hello World App Rubric

} override func didreceivememorywarning() { 26 super.didreceivememorywarning() 27 } 28 } Pause Stop

View Concepts. iphone Application Programming Lecture 4: User Interface Design. SDK provide many types of Views to show your content

Transcription:

Lecture 8 Demo Code: Cassini Multithreading Objective Included below is the source code for the demo in lecture. It is provided under the same Creative Commons licensing as the rest of CS193p s course materials. The code which has not changed since the previous lecture is included but is grayed out. And here is the complete project. ImageViewController.swift Cassini Created by CS193p Instructor. Copyright 2017 Stanford University. All rights reserved. import UIKit class ImageViewController: UIViewController MARK: Model var imageurl: URL? didset image = nil if view.window!= nil if we're on screen fetchimage() then fetch image PAGE 1 OF 5

MARK: Private Implementation when we dropped this spinner into our storyboard it initially put it as a subview of our scrollview! we used the Document Outline to drag it out to be a sibling of scrollview instead of a subview @IBOutlet weak var spinner: UIActivityIndicatorView! private func fetchimage() if let url = imageurl we're going to start something on another queue soon so let's start a spinner going in the UI to let the user know we'll stop this any time an image actually gets set (we'd never want the spinner and an image appearing at the same time!) spinner.startanimating() try? Data(contentsOf:) blocks the thread it is on we are currently on the main thread so we must dispatch that call off to a background queue we'll use one of the shared, global, concurrent background queues DispatchQueue.global(qos:.userInitiated).async [weak self] in let urlcontents = try? Data(contentsOf: url) now that we're back from blocking are we still even interested in this url (i.e. does it == self.imageurl)? if let imagedata = urlcontents, url == self?.imageurl now we want to set the image in the UI but we are not on the main thread right now so we are not allowed to do UI no problem: just dispatch the UI stuff back to the main queue DispatchQueue.main.async self?.image = UIImage(data: imagedata) MARK: View Controller Lifecycle override func viewwillappear(_ animated: Bool) super.viewwillappear(animated) if image == nil we're about to appear on screen so, if needed, fetchimage() fetch image PAGE 2 OF 5

MARK: User Interface @IBOutlet weak var scrollview: UIScrollView! didset to zoom we have to handle viewforzooming(in scrollview:) scrollview.delegate = self and we must set our minimum and maximum zoom scale scrollview.minimumzoomscale = 0.03 scrollview.maximumzoomscale = 1.0 most important thing to set in UIScrollView is contentsize scrollview.contentsize = imageview.frame.size scrollview.addsubview(imageview) fileprivate var imageview = UIImageView() private var image: UIImage? get return imageview.image set imageview.image = newvalue imageview.sizetofit() careful here because scrollview might be nil (for example, if we're setting our image as part of a prepare) so use optional chaining to do nothing if our scrollview outlet has not yet been set scrollview?.contentsize = imageview.frame.size now that we've set an image stop any spinner that exists from spinning spinner?.stopanimating() MARK: UIScrollViewDelegate Extension which makes ImageViewController conform to UIScrollViewDelegate Handles viewforzooming(in scrollview:) by returning the UIImageView as the view to transform when zooming extension ImageViewController : UIScrollViewDelegate func viewforzooming(in scrollview: UIScrollView) -> UIView? return imageview PAGE 3 OF 5

CassiniViewController.swift Cassini Created by CS193p Instructor. Copyright 2017 Stanford University. All rights reserved. import UIKit class CassiniViewController: UIViewController, UISplitViewControllerDelegate for the UISplitViewControllerDelegate method below to work we have to set ourself as the UISplitViewController's delegate (only we can be that because ImageViewControllers come and goes from the heap) we could probably get away with doing this as late as viewdidload but it's a bit safer to do it as early as possible and this is as early as possible (we just came out of the storyboard and "awoke" so we know we are in our UISplitViewController by now) override func awakefromnib() super.awakefromnib() self.splitviewcontroller?.delegate = self MARK: - Navigation we interpret our segue identifier as a key into the DemoURL.NASA Dictionary (a [String:URL]) if we find a URL for that key then we just set the public imageurl of the ImageViewController to it override func prepare(for segue: UIStoryboardSegue, sender: Any?) if let url = DemoURL.NASA[segue.identifier?? ""] if let imagevc = (segue.destination.contents as? ImageViewController) imagevc.imageurl = url imagevc.title = (sender as? UIButton)?.currentTitle we "fake out" ios here this delegate method of UISplitViewController allows the delegate to do the work of collapsing the primary view controller (the master) on top of the secondary view controller (the detail) this happens whenever the split view wants to show the detail but the master is on screen in a spot that would be covered up by the detail the return value of this delegate method is a Bool "true" means "yes, Mr. UISplitViewController, I did collapse that for you" "false" means "sorry, Mr. UISplitViewController, I couldn't collapse so you do it for me" if our secondary (detail) is an ImageViewController with a nil imageurl then we will return true even though we're not actually going to do anything that's because when imageurl is nil, we do NOT want the detail to collapse on top of the master func splitviewcontroller( _ splitviewcontroller: UISplitViewController, collapsesecondary secondaryviewcontroller: UIViewController, onto primaryviewcontroller: UIViewController ) -> Bool if primaryviewcontroller.contents == self if let ivc = secondaryviewcontroller.contents as? ImageViewController, ivc.imageurl == nil return true return false extension UIViewController this var returns the "contents" of this UIViewController which, if this UIViewController is a UINavigationController means "the UIViewController contained in me (and visible)" otherwise, it just means the UIViewController itself could easily imagine extending this for UITabBarController too var contents: UIViewController if let navcon = self as? UINavigationController return navcon.visibleviewcontroller?? self else return self PAGE 4 OF 5

DemoURL.swift Created by CS193p Instructor. Copyright (c) 2017 Stanford University. All rights reserved. import Foundation struct DemoURL static let stanford = URL(string: "http:stanford.edu/about/images/intro_about.jpg") static var NASA: Dictionary<String,URL> = let NASAURLStrings = [ "Cassini" : "http:www.jpl.nasa.gov/images/cassini/20090202/pia03883-full.jpg", "Earth" : "http:www.nasa.gov/sites/default/files/wave_earth_mosaic_3.jpg", "Saturn" : "http:www.nasa.gov/sites/default/files/saturn_collage.jpg" ] var urls = Dictionary<String,URL>() for (key, value) in NASAURLStrings urls[key] = URL(string: value) return urls () PAGE 5 OF 5