Cocoa Touch Best Practices

Similar documents
Enhancing your apps for the next dimension of touch

Monetize and Promote Your App with iad

App Extension Best Practices

Mastering Drag and Drop

Implementing UI Designs in Interface Builder

Document Version Date: 1st March, 2015

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

ios Accessibility Developing for everyone Session 201 Ian Fisch ios Accessibility

News- ipad: ios(swift) Application

Mysteries of Auto Layout, Part 1

Leveraging Touch Input on ios

Social Pinboard: ios(swift) Application

Media and Gaming Accessibility

What s New in tvos #WWDC16. App Frameworks. Session 206. Hans Kim tvos Engineer

View Controller Advancements for ios8

Mastering UIKit on tvos

Advanced Notifications

CSE 438: Mobile Application Development Lab 2: Virtual Pet App

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

What's New in UIKit Dynamics and Visual Effects Session 229

Seamless Linking to Your App

Modern User Interaction on ios

Advanced Scrollviews and Touch Handling Techniques

Mobile Application Programing: ios. Messaging

Building Watch Apps #WWDC15. Featured. Session 108. Neil Desai watchos Engineer

Announcements. Today s Topics

What s New in Testing

Getting the Most Out of HealthKit

What s New in Notifications

Mobile Application Programming. Controls

Introducing On Demand Resources

What s New in imessage Apps

ITP 342 Mobile App Dev. Collection View

let w = UIWindow(frame: UIScreen.mainScreen().bounds)

imessage Apps and Stickers, Part 2

COMP327 Mobile Computing Session: Lecture Set 1a - Swift Introduction and the Foundation Framework Part 2

What s New in tvos 12

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

Multitasking Support on the ios Platform

Rx in the real world. 1 Rob Ciolli

CarPlay Audio and Navigation Apps

New UIKit Support for International User Interfaces

Modern Auto Layout. Building Adaptive Layouts For ios. Keith Harrison. Web: useyourloaf.com Version: 1.0 ( ) Copyright 2018 Keith Harrison

Quick Interaction Techniques for watchos

Building Apps with Dynamic Type

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

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

What s New in NSCollectionView Session 225

What s New in MapKit. App Frameworks #WWDC17. Fredrik Olsson

Improving your Existing Apps with Swift

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

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

iphone Programming Touch, Sound, and More! Norman McEntire Founder Servin Flashlight CodeTour TouchCount CodeTour

Introductory ios Development

Building Visually Rich User Experiences

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.

ITP 342 Mobile App Dev. Animation

Using and Extending the Xcode Source Editor

CSC 581: Mobile App Development Spring 2019

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Extending Your Apps with SiriKit

Building Mapping Apps for ios With Swift

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

AdFalcon ios Native Ad SDK Developer's Guide. AdFalcon Mobile Ad Network Product of Noqoush Mobile Media Group

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

High Performance Auto Layout

Miscellaneous Topics

Getting the Most out of Playgrounds in Xcode

Introducing the Contacts Framework

Chapter 22 TableView TableView. TableView ios. ViewController. Cell TableViewCell TableView

Mobile Development - Lab 2

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

Adopting Advanced Features of the New UI

Xcode 6 and ios 8 What s New for Software Developers

ios Core Data Example Application

Building the App - Part 5 - Adding a Link

Use the API or contact customer service to provide us with the following: General ios Android App Name (friendly one-word name)

Creating Complications with ClockKit Session 209

iphone Programming Patrick H. Madden SUNY Binghamton Computer Science Department

User Experience: Windows & Views

Event Delivery: The Responder Chain

Managing Documents In Your ios Apps

Accessibility on OS X

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

Introduction to WatchKit. CS193W - Spring Lecture 1

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

Touch Bar Fundamentals

Chapter 2 Welcome App

CSC 581: Mobile App Development Spring 2018

Building Faster in Xcode

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

INTRODUCTION TO ARCHITECTING YOUR IOS APP

Designing for Apple Watch

What s New in Core Image

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

ITP 342 Mobile App Dev. Connections

ITP 342 Mobile App Dev. Animation

The MVC Design Pattern

WatchKit In-Depth, Part 2

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

Transcription:

App Frameworks #WWDC15 Cocoa Touch Best Practices Session 231 Luke Hiesterman UIKit Engineer 2015 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.

Agenda

Agenda App Lifecycle

Agenda App Lifecycle Views and View Controllers

