低仿扫描全能王的选择区域功能

本文探讨如何低仿扫描全能王中的选择区域功能,包括端点可拖动、最小区域检测、凹四边形检测和交叉重连等步骤。详细介绍了识别点、更新UI以及使用CALayer进行放大镜处理的实现方法,并提供了GitHub仓库链接以供参考。
摘要由CSDN通过智能技术生成

扫描全能王 CS, Cam Scanner 很是强大,

本文简单仿一下他的选择区域功能。

端点可拖动

1, 识别点

本文例子比较简单,只有四个端点,用于拖拽

给定四个坐标,

先识别到开始点击的位置,距离哪个点近,

就认为要拖动那个点


    // 四个点,按方位划分
    enum SketchPointOption: Int{
        case leftTop = 0, rightTop = 1, leftBottom = 2
        case rightBottom = 3
    }



//	先识别到开始点击的位置,距离哪个点,在一定范围内
 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        super.touchesBegan(touches, with: event)
        
        guard let touch = touches.first else{
            return
        }
        
        
        let currentPoint = touch.location(in: self)
        
        // 判定选中的最大距离
        let maxDistance: CGFloat = 20
        let points = [defaultPoints.leftTop, defaultPoints.rightTop, defaultPoints.leftBottom,
                      defaultPoints.rightBottom]
        for pt in points{
            let distance = abs(pt.x - currentPoint.x) + abs(pt.y - currentPoint.y)
            if distance <= maxDistance, let pointIndex = points.firstIndex(of: pt){
                currentControlPointType = SketchPointOption(rawValue: pointIndex)
                break
            }
        }
    }
    


    // 如果上一步识别到了,就更新选择点的位置,并重新连线
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesMoved(touches, with: event)
        if currentControlPointType != nil, let touch = touches.first{
            
            let current = touch.location(in: self)
            guard bounds.contains(current) else{
                return
            }
            // 就更新选择点的位置
            prepare(point: current)
            // 并重新连线
            reloadData()
        }
        
    }
2,更新 UI

本文中,图上四个点,没有用四个控件,

四个点用的是 CALayer, 事件处理采用上面的触摸检测,


    var lineLayer: CAShapeLayer = {
        let l = CAShapeLayer()
        l.lineWidth = 1
        l.fillColor = UIColor.clear.cgColor
        l.strokeColor = SketchColor.normal
        return l
    }()
    
    var pointsLa
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值