SlideShare ist ein Scribd-Unternehmen logo
1 von 13
Downloaden Sie, um offline zu lesen
SwiftでUIKitDynamics
Shinji Kobayashi	

GentleSoft
もくもくiOS 1周年LT会@Retty
Name:Shinji Kobayashi
Twitter:@gentlejkov
GentleSoft
Who are you?
http://gentlesoft.net
https://github.com/gentlesoft/UIKitDynamicsTest
• 画面遷移	

• UIViewを動かす	

• UICollectionViewで多数のオブ
ジェクト制御
UIDynamicsの使い道
• 画面遷移	

• UIViewを動かす	

• UICollectionViewで多数のオブ
ジェクト制御
UIDynamicsの使い道
画面遷移
override func viewDidLoad() {
super.viewDidLoad()
!
self.transitioningDelegate = self
}
!
// MARK: - UIViewControllerAnimatedTransitioning
func animationControllerForPresentedController(presented: UIViewController,
presentingController presenting: UIViewController, sourceController source:
UIViewController) -> UIViewControllerAnimatedTransitioning? {
return DropTransition()
}
func animationControllerForDismissedController(dismissed: UIViewController) ->
UIViewControllerAnimatedTransitioning? {
return DropTransition()
}
UIViewControllerTransitioningDelegateを実装
画面遷移
class DropTransition: NSObject, UIViewControllerAnimatedTransitioning,
UIDynamicAnimatorDelegate {
private var transitionContext : UIViewControllerContextTransitioning?
private lazy var gravity : UIGravityBehavior = {
let gravity = UIGravityBehavior()
gravity.gravityDirection = CGVector(dx: 0.5, dy: 1)
return gravity
}()
private let collision = UICollisionBehavior()
private lazy var dynamicItem : UIDynamicItemBehavior = {
let dynamicItem = UIDynamicItemBehavior()
dynamicItem.elasticity = 0.5
dynamicItem.resistance = 0.8
return dynamicItem
}()
private var animator : UIDynamicAnimator?
インスタンスの遅延生成と初期化は便利?
画面遷移
func transitionDuration(transitionContext:
UIViewControllerContextTransitioning) -> NSTimeInterval {
return 0
}
func animateTransition(transitionContext:
UIViewControllerContextTransitioning) {
self.transitionContext = transitionContext
let from =
transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey
)!.view
let to =
transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
.view
・・・アニメーションをセット
}
override func animationDidStop(anim: CAAnimation!, finished flag: Bool) {
self.animator = nil
}
// MARK: - UIDynamicAnimatorDelegate
func dynamicAnimatorDidPause(animator: UIDynamicAnimator) {
self.transitionContext?.completeTransition(true)
}
アニメーション終了でcompleteTransitionを呼ぶ
• 画面遷移	

• UIViewを動かす	

• UICollectionViewで多数のオブ
ジェクト制御
UIDynamicsの使い道
UIViewを動かす
// MARK: Gesture
func handleGesture(recognizer: UIPanGestureRecognizer) {
switch recognizer.state {
case UIGestureRecognizerState.Began:
self.tapPos = self.attachment.anchorPoint
case UIGestureRecognizerState.Changed:
let pos = recognizer.translationInView(self.malletView)
self.attachment.anchorPoint = CGPoint(x: self.tapPos.x + pos.x,
y: self.tapPos.y + pos.y)
default:
break
}
}
• UIViewのタッチ判定はGestureRecognizerが使える	

• 指定ポイントへの移動はUIAttatchmentBehavior	

• Staticが使えないのはちょっと不便?
• 画面遷移	

• UIViewを動かす	

• UICollectionViewで多数のオブ
ジェクト制御
UIDynamicsの使い道
UICollectionView + UIKitDynamics
class SandboxLayout: UICollectionViewLayout, UIDynamicAnimatorDelegate {
!
・・・
!
override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) ->
UICollectionViewLayoutAttributes! {
var attr = self.animator.layoutAttributesForCellAtIndexPath(indexPath)
if attr == nil {
attr = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
attr.frame = self.sandBoxDelegate.initSand(indexPath)
}
return attr
}
• Swiftっぽくない実装を強いられる	

