This file contains 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 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)) |
This file contains 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 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 |
This file contains 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 |
This file contains 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 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 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 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 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 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 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)) |
OlderNewer