Skip to content

Instantly share code, notes, and snippets.


Andrés Brun andresbrun

View GitHub Profile
View UIDynamics_check_outOfBounds.swift
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) }) {
andresbrun / DynamicModalBehaviour_dismiss_cancel.swift
Last active Jan 2, 2017
Shows how to configure UIDynamics to dismiss the modal.
View DynamicModalBehaviour_dismiss_cancel.swift
let pushBehaviour = UIPushBehavior(items: [mainModalView], mode: .instantaneous)
pushBehaviour.pushDirection = velocity.vector * Constants.pushForceRatio
pushBehaviour.setTargetOffsetFromCenter(offset, for: mainModalView)
// Handy extension
View BallView.swift
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 / UIDynamicItemBehavior.swift
Created Dec 22, 2016
Shows how to create an UIDynamicItemBehavior.
View UIDynamicItemBehavior.swift
let dynamicBehaviour = UIDynamicItemBehavior(items: balls)
dynamicBehaviour.elasticity = 0.8
dynamicBehaviour.friction = 5.0
andresbrun / UIAttachmentBehaviour_sliding.swift
Created Dec 22, 2016
Shows how to configure a sliding behaviour
View UIAttachmentBehaviour_sliding.swift
let slidingBehaviour = UIAttachmentBehavior.slidingAttachment(with: targetView,
attachedTo: draggableView,
axisOfTranslation: CGVector(dx: 1, dy: 0))
andresbrun / DynamicModalBehaviour.swift
Last active Dec 22, 2016
Swarm animation for modal's dismissal.
View DynamicModalBehaviour.swift
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 / UIAttachmentBehaviour.swift
Last active Dec 21, 2016
Shows the different ways to define a attachmentBehaviour
View UIAttachmentBehaviour.swift
// 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 / UICollisionBehaviour.swift
Last active Dec 23, 2016
Shows how to add a collision behaviour
View UICollisionBehaviour.swift
let collisionBehaviour = UICollisionBehavior(items: [targetView])
// 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 / UISnapBehaviour.swift
Created Dec 21, 2016
Shows how to configure UISnapBehavior
View UISnapBehaviour.swift
let snapBehaviour = UISnapBehavior(item: targetView, snapTo: CGPoint(x: 100, y: 100))
// You can configure the oscillation of the item when it has to go to the snap point
snapBehaviour.damping = 0.5 // Medium oscillation
andresbrun / UIGravityBehaviour.swift
Last active Dec 21, 2016
Shows how to apply gravity behaviour
View UIGravityBehaviour.swift
let gravityBehaviour = UIGravityBehavior(items: [targetView])
// 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
You can’t perform that action at this time.