• animatorへのItemの追加時に一瞬アニメーションが止まる	

• ときおり動かなくなる・・・
1周年おめでとう	

ございます

Weitere ähnliche Inhalte

Ähnlich wie SwiftでUIKitDynamics

Building html5 apps using Cordova
Building html5 apps using Cordova Building html5 apps using Cordova
Building html5 apps using Cordova
David Voyles
 
CocoaHeads Rennes #4 : la rotation sur iOS
CocoaHeads Rennes #4 : la rotation sur iOSCocoaHeads Rennes #4 : la rotation sur iOS
CocoaHeads Rennes #4 : la rotation sur iOS
CocoaHeadsRNS
 
ios_summit_2016_korhan
ios_summit_2016_korhanios_summit_2016_korhan
ios_summit_2016_korhan
Korhan Bircan
 
Heat on Wed.(ヒートオンウェンズディ)! Vol.1
Heat on Wed.(ヒートオンウェンズディ)! Vol.1Heat on Wed.(ヒートオンウェンズディ)! Vol.1
Heat on Wed.(ヒートオンウェンズディ)! Vol.1
Noriyuki Nonomura
 
Quick Start to iOS Development
Quick Start to iOS DevelopmentQuick Start to iOS Development
Quick Start to iOS Development
Jussi Pohjolainen
 

Ähnlich wie SwiftでUIKitDynamics (20)

Leaving Interface Builder Behind
Leaving Interface Builder BehindLeaving Interface Builder Behind
Leaving Interface Builder Behind
 
Objective c design pattens-architetcure
Objective c design pattens-architetcureObjective c design pattens-architetcure
Objective c design pattens-architetcure
 
Swf2 ui
Swf2 uiSwf2 ui
Swf2 ui
 
スケーラブル SwiftUI プロジェクトにおける実用的な TCA モジュラー化
スケーラブル SwiftUI プロジェクトにおける実用的な TCA モジュラー化スケーラブル SwiftUI プロジェクトにおける実用的な TCA モジュラー化
スケーラブル SwiftUI プロジェクトにおける実用的な TCA モジュラー化
 
ViewController Transitions - Swift Paris Junior #3
ViewController Transitions - Swift Paris Junior #3ViewController Transitions - Swift Paris Junior #3
ViewController Transitions - Swift Paris Junior #3
 
iOS_Presentation
iOS_PresentationiOS_Presentation
iOS_Presentation
 
Building html5 apps using Cordova
Building html5 apps using Cordova Building html5 apps using Cordova
Building html5 apps using Cordova
 
How To Build iOS Apps Without interface Builder
How To Build iOS Apps Without interface BuilderHow To Build iOS Apps Without interface Builder
How To Build iOS Apps Without interface Builder
 
CocoaHeads Rennes #4 : la rotation sur iOS
CocoaHeads Rennes #4 : la rotation sur iOSCocoaHeads Rennes #4 : la rotation sur iOS
CocoaHeads Rennes #4 : la rotation sur iOS
 
FrenchKit: SwiftUI Data Flow with Redux
FrenchKit: SwiftUI Data Flow with ReduxFrenchKit: SwiftUI Data Flow with Redux
FrenchKit: SwiftUI Data Flow with Redux
 
IOS APPs Revision
IOS APPs RevisionIOS APPs Revision
IOS APPs Revision
 
ios_summit_2016_korhan
ios_summit_2016_korhanios_summit_2016_korhan
ios_summit_2016_korhan
 
iOSインタラクションデザイン
iOSインタラクションデザインiOSインタラクションデザイン
iOSインタラクションデザイン
 
Taking control of Storyboard
Taking control of StoryboardTaking control of Storyboard
Taking control of Storyboard
 
Creating Container View Controllers
Creating Container View ControllersCreating Container View Controllers
Creating Container View Controllers
 
