A simple protocol (No need to subclass, no need to implement methods and properties yourself. Just drop the BJDraggable file into your project) utilizing the powerful UIKitDynamics
API, which makes ANY UIView
draggable within a boundary view that acts as collision body, with a single method call.
Use pod 'BJDraggable'
OR
Drag and Drop the BJDraggable.swift file (Located inside the Source folder in repo.) into your Xcode project.
- Just call the
addDraggability
method on your view and pass in a boundary view and that's it:
override func viewDidLoad() {
super.viewDidLoad()
self.loginView.addDraggability(withinView: self.view)
self.omegaView.addDraggability(withinView: self.omegaContainerView)
self.omegaContainerView.addDraggability(withinView: self.view)
self.signupButton.addDraggability(withinView: self.view)
}
- You can also add margin to your boundary view by passing in a
UIEdgeInsets
parameter:
self.loginView.addDraggability(withinView: self.view, withMargin: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
- To stop the dragging ability, simply call
removeDraggability
method:
@IBAction func removeDraggabilityButtonTouched(_ sender: UIButton) {
self.loginView.removeDraggability()
self.omegaView.removeDraggability()
self.omegaContainerView.removeDraggability()
self.signupButton.removeDraggability()
}
Note: Call removeDraggability()
in the deinit
of your UIViewController
or UIView
if your think you're done with it. This does the cleanup of memory and other variables used.
-
If you want the view to move to its original position (respecting its autolayout constraints) after removing draggability like in the example GIF above, set the
shouldResetViewPositionAfterRemovingDraggability
property totrue
. Default value isfalse
. -
To visually highlight the boundary lines within which the drag will happen, you can make
isDraggableDebugModeEnabled
property totrue
. The effective boundary (with margins applied) will be highlighted in red color. Specifying your own color will be added in the next or subsequent commits.
- Currently, due to the nature of the implementation, the draggability will not work well with rounded-corner views, since invisible walls are drawn around the superview separately in the manner like
left
,right
,top
,bottom
to simulate boundary. - This could be overcome by eliminating the use of imaginary special views and instead, using real boundaries with paths. BUT this method comes with one little caveat that, the view that you're dragging will not slide against the walls, they just stick to it.
This code is distributed under the terms and conditions of the MIT license.