Swift:二阶贝塞尔曲线的联动

效果图

目录

  • 曲线上点算法
  • 点的手势处理:点击线生成点和距离线有效距离内拖动生成点
  • 方格和曲线交叉点的坐标获取

一、曲线上点算法

  • 1.1、思路:根据坐标上的点,计算控制点,从而再通过控制点之间分割成100个点(具体的控制点之间点的个数可以自己定义),通过每个控制点之间的计算生成点,最后串起来就是上图看到的曲线

  • 1.2、部分代码

    //MARK: 通过已知点绘制path
    private func calculate(pointList: [CGPoint]) {
        allPointList.removeAll()
        let path = CGMutablePath()
        // 曲线斜率
        let sharpenRatio = 1.0
        if (pointList.count < 3) {
            path.addLines(between: pointList)
            drawPath(path: path)
            return
        }
        var pMidOfLm = CGPoint()
        var pMidOfMr = CGPoint()
        var cache: CGPoint? = nil
        var startPoint = pointList[0]
        for i in 0...pointList.count - 3 {
            let pL = pointList[I]
            let pM = pointList[i + 1]
            let pR = pointList[i + 2]
            pMidOfLm.x = (pL.x + pM.x) / 2.0
            pMidOfLm.y = (pL.y + pM.y) / 2.0
            pMidOfMr.x = (pM.x + pR.x) / 2.0
            pMidOfMr.y = (pM.y + pR.y) / 2.0
            let lengthOfLm = distanceBetweenPoints(pL, pM)
            let lengthOfMr = distanceBetweenPoints(pR, pM)
            var ratio = lengthOfLm / (lengthOfLm + lengthOfMr) * sharpenRatio
            let oneMinusRatio = (1 - ratio) * sharpenRatio
            let dx = pMidOfLm.x - pMidOfMr.x
            let dy = pMidOfLm.y - pMidOfMr.y
            var cLeft = CGPoint()
            cLeft.x = pM.x + dx * ratio
            cLeft.y = pM.y + dy * ratio
            var cRight = CGPoint()
            cRight.x = pM.x + -dx * oneMinusRatio
            cRight.y = pM.y + -dy * oneMinusRatio
            if (i == 0) {
                let pMidOfLCLeft = CGPoint(x: (pL.x + cLeft.x) / 2.0, y: (pL.y + cLeft.y) / 2.0)
                let pMidOfCLeftM = CGPoint(x: (cLeft.x + pM.x) / 2.0, y: (cLeft.y + pM.y) / 2.0)
                let length1 = distanceBetweenPoints(cLeft, pL)
                let length2 = distanceBetweenPoints(cLeft, pM)
                ratio = length1 / (length1 + length2) * sharpenRatio
                var first = CGPoint()
                first.x = cLeft.x + (pMidOfLCLeft.x - pMidOfCLeftM.x) * ratio
                first.y = cLeft.y + (pMidOfLCLeft.y - pMidOfCLeftM.y) * ratio
                addPoint(startPoint, first, cLeft, pM)
                startPoint = pM
            } else {
                // bezierPath.move(to: startPoint)
                if let weakCache = cache {
                    // bezierPath.addCurve(to: pM, control1: weakCache, control2: cLeft)
                    addPoint(startPoint, weakCache, cLeft, pM)
                    startPoint = pM
                }
            }
            cache = cRight
            if (i == pointList.count - 3) {
                let pMidOfMCRight = CGPoint(x: (pM.x + cRight.x) / 2.0, y: (pM.y + cRight.y) / 2.0)
                let pMidOfCRightR = CGPoint(x: (pR.x + cRight.x) / 2.0, y: (pR.y + cRight.y) / 2.0)
                let length1 = distanceBetweenPoints(cRight, pM)
                let length2 = distanceBetweenPoints(pR, cRight)
                ratio = length2 / (length1 + length2) * sharpenRatio
                var last = CGPoint()
                last.x = cRight.x + (pMidOfCRightR.x - pMidOfMCRight.x) * ratio
                last.y = cRight.y + (pMidOfCRightR.y - pMidOfMCRight.y) * ratio
                // startPoint = pM
                // bezierPath.move(to: startPoint)
                // bezierPath.addCurve(to: pR, control1: cRight, control2: last)
                addPoint(startPoint, cRight, last, pR)
            }
        }
        path.addLines(between: allPointList)
        drawPath(path: path)
    }
    