Agenda App Lifecycle Views and View Controllers Auto Layout

Agenda App Lifecycle Views and View Controllers Auto Layout Table and Collection Views

Goals

Goals Performance

Goals Performance User Experience

Goals Performance User Experience Future Proofing

App Lifecycle Best practices begin here

Launch Quickly

Launch Quickly Return quickly from applicationdidfinishlaunching

Launch Quickly Return quickly from applicationdidfinishlaunching Defer long running work

Launch Quickly Return quickly from applicationdidfinishlaunching Defer long running work App killed if too much time passes

Beyond App Launch Being responsive to every input

Beyond App Launch Being responsive to every input Not just about asynchrony

Beyond App Launch Being responsive to every input Not just about asynchrony Move long running work to background queues

Beyond App Launch Being responsive to every input func application(application: UIApplication, didfinishlaunchingwithoptions launchoptions: [NSObject: AnyObject]?) -> Bool { globaldatastructure = MyCoolDataStructure() globaldatastructure.fetchdatafromdatabase() } return true

Beyond App Launch Being responsive to every input func application(application: UIApplication, didfinishlaunchingwithoptions launchoptions: [NSObject: AnyObject]?) -> Bool { globaldatastructure = MyCoolDataStructure() globaldatastructure.fetchdatafromdatabase() } return true

Beyond App Launch Being responsive to every input func application(application: UIApplication, didfinishlaunchingwithoptions launchoptions: [NSObject: AnyObject]?) -> Bool { globaldatastructure = MyCoolDataStructure() dispatch_async(dispatch_get_main_queue()) { // defer work until later globaldatastructure.fetchdatafromdatabase() } } return true

Beyond App Launch Being responsive to every input func application(application: UIApplication, didfinishlaunchingwithoptions launchoptions: [NSObject: AnyObject]?) -> Bool { globaldatastructure = MyCoolDataStructure() let globalqueue = dispatch_get_global_queue(dispatch_queue_priority_default, 0) dispatch_async(globalqueue) { // defer work until later globaldatastructure.fetchdatafromdatabase() } } return true

Launch Quickly Again

Launch Quickly Again System Memory

Launch Quickly Again Kernel + OS Processes System Memory

Launch Quickly Again Foreground App Kernel + OS Processes System Memory

Launch Quickly Again Background App Background App Background App Background App Foreground App Kernel + OS Processes System Memory

Launch Quickly Again Background App Background App Background App Background App First to Die Foreground App Kernel + OS Processes System Memory

Launch Quickly Again Background App Background App Background App Foreground App Background App First to Die Foreground App Kernel + OS Processes System Memory

Launch Quickly Again Background App Background App Background App Foreground App Foreground App Kernel + OS Processes System Memory

Leverage Frameworks

Leverage Frameworks Reduce maintenance burden

Leverage Frameworks Reduce maintenance burden Get improvements for free

Leverage Frameworks Reduce maintenance burden Get improvements for free Focus time on what makes your app special

Leverage Frameworks Properly manage version change

Leverage Frameworks Properly manage version change Target two most recent major releases

Leverage Frameworks Properly manage version change Target two most recent major releases Include version fallbacks

Leverage Frameworks Properly manage version change Target two most recent major releases Include version fallbacks e.g., if systemversion == 9.0

Leverage Frameworks Properly manage version change Target two most recent major releases Include version fallbacks e.g., if systemversion == 9.0 e.g., if systemversion >= 9.0

Leverage Frameworks Properly manage version change Target two most recent major releases Include version fallbacks e.g., if systemversion == 9.0 e.g., if systemversion >= 9.0 e.g., if #available (ios 9.0, *) {}

Leverage Frameworks Properly manage version change Target two most recent major releases Include version fallbacks e.g., if systemversion == 9.0 e.g., if systemversion >= 9.0 e.g., if #available (ios 9.0, *) {} Have an else clause

Views and View Controllers A best practice for every screen

Layout on Modern Devices

Layout on Modern Devices

Layout to Proportions

Layout to Proportions Avoid hard-coded layout values

Layout to Proportions Avoid hard-coded layout values A UILabel to contain some content

Layout to Proportions 30 Pts 260 Pts Avoid hard-coded layout values A UILabel to contain some content

Layout to Proportions 30 Pts 260 Pts Avoid hard-coded layout values Either or both dimensions may scale A UILabel to contain some content

Layout to Proportions Avoid hard-coded layout values Either or both dimensions may scale A UILabel to contain some content Centered

Size Classes

Size Classes Some size thresholds trigger major change

Size Classes Some size thresholds trigger major change iphone 4S

Size Classes Some size thresholds trigger major change iphone 5

Size Classes Some size thresholds trigger major change iphone 6

Size Classes Some size thresholds trigger major change iphone 6 Plus

Size Classes Some size thresholds trigger major change ipad

Size Classes Some size thresholds trigger major change iphone 6 Plus

Size Classes Some size thresholds trigger major change Packaged in UITraitCollection iphone 6 Plus

Properties, Not Tags!

Properties, Not Tags! Avoid settag: and viewwithtag:

Properties, Not Tags! Avoid settag: and viewwithtag: Possible collisions with other code

Properties, Not Tags! Avoid settag: and viewwithtag: Possible collisions with other code No compiler warnings

Properties, Not Tags! Avoid settag: and viewwithtag: Possible collisions with other code No compiler warnings No runtime errors

Properties, Not Tags! Avoid settag: and viewwithtag: Possible collisions with other code No compiler warnings No runtime errors Instance variables and properties provide better alternative

Properties, Not Tags! let ImageViewTag = 1000 class ViewController: UIViewController { override func viewdidload() { super.viewdidload() } } let imageview = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50)) imageview.tag = ImageViewTag view.addsubview(imageview)

Properties, Not Tags! let ImageViewTag = 1000 class ViewController: UIViewController { override func viewdidload() { super.viewdidload() } } let imageview = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50)) imageview.tag = ImageViewTag view.addsubview(imageview)

