Top Banner
今だからはじめる Adaptive User Interface 2015/08/07 Yusuke Kawanabe
49

今だからはじめるAdaptive User Interface

Apr 13, 2017

Download

Technology

Yusuke Kawanabe
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 今だからはじめるAdaptive User Interface

今だからはじめる Adaptive User Interface

2015/08/07 Yusuke Kawanabe

Page 2: 今だからはじめるAdaptive User Interface

Yusuke Kawanabe @jeffsuke

Mobile App Developer at Gunosy

Blog: Jeffsuke is not a pen. http://jeffsuke.hatenablog.com/

Working on Auto Layout book

Page 3: 今だからはじめるAdaptive User Interface

今だからはじめたいな Adaptive User Interface

本日のテーマ

Page 4: 今だからはじめるAdaptive User Interface
Page 5: 今だからはじめるAdaptive User Interface

from Getting Started with Multitasking on iPad in iOS 9 https://developer.apple.com/videos/wwdc/2015/?id=205

Page 6: 今だからはじめるAdaptive User Interface

Gunosyアプリを Adaptiveにするなら

本日のテーマ

という妄想

Page 7: 今だからはじめるAdaptive User Interface

Adaptive User Interface

Page 8: 今だからはじめるAdaptive User Interface

Adaptive User Interface?

どんな画面サイズや向きでもサポートする事によってアプリの経験は格段に向上します。iOS8におけるView Controlerの進化や、XcodeにおけるAuto Layoutが、複数の状態やデバイスサイズの違いに対して、あなたのアプリをより簡単にAdapt(適応)することを可能にします。

from https://developer.apple.com/design/adaptivity/ (Yusuke Kawanabe意訳)

Page 9: 今だからはじめるAdaptive User Interface

Adaptive User Interface?

以下が出来ている状態

• 複数画面サイズ対応

• 複数画面向き対応

Page 10: 今だからはじめるAdaptive User Interface

はじめてみよう

Page 11: 今だからはじめるAdaptive User Interface

iPhone版 iPad版

Page 12: 今だからはじめるAdaptive User Interface

記事リスト 詳細

Page 13: 今だからはじめるAdaptive User Interface

UISplitViewController

• MasterView/DetailViewのレイアウトを実現できる。

• iPhoneならナビゲーション、iPadならスプリットビューを表示してくれる。

Page 14: 今だからはじめるAdaptive User Interface

UISplitViewController

Master Detail

Page 15: 今だからはじめるAdaptive User Interface

Adaptive Layout対応できた!

Page 16: 今だからはじめるAdaptive User Interface

Adaptive User Interfaceまでの道のり

• iPadマルチタスクに対応する

• Size Classを用いて複数画面サイズ、回転に対応する

• Auto Layoutを有効活用しレイアウトを生成する

Page 17: 今だからはじめるAdaptive User Interface

前提

WWDC 2015”Cocoa Touch Best Practices”にて

「最新のOS2つをサポートすると良い」

iOS8、iOS9対応と仮定

Page 18: 今だからはじめるAdaptive User Interface

マルチタスク編

Page 19: 今だからはじめるAdaptive User Interface

マルチタスク対応に必要な事

• iOS 9 SDKでビルドする

• Launch Stroyboardを使う

• 全てのオリエンテーションをサポート

Page 20: 今だからはじめるAdaptive User Interface

マルチタスク対応に必要な事

• iOS 9 SDKでビルドする

• Launch Stroyboardを使う

• 全てのオリエンテーションをサポート

Page 21: 今だからはじめるAdaptive User Interface

マルチタスク対応に必要な事

• iOS 9 SDKでビルドする

• Launch Stroyboardを使う

• 全てのオリエンテーションをサポート

Page 22: 今だからはじめるAdaptive User Interface

気になること

• 簡単にマルチタスクに対応アプリになってしまう。マルチタスキング対応の条件を満たしており、横幅がCompactのレイアウト等の従来iPadアプリを作る上で必要なかったレイアウトを考慮していないアプリはリジェクト対象になってしまう可能性があるのでは?

Page 23: 今だからはじめるAdaptive User Interface

マルチタスク編

Page 24: 今だからはじめるAdaptive User Interface

Size Class編

Page 25: 今だからはじめるAdaptive User Interface

Size Class

