Skip to content

Instantly share code, notes, and snippets.

View andresbrun's full-sized avatar

Andrés Brun andresbrun

View GitHub Profile
@andresbrun
andresbrun / SettingPlayground.swift
Last active December 21, 2016 08:45
Create easily a view in Playground to play with
import UIKit
import PlaygroundSupport
let containerView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 320.0, height: 480.0))
// Play with containerView
let targetView = UIView(frame: CGRect(x: containerView.bounds.midX,
y: containerView.bounds.midY,
width: 50.0,
height: 50.0))
@andresbrun
andresbrun / UIPushBehaviour_continuous.swift
Created December 21, 2016 08:55
Shows how to apply a continuous push
let pushContinousBehaviour = UIPushBehavior(items: [targetView], mode: .continuous)
animator.addBehavior(pushContinousBehaviour)
// Using pushDirection
pushContinousBehaviour.pushDirection = CGVector(dx: 50, dy: 0)
// Using magnitude and angle
pushContinousBehaviour.magnitude = 50
pushContinousBehaviour.angle = 0
@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
@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 / 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 / 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_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 / 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)
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 / 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))