Properties, Not Tags! class ViewController: UIViewController { var imageview : UIImageView override func viewdidload() { super.viewdidload() } } imageview = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50)) view.addsubview(imageview)

Make Timing Deterministic

Make Timing Deterministic Leverage UIViewControllerTransitionCoordinator

Make Timing Deterministic Leverage UIViewControllerTransitionCoordinator Animate alongside a transition

Make Timing Deterministic Leverage UIViewControllerTransitionCoordinator Animate alongside a transition Get accurate completion timing

Make Timing Deterministic Leverage UIViewControllerTransitionCoordinator Animate alongside a transition Get accurate completion timing Support interactive and cancelable animations

Auto Layout Layout ninjas unite

Modify Constraints Efficiently

Modify Constraints Efficiently Identify constraints that get changed, added, or removed

Modify Constraints Efficiently Identify constraints that get changed, added, or removed Unchanged constraints are optimized

Modify Constraints Efficiently Identify constraints that get changed, added, or removed Unchanged constraints are optimized Avoid removing all constraints

Modify Constraints Efficiently Identify constraints that get changed, added, or removed Unchanged constraints are optimized Avoid removing all constraints Use explicit constraint references

Modify Constraints Efficiently override func updateviewconstraints() { super.updateviewconstraints() } view.removeconstraints(view.constraints()) view.addconstraints(self.generateconstraints())

Modify Constraints Efficiently override func updateviewconstraints() { super.updateviewconstraints() } view.removeconstraints(view.constraints()) view.addconstraints(self.generateconstraints())

Modify Constraints Efficiently override func updateviewconstraints() { super.updateviewconstraints() } view.removeconstraint(imageviewhorizontalconstraint) imageviewhorizontalconstraint = self.generateimageviewhorizontalconstraint() view.addconstraint(imageviewhorizontalconstraint)

Modify Constraints Efficiently override func updateviewconstraints() { super.updateviewconstraints() } view.removeconstraint(imageviewhorizontalconstraint) imageviewhorizontalconstraint = self.generateimageviewhorizontalconstraint() view.addconstraint(imageviewhorizontalconstraint)

Modify Constraints Efficiently override func updateviewconstraints() { super.updateviewconstraints() } view.removeconstraint(imageviewhorizontalconstraint) imageviewhorizontalconstraint = self.generateimageviewhorizontalconstraint() view.addconstraint(imageviewhorizontalconstraint)

Modify Constraints Efficiently override func updateviewconstraints() { super.updateviewconstraints() } view.removeconstraint(imageviewhorizontalconstraint) imageviewhorizontalconstraint = self.generateimageviewhorizontalconstraint() view.addconstraint(imageviewhorizontalconstraint)

Constraint Specificity De-duplicating constraints

