Skip to content

Instantly share code, notes, and snippets.

View andresbrun's full-sized avatar

Andrés Brun andresbrun

View GitHub Profile
modalViewExitBehaviour = UIDynamicItemBehavior(items: [targetView])
// Action is called in every animation step
modalViewExitBehaviour.action = { [unowned self] _ in
// Check if all views are outside of the reference frame
if !self.allViews.contains(where: { $0.frame.intersects(self.superview.bounds) }) {
self.onDismiss?()
}
}
animator?.addBehavior(modalViewExitBehaviour)
@andresbrun
andresbrun / DynamicModalBehaviour_dismiss_cancel.swift
Last active January 2, 2017 18:15
Shows how to configure UIDynamics to dismiss the modal.
let pushBehaviour = UIPushBehavior(items: [mainModalView], mode: .instantaneous)
pushBehaviour.pushDirection = velocity.vector * Constants.pushForceRatio
pushBehaviour.setTargetOffsetFromCenter(offset, for: mainModalView)
animator.addBehavior(pushBehaviour)
animator.addBehavior(gravityBehaviour)
animator.removeBehaviors(itemsAttachmentBehaviours)
// Handy extension
class BallView: UIImageView {
init(size: CGFloat) {
super.init(frame: CGRect(origin: .zero, size: CGSize(width: size, height: size)))
image = UIImage(named: "basketball-png-0")
contentMode = .scaleToFill
layer.cornerRadius = size * 0.5
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
@andresbrun
andresbrun / UIDynamicItemBehavior.swift
Created December 22, 2016 19:18
Shows how to create an UIDynamicItemBehavior.
let dynamicBehaviour = UIDynamicItemBehavior(items: balls)
dynamicBehaviour.elasticity = 0.8
dynamicBehaviour.friction = 5.0
animator.addBehavior(dynamicBehaviour)
@andresbrun
andresbrun / UIAttachmentBehaviour_sliding.swift
Created December 22, 2016 11:11
Shows how to configure a sliding behaviour
let slidingBehaviour = UIAttachmentBehavior.slidingAttachment(with: targetView,
attachedTo: draggableView,
attachmentAnchor: targetView.center,
axisOfTranslation: CGVector(dx: 1, dy: 0))
animator.addBehavior(slidingBehaviour)
@andresbrun
andresbrun / DynamicModalBehaviour.swift
Last active December 22, 2016 10:33
Swarm animation for modal's dismissal.
import UIKit
import XCPlayground
public class DynamicModalBehaviour: NSObject {
private var animator: UIDynamicAnimator?
private var dragAttachmentBehaviour: UIAttachmentBehavior!
private var itemsCollisionBehaviour: UICollisionBehavior!
private var modalViewExitBehaviour: UIDynamicItemBehavior!
private var itemsAttachmentBehaviours: [UIAttachmentBehavior]!
private var snapBehaviours: [UISnapBehavior]!
@andresbrun
andresbrun / UIAttachmentBehaviour.swift
Last active December 21, 2016 12:52
Shows the different ways to define a attachmentBehaviour
// Dragging an element with a PanGestureRecognizer for instance
let dragAttachmentBehaviour = UIAttachmentBehavior(item: targetView,
offsetFromCenter: offsetFromCenter,
attachedToAnchor: dragLocation)
// Attaching two box in a chain
let boxAttachmentBehaviour = UIAttachmentBehavior(item: box1,
offsetFromCenter: UIOffset(horizontal: 0, vertical: box1.bounds.midY),
attachedTo: box2,
offsetFromCenter: UIOffset(horizontal: 0, vertical: -box2.bounds.midY))
@andresbrun
andresbrun / UICollisionBehaviour.swift
Last active December 23, 2016 09:00
Shows how to add a collision behaviour
let collisionBehaviour = UICollisionBehavior(items: [targetView])
animator.addBehavior(collisionBehaviour)
// Collision with bounds of superview
collisionBehaviour.setTranslatesReferenceBoundsIntoBoundary(with: .zero)
// Collision with custom boundaries
collisionBehaviour.addBoundary(withIdentifier: "barrier" as NSCopying,
for: UIBezierPath(ovalIn: containerView.bounds))
@andresbrun
andresbrun / UISnapBehaviour.swift
Created December 21, 2016 11:09
Shows how to configure UISnapBehavior
let snapBehaviour = UISnapBehavior(item: targetView, snapTo: CGPoint(x: 100, y: 100))
animator.addBehavior(snapBehaviour)
// You can configure the oscillation of the item when it has to go to the snap point
snapBehaviour.damping = 0.5 // Medium oscillation
@andresbrun
andresbrun / UIGravityBehaviour.swift
Last active December 21, 2016 10:54
Shows how to apply gravity behaviour
let gravityBehaviour = UIGravityBehavior(items: [targetView])
animator.addBehavior(gravityBehaviour)
// It can be defined with
gravityBehaviour.magnitude = 1.0 // Gravity on earth
gravityBehaviour.angle = .pi / 2.0 // downward direction
// Or
gravityBehaviour.gravityDirection = CGVector(dx: 0.0, dy: 1.0) // Gravity on earth