You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
4.4 KiB

2 years ago
//
// HPMeasureLineClosure
// HPlusFit
//
// Created by lemo. on 2019/9/24.
// Copyright © 2019 lemo. All rights reserved.
//
import UIKit
fileprivate struct Metric {
static let radius = kScaleWidth(3.0)
}
typealias HPMeasureLineClosure = (CGFloat, Bool) -> Void
class MeasureLineView: UIView {
var measureLineView: UIView!
var shaperLayers: [CAShapeLayer] = []
var measureLineClosure: HPMeasureLineClosure?
override init(frame: CGRect) {
super.init(frame: frame)
//
let pan = UIPanGestureRecognizer(target: self, action: #selector(panAction(pan:)))
pan.maximumNumberOfTouches = 1 //
self.addGestureRecognizer(pan)
self.measureLineView = UIView()
self.measureLineView.frame = CGRect(x: 0, y: kScaleHeight(0), width: 2*Metric.radius, height: self.height)
self.measureLineView.center = CGPoint(x: self.center.x, y: self.measureLineView.center.y)
self.measureLineView.backgroundColor = UIColor.clear
self.addSubview(self.measureLineView)
}
func drawMeasureLine(topSpace: CGFloat) {
for layer in self.shaperLayers {
layer.removeFromSuperlayer()
}
// 线
let solidShaperLayer = CAShapeLayer()
solidShaperLayer.lineWidth = kScaleWidth(2.0)
solidShaperLayer.strokeColor = UIColor.hexColor(0x4E576F).withAlphaComponent(0.1).cgColor
solidShaperLayer.fillColor = UIColor.clear.cgColor
self.measureLineView.layer.addSublayer(solidShaperLayer)
self.shaperLayers.append(solidShaperLayer)
let solidPath = UIBezierPath()
solidPath.move(to: CGPoint(x: Metric.radius, y: 0))
solidPath.addLine(to: CGPoint(x: Metric.radius, y: topSpace - Metric.radius))
solidShaperLayer.path = solidPath.cgPath
//
let circleShaperLayer = CAShapeLayer()
circleShaperLayer.lineWidth = kScaleWidth(2.0)
circleShaperLayer.strokeColor = UIColor.hexColor(0x4E576F).cgColor
circleShaperLayer.fillColor = UIColor.clear.cgColor
self.measureLineView.layer.addSublayer(circleShaperLayer)
self.shaperLayers.append(circleShaperLayer)
let circlePath = UIBezierPath()
circlePath.addArc(withCenter: CGPoint(x: Metric.radius, y: topSpace), radius: Metric.radius, startAngle: 0, endAngle: CGFloat(Double.pi*2.0), clockwise: true)
circleShaperLayer.path = circlePath.cgPath
// 线
let dashLineShaperLayer = CAShapeLayer()
dashLineShaperLayer.lineWidth = kScaleWidth(2.0)
dashLineShaperLayer.strokeColor = UIColor.hexColor(0x4E576F).cgColor
dashLineShaperLayer.lineDashPattern = [NSNumber(value: 4), NSNumber(value: 4)]
dashLineShaperLayer.lineCap = CAShapeLayerLineCap.round
dashLineShaperLayer.lineJoin = CAShapeLayerLineJoin.round
self.measureLineView.layer.addSublayer(dashLineShaperLayer)
self.shaperLayers.append(dashLineShaperLayer)
let dashPath = UIBezierPath()
dashPath.move(to: CGPoint(x: Metric.radius, y: topSpace + Metric.radius))
dashPath.addLine(to: CGPoint(x: Metric.radius, y: self.height))
dashLineShaperLayer.path = dashPath.cgPath
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//
@objc func panAction(pan: UIPanGestureRecognizer) {
let point = pan.location(in: self)
print("point x: \(point.x)")
var xValue: CGFloat = 0.0
if point.x <= 0 {
self.measureLineView.center = CGPoint(x: 0, y: self.measureLineView.center.y)
}else if point.x >= self.width {
self.measureLineView.center = CGPoint(x: self.width, y: self.measureLineView.center.y)
xValue = self.width
}else {
self.measureLineView.center = CGPoint(x: point.x, y: self.measureLineView.center.y)
xValue = point.x
}
print("state: \(pan.state.rawValue)")
if pan.state.rawValue == 3 {
xValue = 0
self.measureLineView.isHidden = true
}else {
self.measureLineView.isHidden = false
}
if let measureColsure = self.measureLineClosure {
measureColsure(xValue, self.measureLineView.isHidden)
}
}
}