Skip to content

Instantly share code, notes, and snippets.

View andresbrun's full-sized avatar

Andrés Brun andresbrun

View GitHub Profile
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 / 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
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)