二、点的手势处理

  • 2.1、点击线生成点


    点击线生成点

    分析:点击线的话,首先是拿到点击点的坐标p0,根据这个坐标,获取这个点与线垂直和水平交叉点的坐标p1和p2,看这个两个点到p0距离
    代码示例

    //MARK: 父视图点击手势
    ///  父视图点击手势
    /// - Parameter panGesture: 手势
    @objc func superTapGester(gesture: UITapGestureRecognizer) {
        guard let currentPath, isCanUserInteractionEnabled, points.count < maxCircleViewNumber else {
            return
        }
        let tapLocation = gesture.location(in: self)
        debugPrint("Tap location in parent view: \(tapLocation)")
        // 1、点击点首先要在 左右两个点的矩形内,如果不在不生点
        var previousPoint: CGPoint = CGPoint()
        var nextPoint: CGPoint = CGPoint()
        /// 要插入的index
        var insertIndex: Int = 0
        for (index, item) in points.enumerated() {
            if tapLocation.x < item.x {
                insertIndex = index
                // 找到后面的点
                nextPoint = item
                break
            }
            previousPoint = item
        }
        guard tapLocation.x > previousPoint.x && tapLocation.y < previousPoint.y && tapLocation.x < nextPoint.x && tapLocation.y > nextPoint.y else {
            debugPrint("❌不在矩形范围内", "previousPoint:\(previousPoint) nextPoint:\(nextPoint)")
            return
        }
        // 在矩形的范围内,确定添加的点事垂直点还是水平点
        // 垂直点
        let vPoint = getPointXY(xy: tapLocation.x, path: currentPath)
        // 水平点
        let hPoint = getPointXY(xy: tapLocation.y, path: currentPath, isX: false)
        // 垂直长度
        let vLength: CGFloat = abs(tapLocation.y - vPoint.y)
        // 水平长度
        let hLength: CGFloat = abs(tapLocation.x - hPoint.x)
        guard vLength < effectiveDistance || hLength < effectiveDistance else {
            debugPrint("✅在矩形范围内 ❌:不在有效距离:\(effectiveDistance) 内, 垂直距离:\(vLength) 水平距离:\(hLength)")
            return
        }
        // 在有效的范围内
        var point: CGPoint = CGPoint()
        if vLength < hLength {
            point = vPoint
            debugPrint("✅在矩形范围内:取值垂直的点")
        } else {
            point = hPoint
            debugPrint("✅在矩形范围内:取值水平的点")
        }
        // 2、在矩形内,生成一个点
        let view = CircleView()
        view.layer.cornerRadius = 7.5
        view.clipsToBounds = false
        view.backgroundColor = .randomColor
        view.layer.borderWidth = 3
        view.layer.borderColor = UIColor.white.cgColor
        view.tag = insertIndex + 100
        self.addSubview(view)
        // 插入视图
        circleViews.insert(view, at: insertIndex)
        // 插入生成的点
        points.insert(point, at: insertIndex)
      
        // 改变其他视图的tag
        for index in (insertIndex + 1)...(circleViews.count - 1) {
            circleViews[index].tag = index + 100
        }
      
        let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(panGester))
        view.addGestureRecognizer(panGestureRecognizer)
      
        view.snp.makeConstraints { make in
            make.center.equalTo(point)
            make.size.equalTo(CGSize(width: 15, height: 15))
        }
      
        // 加震动
        let generator = UINotificationFeedbackGenerator()
        generator.notificationOccurred(.success)
      
        let param = getParamPointArray()
        dataClosure?(param.cmd_state, param.auxiliary_curve)
    }
    
  • 2.2、距离线有效距离内拖动生成点
    分析:拖动的话,首先是拿到点拖动起点的坐标p0,根据p0的坐标xy,分别获取曲线上的点p1和p2,同2.1一样拿到p0与两点的距离,看是否在有效距离effectiveDistance内,如果在看谁距离很近,近的则是点生成点的起点,从而拖动中点跟着移动,这个是利用的父视图的拖动手势
    代码示例

    //MARK: 父视图拖动手势
    ///  父视图拖动手势
    /// - Parameter panGesture: 手势
    @objc func superPanGester(panGesture: UIPanGestureRecognizer) {
        // 最多maxCircleViewNumber个点,包含两头的点
        guard isCanUserInteractionEnabled, points.count < maxCircleViewNumber else {
            return
        }
        switch panGesture.state {
        case .began:
            let startPanLocation = panGesture.location(in: self)
            let result = isPointLine(point: startPanLocation)
            if result.isEffectivePoint {
                // 在拖动开始的位置生成一个点
                superPanInserTag = 100 + result.insertIndex
                // 2、在矩形内,生成一个点
                let view = CircleView()
                view.layer.cornerRadius = 7.5
                view.clipsToBounds = false
                view.backgroundColor = .randomColor
                view.layer.borderWidth = 3
                view.layer.borderColor = UIColor.white.cgColor
                view.tag = superPanInserTag
                self.addSubview(view)
                // 插入视图
                circleViews.insert(view, at: result.insertIndex)
                // 插入生成的点
                points.insert(startPanLocation, at: result.insertIndex)
              
                // 改变其他视图的tag
                for index in (result.insertIndex + 1)...(circleViews.count - 1) {
                    circleViews[index].tag = index + 100
                }
              
                let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(panGester))
                view.addGestureRecognizer(panGestureRecognizer)
              
                view.snp.makeConstraints { make in
                    make.center.equalTo(startPanLocation)
                    make.size.equalTo(CGSize(width: 15, height: 15))
                }
                setNeedsDisplay()
                // 加震动
                let generator = UINotificationFeedbackGenerator()
                generator.notificationOccurred(.success)
            }
            debugPrint("super-拖动开始: \(startPanLocation) superPanInserTag:\(superPanInserTag)")
        case .changed:
            let tapLocation = panGesture.location(in: self)
            if superPanInserTag > 0 {
                debugPrint("super-拖动中: \(tapLocation) inserTag:\(superPanInserTag)")
                // 添加的点跟着移动
                let panGestureRecognizerTag = superPanInserTag - 100
                let previousPoint: CGPoint = points[panGestureRecognizerTag - 1]
                let nextPoint: CGPoint = points[panGestureRecognizerTag + 1]
                guard tapLocation.x > previousPoint.x && tapLocation.y < previousPoint.y && tapLocation.x < nextPoint.x && tapLocation.y > nextPoint.y else {
                    debugPrint("❌不在矩形范围内", "previousPoint:\(previousPoint) nextPoint:\(nextPoint)")
                    // 移除该点
                    points.remove(at: panGestureRecognizerTag)
                    let view = circleViews[panGestureRecognizerTag]
                    circleViews.remove(at: panGestureRecognizerTag)
                    view.removeFromSuperview()
                    for index in panGestureRecognizerTag...(circleViews.count - 1) {
                        circleViews[index].tag = index + 100
                    }
                    // 加震动
                    let generator = UINotificationFeedbackGenerator()
                    generator.notificationOccurred(.success)
                    superPanInserTag = 0
                    setNeedsDisplay()
                    return
                }
                let view = circleViews[panGestureRecognizerTag]
                view.snp.updateConstraints { make in
                    make.center.equalTo(tapLocation)
                }
                points[panGestureRecognizerTag] = tapLocation
                debugPrint("打印tag:\(panGestureRecognizerTag)")
                setNeedsDisplay()
            }
        case .ended:
            superPanInserTag = 0
            debugPrint("super-拖动结束 新的value")
        default:
            debugPrint("super-其他")
        }
    }
    
    //MARK: 是否响应父视图拖动的手势
    /// 是否响应拖动的手势:实现 gestureRecognizer(_:shouldReceive:) 方法
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        // 根据条件决定是否响应手势
        if isCanUserInteractionEnabled {
            let location = touch.location(in: self)
            let result = isPointLine(point: location)
            return result.isEffectivePoint
         } else {
            return false
         }
    }
    

