Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SwiftでUIKitDynamics

 SwiftでUIKitDynamics

Shinji Kobayashi

December 18, 2014
Tweet

More Decks by Shinji Kobayashi

Other Decks in Programming

Transcript

  1. ը໘ભҠ 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Λ࣮૷
  2. ը໘ભҠ 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? Πϯελϯεͷ஗Ԇੜ੒ͱॳظԽ͸ศརʁ
  3. ը໘ભҠ 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ΛݺͿ
  4. 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͕࢖͑ͳ͍ͷ͸ͪΐͬͱෆศʁ
  5. 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ͷ௥Ճ࣌ʹҰॠΞχϝʔγϣϯ͕ࢭ·Δ • ͱ͖͓Γಈ͔ͳ͘ͳΔɾɾɾ