TechGarage Hackaton
TechGarage HackatonTechGarage Hackaton
TechGarage Hackaton
 
Heat on Wed.(ヒートオンウェンズディ)! Vol.1
Heat on Wed.(ヒートオンウェンズディ)! Vol.1Heat on Wed.(ヒートオンウェンズディ)! Vol.1
Heat on Wed.(ヒートオンウェンズディ)! Vol.1
 
004
004004
004
 
I os 11
I os 11I os 11
I os 11
 
Quick Start to iOS Development
Quick Start to iOS DevelopmentQuick Start to iOS Development
Quick Start to iOS Development
 

Kürzlich hochgeladen

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Kürzlich hochgeladen (20)

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 

SwiftでUIKitDynamics

  • 4. • 画面遷移 • UIViewを動かす • UICollectionViewで多数のオブ ジェクト制御 UIDynamicsの使い道
  • 5. • 画面遷移 • UIViewを動かす • UICollectionViewで多数のオブ ジェクト制御 UIDynamicsの使い道
  • 6. 画面遷移 override func viewDidLoad() { super.viewDidLoad() ! self.transitioningDelegate = self } ! // MARK: - UIViewControllerAnimatedTransitioning func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? { return DropTransition() } func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { return DropTransition() } UIViewControllerTransitioningDelegateを実装
  • 7. 画面遷移 class DropTransition: NSObject, UIViewControllerAnimatedTransitioning, UIDynamicAnimatorDelegate { private var transitionContext : UIViewControllerContextTransitioning? private lazy var gravity : UIGravityBehavior = { let gravity = UIGravityBehavior() gravity.gravityDirection = CGVector(dx: 0.5, dy: 1) return gravity }() private let collision = UICollisionBehavior() private lazy var dynamicItem : UIDynamicItemBehavior = { let dynamicItem = UIDynamicItemBehavior() dynamicItem.elasticity = 0.5 dynamicItem.resistance = 0.8 return dynamicItem }() private var animator : UIDynamicAnimator? インスタンスの遅延生成と初期化は便利?
  • 8. 画面遷移 func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval { return 0 } func animateTransition(transitionContext: UIViewControllerContextTransitioning) { self.transitionContext = transitionContext let from = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey )!.view let to = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)! .view ・・・アニメーションをセット } override func animationDidStop(anim: CAAnimation!, finished flag: Bool) { self.animator = nil } // MARK: - UIDynamicAnimatorDelegate func dynamicAnimatorDidPause(animator: UIDynamicAnimator) { self.transitionContext?.completeTransition(true) } アニメーション終了でcompleteTransitionを呼ぶ
  • 9. • 画面遷移 • UIViewを動かす • UICollectionViewで多数のオブ ジェクト制御 UIDynamicsの使い道
  • 10. UIViewを動かす // MARK: Gesture func handleGesture(recognizer: UIPanGestureRecognizer) { switch recognizer.state { case UIGestureRecognizerState.Began: self.tapPos = self.attachment.anchorPoint case UIGestureRecognizerState.Changed: let pos = recognizer.translationInView(self.malletView) self.attachment.anchorPoint = CGPoint(x: self.tapPos.x + pos.x, y: self.tapPos.y + pos.y) default: break } } • UIViewのタッチ判定はGestureRecognizerが使える • 指定ポイントへの移動はUIAttatchmentBehavior • Staticが使えないのはちょっと不便?
  • 11. • 画面遷移 • UIViewを動かす • UICollectionViewで多数のオブ ジェクト制御 UIDynamicsの使い道
  • 12. UICollectionView + UIKitDynamics class SandboxLayout: UICollectionViewLayout, UIDynamicAnimatorDelegate { ! ・・・ ! override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes! { var attr = self.animator.layoutAttributesForCellAtIndexPath(indexPath) if attr == nil { attr = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath) attr.frame = self.sandBoxDelegate.initSand(indexPath) } return attr } • Swiftっぽくない実装を強いられる • animatorへのItemの追加時に一瞬アニメーションが止まる • ときおり動かなくなる・・・