You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-ios/Session/Utilities/UIView+Draggable.swift

41 lines
2.2 KiB
Swift

// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
extension UIView {
func makeViewDraggable() {
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePanForDragging))
addGestureRecognizer(panGestureRecognizer)
}
@objc private func handlePanForDragging(_ gesture: UIPanGestureRecognizer) {
let location = gesture.location(in: self.superview!)
if let draggedView = gesture.view {
draggedView.center = location
if gesture.state == .ended {
if draggedView.frame.midX >= self.superview!.layer.frame.width / 2 {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseIn, animations: {
draggedView.center.x = self.superview!.layer.frame.width - draggedView.width() / 2
}, completion: nil)
}else{
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseIn, animations: {
draggedView.center.x = draggedView.width() / 2
}, completion: nil)
}
let topMargin = UIApplication.shared.keyWindow!.safeAreaInsets.top + Values.veryLargeSpacing
if draggedView.frame.minY <= topMargin {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseIn, animations: {
draggedView.center.y = topMargin + draggedView.height() / 2
}, completion: nil)
}
let bottomMargin = UIApplication.shared.keyWindow!.safeAreaInsets.bottom
if draggedView.frame.maxY >= self.superview!.layer.frame.height {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseIn, animations: {
draggedView.center.y = self.superview!.layer.frame.height - draggedView.height() / 2 - bottomMargin
}, completion: nil)
}
}
}
}
}