if UIScreen.mainScreen().bounds.width > 320.0 { // Do something }

Before iOS8

Page 26: 今だからはじめるAdaptive User Interface

Size Class

enum UIUserInterfaceSizeClass: Int { case Unspecified case Compact case Regular }

After iOS8

Page 27: 今だからはじめるAdaptive User Interface

Size Class for iPhone

Size Class for iPhone 6 Plus

Page 28: 今だからはじめるAdaptive User Interface

Size Class for iPad

Page 29: 今だからはじめるAdaptive User Interface

UITraitCollection

horizontalSizeClass Compact

verticalSizeClass Regular

userfaceIdiom Phone

displayScale 2.0

Page 30: 今だからはじめるAdaptive User Interface

UIScreen

UIWindow

UIViewController

UIView

UITraitCollection

Page 31: 今だからはじめるAdaptive User Interface

UIScreen

UIWindow

UIViewController

UIView

マルチタスキング時

horizontalSizeClass Compact

verticalSizeClass Regular

UIScreen.mainScreen().boundsUIWindow().bounds

Page 32: 今だからはじめるAdaptive User Interface

UIScreenとUIWindow

(0.0)UIScreen

UIWindow(0.0)

First app

Second app

Page 33: 今だからはじめるAdaptive User Interface

iOS8以前の回転動作

func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval)

func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation)

func willAnimateRotationToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval)

UIViewController

Page 34: 今だからはじめるAdaptive User Interface

iOS8以前の回転動作

func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval)

func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation)

func willAnimateRotationToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval)

UIViewController

端末が縦向きだとか横向きだとか いう時代は終わった!

Page 35: 今だからはじめるAdaptive User Interface

iOS8以降の回転動作

func willTransitionToTraitCollection(newCollection: UITraitCollection, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)

UITraitEnvironment

UIContentContainer

func traitCollectionDidChange(previousTraitCollection: UITraitCollection?)

Page 36: 今だからはじめるAdaptive User Interface

回転とSize ClassSetup

Create Animation

Run Animation

Cleanup

willTransitionToTraitCollectionviewWillTransitionToSizetraitCollectionDidChange

Page 37: 今だからはじめるAdaptive User Interface

回転とSize ClassSetup

Create Animation

Run Animation

Cleanup

willTransitionToTraitCollectionviewWillTransitionToSizetraitCollectionDidChange

Page 38: 今だからはじめるAdaptive User Interface

マルチタスクとSize Class

First App

Page 39: 今だからはじめるAdaptive User Interface

マルチタスクとSize ClassCompact

Regular

Page 40: 今だからはじめるAdaptive User Interface

マルチタスクとSize ClassCompact

Regular

willTransitionToTraitCollection traitCollectionDidChange は呼ばれない

Page 41: 今だからはじめるAdaptive User Interface

マルチタスクとSize ClassRegular

Regular

Page 42: 今だからはじめるAdaptive User Interface

Size Class編まとめ

• UITraitCollectionを知る

• 回転挙動を把握する

• Split Viewの挙動を把握する

Page 43: 今だからはじめるAdaptive User Interface

Auto Layout編

Page 44: 今だからはじめるAdaptive User Interface

Auto Layout

iOS8以上対応なので出来る事が増える

• Self sizing Cell

• Perfomance Concern

Page 45: 今だからはじめるAdaptive User Interface

Self Sizing Cell

iOS7以前

• セルのインスタンスを生成し、オフスクリーンでレイアウト実行しセルの高さを取得していた。

iOS8以降

• セルが表示される直前に、セルの高さを自動で計算。

Page 46: 今だからはじめるAdaptive User Interface

Self Sizing Cell

tableView.estimatedRowHeight = 120.0 tableView.rowHeight = UITableViewAutomaticDimension

Page 47: 今だからはじめるAdaptive User Interface

Perfomanceの向上

let constraints = [ NSLayoutConstraint.constraintsWithVisualFormat("|[view]|", options: nil, metrics: nil, views: views) ]

NSLayoutConstraint.activateConstraints(constraints)

Auto LayoutエンジンはConstraintsが追加削除されると都度レイアウトの計算を実行する。

Constraintsの追加削除ではなく、Constraintsの有効化を使う。

Page 48: 今だからはじめるAdaptive User Interface

Auto Layout編

Page 49: 今だからはじめるAdaptive User Interface

まとめ

• Adaptiveとは状態に依存しないレイアウト

• その為に、回転とマルチタスクを考慮

• Size ClassとAuto Layoutを有効活用し困難を乗り切る