三、方格和曲线交叉点的坐标获取

  • 3.1、方格
    这个花方格就比较简单了,只需要两个for循环即可

    class GridView: UIView {
        override init(frame: CGRect) {
            super.init(frame: frame)
        }
    
        override func draw(_ rect: CGRect) {
            super.draw(rect)
            let width: CGFloat = rect.size.width
            let height: CGFloat = rect.size.height
            // 创建一个UIBezierPath对象
            let path = UIBezierPath()
      
            // 设置线宽和颜色
            UIColor.yellow.setStroke()
            path.lineWidth = 1.0
            let lineVWidth: CGFloat = height / 10.0
            // 绘制水平线
            for i in 0...10 {
                path.move(to: CGPoint(x: 0, y: CGFloat(i) * lineVWidth))
                path.addLine(to: CGPoint(x: width, y: CGFloat(i) * lineVWidth))
            }
      
            // 绘制垂直线
            let lineHWidth: CGFloat = width / 10.0
            for i in 0...10 {
                path.move(to: CGPoint(x: CGFloat(i) * lineHWidth, y: 0))
                path.addLine(to: CGPoint(x: CGFloat(i) * lineHWidth, y: height))
            }
            // 将路径添加到视图中并绘制
            path.stroke()
        }
    
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    
  • 3.2、方格和曲线交叉点的坐标获取

    //MARK: - CGMutablePath曲线-根据x坐标获取y坐标
    extension BezierCurveView {
        //MARK: 根据某个点的x坐标获取y坐标
        /// 根据某个点的x坐标获取y坐标
        /// - Parameters:
        ///   - x: x / y坐标
        ///   - path: CGMutablePath
        /// - Returns: description
        private func getPointXY(xy: CGFloat, path: CGPath, isX: Bool = true) -> CGPoint {
            var value: CGFloat = 0.0
            var prevPoint = CGPoint.zero
            path.applyWithBlock { element in
                switch element.pointee.type {
                case .moveToPoint:
                    prevPoint = element.pointee.points[0]
                case .addLineToPoint:
                    let startPoint = prevPoint
                    let endPoint = element.pointee.points[0]
                    if isX {
                        if xy >= startPoint.x && xy <= endPoint.x {
                            let t = (xy - startPoint.x) / (endPoint.x - startPoint.x)
                            value = startPoint.y + t * (endPoint.y - startPoint.y)
                        }
                    } else {
                        if xy <= startPoint.y && xy >= endPoint.y {
                            let t = (xy - startPoint.y) / (endPoint.y - startPoint.y)
                            value = startPoint.x + t * (endPoint.x - startPoint.x)
                        }
                    }
                    prevPoint = endPoint
                default:
                    break
                }
            }
            return isX ? CGPoint(x: xy, y: value) : CGPoint(x: value, y: xy)
        }
    }
    

demo地址
更多的扩展请参考另一个基础库JKSwiftExtension

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,013评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,205评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,370评论 0 342
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,168评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,153评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,954评论 1 283
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,271评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,916评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,382评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,877评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,989评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,624评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,209评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,199评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,418评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,401评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,700评论 2 345

推荐阅读更多精彩内容