This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let dynamicBehaviour = UIDynamicItemBehavior(items: balls) | |
| dynamicBehaviour.elasticity = 0.8 | |
| dynamicBehaviour.friction = 5.0 | |
| animator.addBehavior(dynamicBehaviour) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let slidingBehaviour = UIAttachmentBehavior.slidingAttachment(with: targetView, | |
| attachedTo: draggableView, | |
| attachmentAnchor: targetView.center, | |
| axisOfTranslation: CGVector(dx: 1, dy: 0)) | |
| animator.addBehavior(slidingBehaviour) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
NewerOlder