车辆监控地图部分
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
//
|
||||
// DDUIPanGestureRecognizer.swift
|
||||
// DDUIGestureRecognizer
|
||||
// Created by DDIsFriend on 2023/12/19.
|
||||
|
||||
|
||||
import Foundation
|
||||
|
||||
open class DDUIPanGestureRecognizer : UIPanGestureRecognizer {
|
||||
public enum PanGestureFromType {
|
||||
case bottom,top
|
||||
}
|
||||
|
||||
public struct PanGesValue {
|
||||
public var from : PanGestureFromType = .bottom
|
||||
public var minDisplayHeight : CGFloat = 0
|
||||
public var maxDisplayHeight : CGFloat = 0
|
||||
public var criticalValue : CGFloat?
|
||||
public var isExpanded : Bool = false
|
||||
internal var translationY : CGFloat = 0
|
||||
internal var currentY : CGFloat = 0
|
||||
}
|
||||
|
||||
public var panGesValue = PanGesValue.init()
|
||||
|
||||
|
||||
/// 会执行默认方法
|
||||
/// - Parameter target: target
|
||||
public init() {
|
||||
super.init(target: nil, action: nil)
|
||||
self.addTarget(self, action: #selector(panGesAction(ges: )))
|
||||
}
|
||||
|
||||
/// 需要自己添加target和action
|
||||
public override init(target: Any?, action: Selector?) {
|
||||
super.init(target: target, action: action)
|
||||
}
|
||||
|
||||
@objc fileprivate func panGesAction(ges: DDUIPanGestureRecognizer) {
|
||||
guard let superView = ges.view?.superview else {
|
||||
assert(false,"父类都没有,拖什么拖")
|
||||
return
|
||||
}
|
||||
|
||||
let superViewFrame = superView.frame
|
||||
let viewFrame = ges.view?.frame ?? .zero
|
||||
let superViewH = superViewFrame.size.height
|
||||
let viewH = viewFrame.size.height
|
||||
|
||||
switch ges.panGesValue.from {
|
||||
case .bottom:
|
||||
/// 拖动的view的最小y值,这个值和view的高度有关,view的最大y值要和它的superView的最大y值相等,所以minY可能等于deltaH
|
||||
var minY = superViewH - ges.panGesValue.maxDisplayHeight
|
||||
let deltaH = superViewH - viewH
|
||||
if deltaH > minY {
|
||||
minY = deltaH
|
||||
}
|
||||
|
||||
/// 拖动的view的最大y值
|
||||
var maxY = superViewH - ges.panGesValue.minDisplayHeight
|
||||
if deltaH > maxY {
|
||||
maxY = deltaH
|
||||
}
|
||||
|
||||
var currentY : CGFloat = 0.0
|
||||
switch ges.state {
|
||||
case .began:
|
||||
currentY = viewFrame.origin.y
|
||||
break
|
||||
case .changed:
|
||||
/// 拖动时的移动距离是当前changed与上一次changed的差值,即deltaY
|
||||
let translationPoint = ges.translation(in: ges.view)
|
||||
let deltaY = translationPoint.y - ges.panGesValue.translationY
|
||||
ges.panGesValue.translationY = translationPoint.y
|
||||
|
||||
currentY = (viewFrame.origin.y) + deltaY
|
||||
|
||||
/// 当到达最小y值时返回
|
||||
if currentY < minY {
|
||||
return
|
||||
}
|
||||
|
||||
/// 当到达最大y值时返回
|
||||
if currentY > maxY {
|
||||
return
|
||||
}
|
||||
|
||||
break
|
||||
case .ended:
|
||||
/// 当拖动的距离到达某个值时就完全显示
|
||||
let criticalValue = ges.panGesValue.criticalValue ?? (ges.panGesValue.maxDisplayHeight / 2)
|
||||
let criticalY = maxY - criticalValue
|
||||
|
||||
if ges.panGesValue.currentY < criticalY {
|
||||
currentY = minY
|
||||
/// 已经展开
|
||||
ges.panGesValue.isExpanded = true
|
||||
}else{
|
||||
currentY = maxY
|
||||
/// 未展开
|
||||
ges.panGesValue.isExpanded = false
|
||||
}
|
||||
ges.panGesValue.translationY = 0
|
||||
break
|
||||
default:
|
||||
currentY = viewFrame.origin.y
|
||||
break
|
||||
}
|
||||
ges.panGesValue.currentY = currentY
|
||||
|
||||
UIView.animate(withDuration: 0.1, animations: {
|
||||
ges.view?.frame = CGRectMake(viewFrame.origin.x, currentY, viewFrame.size.width, viewFrame.size.height)
|
||||
})
|
||||
break
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
extension UIView {
|
||||
public func addPanGesture(from: DDUIPanGestureRecognizer.PanGestureFromType, minDisplayHeight: CGFloat, maxDisplayHeight: CGFloat, criticalValue: CGFloat? = nil) {
|
||||
let pan = DDUIPanGestureRecognizer.init()
|
||||
pan.panGesValue.from = from
|
||||
pan.panGesValue.minDisplayHeight = minDisplayHeight
|
||||
pan.panGesValue.maxDisplayHeight = maxDisplayHeight
|
||||
pan.panGesValue.criticalValue = criticalValue
|
||||
addGestureRecognizer(pan)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user