SlideShare a Scribd company logo
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周年おめでとう	

ございます

More Related Content

Similar to 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
 

Similar to 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
 

Recently uploaded

Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Peter Udo Diehl
 

Recently uploaded (20)

From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 

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の追加時に一瞬アニメーションが止まる • ときおり動かなくなる・・・