24
デリゲートを理解して 脱初心者! 株式会社トルクス 山田 2015/07/15 iOSアプリ勉強会

デリゲートを理解して脱初心者! iOSアプリ勉強会

Embed Size (px)

Citation preview

Page 1: デリゲートを理解して脱初心者! iOSアプリ勉強会

デリゲートを理解して 脱初心者!

株式会社トルクス 山田 2015/07/15 iOSアプリ勉強会

Page 2: デリゲートを理解して脱初心者! iOSアプリ勉強会

Torques Inc. 2015

自己紹介:山田 宏道• 株式会社トルクス 代表

Page 3: デリゲートを理解して脱初心者! iOSアプリ勉強会

Torques Inc. 2015

VRコンテンツ• 「MotherBird」ニコニコ超会議(オッキューランド)

Page 4: デリゲートを理解して脱初心者! iOSアプリ勉強会

Torques Inc. 2015

ときどき、講師

Page 5: デリゲートを理解して脱初心者! iOSアプリ勉強会

Torques Inc. 2015

今日は、デリゲート• iOSアプリ開発でつまづくポイント

Page 6: デリゲートを理解して脱初心者! iOSアプリ勉強会

Torques Inc. 2015

開発で重要な4要素• Tools(Xcode, Android Studio, Eclipse) • Language(Swift, Obj-C, Java, PHP, ..) • Libraries, Frameworks(iOS SDK, etc) • Algorithms, Design Patterns(←ココ!)

Page 7: デリゲートを理解して脱初心者! iOSアプリ勉強会

Torques Inc. 2015

デザインパターンとは<クラスのつくりかたの事例集> • GoF(The Gang of Four)が有名だが、全部知っておく必要はない

• Delegation Patternというやり方があり、ある仕組みを作るのに便利、となっている。

Page 8: デリゲートを理解して脱初心者! iOSアプリ勉強会

Torques Inc. 2015

クラス、メソッドの復習

Page 9: デリゲートを理解して脱初心者! iOSアプリ勉強会

Torques Inc. 2015

クラス、メソッドの復習class Human { var name: String = "Default" var stamina: Int = 100 var pos: CGPoint = CGPoint(x: 0, y: 0) init( name: String ) { self.name = name } func sayHello() { println("My name is \(name)! @ (\(pos.x),\(pos.y))“) } }

// -----

var me = Human(name:"Yamada") me.sayHello()

Page 10: デリゲートを理解して脱初心者! iOSアプリ勉強会

Torques Inc. 2015

SwiftのProtocol

「プロトコルに準拠している」 =「必要な条件を満たしている」

=「必要なメソッドを実装している」

Page 11: デリゲートを理解して脱初心者! iOSアプリ勉強会

Torques Inc. 2015

SwiftのProtocolprotocol PlayerProtocol { func runTo( target:CGPoint ) }

class Player: Human, PlayerProtocol { }

Page 12: デリゲートを理解して脱初心者! iOSアプリ勉強会

Torques Inc. 2015

SwiftのProtocolprotocol PlayerProtocol { func runTo( target:CGPoint ) }

class Player: Human, PlayerProtocol { }

<実装デモ>

Page 13: デリゲートを理解して脱初心者! iOSアプリ勉強会

Torques Inc. 2015

SwiftのProtocolprotocol PlayerProtocol { func runTo( target:CGPoint ) }

class Player: Human, PlayerProtocol { func runTo( target:CGPoint ) { println("ε=ε=ε=┌(;*´Д`)ノ") } }

Page 14: デリゲートを理解して脱初心者! iOSアプリ勉強会

Torques Inc. 2015

SwiftのProtocolvar players = [Player(name: "P0"),Player(name: "P1"),Player(name: "P2"),Human(name: "Hiro")]

var ball = CGPoint(x: 10, y: 10)

for p in players { p.sayHello() if let pr = p as? PlayerProtocol { pr.runTo(ball) } }

Page 15: デリゲートを理解して脱初心者! iOSアプリ勉強会

Torques Inc. 2015

SwiftのProtocol

「プロトコルに準拠している」 =「必要な条件を満たしている」

=「必要なメソッドを実装している」

Page 16: デリゲートを理解して脱初心者! iOSアプリ勉強会

Torques Inc. 2015

delegate

「任せられる人に、任せる」 =「必要な実装をしているオブジェクトに、任せる」 =「プロトコルに準拠しているオブジェクトに、任せる」

Page 17: デリゲートを理解して脱初心者! iOSアプリ勉強会

Torques Inc. 2015

パルプンテDelegateprotocol ParupunteProtocol { func performParupunte() }

class GameSystem { }

class Hero : Human { func sayParupunte() { } }

// ----- var game = GameSystem() var hero = Hero(name: "ああああ") hero.sayParupunte()

Page 18: デリゲートを理解して脱初心者! iOSアプリ勉強会

Torques Inc. 2015

パルプンテDelegateprotocol ParupunteProtocol { func performParupunte() }

class GameSystem: ParupunteProtocol { func performParupunte(){ println("パルプンテ!") } }

Page 19: デリゲートを理解して脱初心者! iOSアプリ勉強会

Torques Inc. 2015

パルプンテDelegateclass Hero : Human { var parupunteDelegate: ParupunteProtocol? func sayParupunte() { if( parupunteDelegate != nil ){ parupunteDelegate.performParupunte() } } }

// ----- var game = GameSystem() var hero = Hero(name: “ああああ") hero.parupunteDelegate = game hero.sayParupunte()

Page 20: デリゲートを理解して脱初心者! iOSアプリ勉強会

Torques Inc. 2015

パルプンテDelegate:全体protocol ParupunteProtocol { func performParupunte() }

class GameSystem: ParupunteProtocol { func performParupunte(){ println("パルプンテ!") } }

class Hero : Human { var parupunteDelegate: ParupunteProtocol? func sayParupunte() { if( parupunteDelegate != nil ){ parupunteDelegate.performParupunte() } } }

// ----- var game = GameSystem() var hero = Hero(name: “ああああ") hero.parupunteDelegate = game hero.sayParupunte()

Page 21: デリゲートを理解して脱初心者! iOSアプリ勉強会

Torques Inc. 2015

AvatarMaker• 実践的な問題をご用意しました! • github.com/HiromichiYamada/AvatarMakerProblem

• ↑”AvatarMaker”で検索してみてください

Page 22: デリゲートを理解して脱初心者! iOSアプリ勉強会

Torques Inc. 2015

AvatarMaker• MainViewController • WearPickerViewController

Page 23: デリゲートを理解して脱初心者! iOSアプリ勉強会

Torques Inc. 2015

AvatarMaker• 実装例:デモ

Page 24: デリゲートを理解して脱初心者! iOSアプリ勉強会

Torques Inc. 2015

ありがとうございました• facebook hiromichi.yamada • 他に、詳しく知りたい技術とかあったらアンケート等にお願いします。