Constraint Specificity De-duplicating constraints Duplicates are implied by existing constraints

Constraint Specificity De-duplicating constraints Duplicates are implied by existing constraints Duplicates cause excess work in layout engine

Constraint Specificity De-duplicating constraints Top Bottom

Constraint Specificity De-duplicating constraints Top Bottom "V:[Top]-[Bottom]- " + NSLayoutFormatAlignAllLeft

Constraint Specificity De-duplicating constraints Top Bottom "V:[Top]-[Bottom]- " + NSLayoutFormatAlignAllLeft "H: -[Top]- "

Constraint Specificity De-duplicating constraints Top Bottom "V:[Top]-[Bottom]- " + NSLayoutFormatAlignAllLeft "H: -[Top]- " "H: -[Bottom]- "

Constraint Specificity Create flexible constraints

Constraint Specificity Create flexible constraints Avoid hard-coded values

Constraint Specificity Create flexible constraints Avoid hard-coded values A UILabel to contain some content

Constraint Specificity Create flexible constraints Avoid hard-coded values 30 Pts 260 Pts A UILabel to contain some content

Constraint Specificity Create flexible constraints Avoid hard-coded values 30 Pts 260 Pts A UILabel to contain some content V:-30-[Label(==260)]

Constraint Specificity Create flexible constraints Avoid hard-coded values 30 Pts 260 Pts Describe constraints using bounds A UILabel to contain some content

Constraint Specificity Create flexible constraints Avoid hard-coded values 30 Pts 260 Pts Describe constraints using bounds A UILabel to contain some content V:[Label(<=superview - 60)] NSLayoutFormatAlignAllCenterX

Constraint Specificity Fully specify constraints

Constraint Specificity Fully specify constraints Underspecification generates ambiguity

Constraint Specificity Fully specify constraints Underspecification generates ambiguity

Constraint Specificity Fully specify constraints Underspecification generates ambiguity Ambiguity is undefined

Constraint Specificity Fully specify constraints Underspecification generates ambiguity Ambiguity is undefined

Constraint Specificity Testing and debugging

Constraint Specificity Testing and debugging -[UIView hasambiguouslayout]

Constraint Specificity Testing and debugging -[UIView hasambiguouslayout] When called on UIWindow, returns result for entire view tree

Constraint Specificity Testing and debugging -[UIView hasambiguouslayout] When called on UIWindow, returns result for entire view tree -[UIView _autolayouttrace]

Constraint Specificity Testing and debugging -[UIView hasambiguouslayout] When called on UIWindow, returns result for entire view tree -[UIView _autolayouttrace] Both can be used for unit testing

Table and Collection Views Impress your friends

Self-Sizing Cells

Self-Sizing Cells

Self-Sizing Cells

Self-Sizing Cells

Self-Sizing Cells Fully specify constraints

Self-Sizing Cells Fully specify constraints Width = input; height = output

Self-Sizing Cells

Self-Sizing Cells

Self-Sizing Cells

Animating Height Changes

Animating Height Changes 0

Animating Height Changes

Animating Height Changes

Animating Height Changes

Animating Height Changes Steps

Animating Height Changes Steps 1. tableview.beginupdates

Animating Height Changes Steps 1. tableview.beginupdates 2. Update model

Animating Height Changes Steps 1. tableview.beginupdates 2. Update model 3. Update cell contents

Animating Height Changes Steps 1. tableview.beginupdates 2. Update model 3. Update cell contents 4. tableview.endupdates

Fast Collection View Layout Invalidation

Fast Collection View Layout Invalidation

Fast Collection View Layout Invalidation

Fast Collection View Layout Invalidation

Fast Collection View Layout Invalidation Modify only what is needed

Fast Collection View Layout Invalidation Modify only what is needed 1. Invalidate on bounds change

Fast Collection View Layout Invalidation Modify only what is needed 1. Invalidate on bounds change 2. Build targeted invalidation context

Fast Collection View Layout Invalidation Modify only what is needed 1. Invalidate on bounds change 2. Build targeted invalidation context 3. Repeat as necessary

Summary

Summary Performance

Summary Performance User experience

Summary Performance User experience Future proofing

More Information Documentation What s New in ios Start Developing ios Apps (Swift) App Programming Guide for ios http://developer.apple.com/ios Technical Support Apple Developer Forums Developer Technical Support Curt Rothert App Frameworks Evangelist rothert@apple.com