• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Swift实时画箭头的实现

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

iOS上实现画箭头,如果是指定了坐标点,那是很简单的,但如果需要做到实时绘制,就需要计算一下了

需求:

在白板上,根据手势落下点和移动点,实时绘制一条箭头直线(如下图)

 

实现代码:

/// 获取箭头的点位置
    ///
    /// - Parameters:
    ///   - fPoint: <#fPoint description#>
    ///   - tPoint: <#tPoint description#>
    /// - Returns: <#return value description#>
    func getArrowPoint(fPoint:CGPoint,tPoint:CGPoint) -> (CGPoint,CGPoint,CGPoint) {
        var p1 = CGPoint.zero           //箭头点1
        var p2 = CGPoint.zero           //箭头点2
        var p3 = CGPoint.zero           //箭头最前面点
        //假设箭头边长20,箭头是一个等腰三角形
        let line = sqrt(pow(fabs(tPoint.x-fPoint.x), 2)+pow(fabs(tPoint.y-fPoint.y), 2))
        let arrowH:CGFloat = line>40 ? 20 : line/3
        //线与水平方向的夹角
        let angle = getAnglesWithThreePoints(p1: fPoint, p2: tPoint, p3: CGPoint(x: fPoint.x, y: tPoint.y))
        let _x = CGFloat(fabs(sin(angle)))*arrowH/2
        let _y = CGFloat(fabs(cos(angle)))*arrowH/2
        //向右上角、水平向右
        if tPoint.x >= fPoint.x && tPoint.y <= fPoint.y{
            p1.x = tPoint.x-_x
            p1.y = tPoint.y-_y
            
            p2.x = tPoint.x+_x
            p2.y = tPoint.y+_y
            
            p3.x = tPoint.x+_y*2
            p3.y = tPoint.y-_x*2
            
        }else if tPoint.x > fPoint.x && tPoint.y > fPoint.y{
            //向右下角
            p1.x = tPoint.x+_x
            p1.y = tPoint.y-_y
            
            p2.x = tPoint.x-_x
            p2.y = tPoint.y+_y
            
            p3.x = tPoint.x+_y*2
            p3.y = tPoint.y+_x*2
        }else if tPoint.x < fPoint.x && tPoint.y < fPoint.y{
            //向左上角
            p1.x = tPoint.x-_x
            p1.y = tPoint.y+_y
            
            p2.x = tPoint.x+_x
            p2.y = tPoint.y-_y
            
            p3.x = tPoint.x-_y*2
            p3.y = tPoint.y-_x*2
            
        }else if tPoint.x < fPoint.x && tPoint.y >= fPoint.y{
            //向左下角,水平向左
            p1.x = tPoint.x-_x
            p1.y = tPoint.y-_y
            
            p2.x = tPoint.x+_x
            p2.y = tPoint.y+_y
            
            p3.x = tPoint.x-_y*2
            p3.y = tPoint.y+_x*2
        }else if fPoint.x==tPoint.x {
            //竖直方向
            p1.x=tPoint.x-arrowH/2
            p1.y=tPoint.y
            p2.x=tPoint.x+arrowH/2
            p2.y=tPoint.y
            p3.x=tPoint.x
            p3.y = tPoint.y>fPoint.y ? tPoint.y+arrowH : tPoint.y-arrowH
        }
        
        return (p1,p2,p3)
    }

 

其中,获取夹角方法:getAnglesWithThreePoints

/// 计算三点之间的角度
    ///
    /// - Parameters:
    ///   - p1: 点1
    ///   - p2: 点2(也是角度所在点)
    ///   - p3: 点3
    /// - Returns: 角度(180度制)
    func getAnglesWithThreePoints(p1:CGPoint,p2:CGPoint,p3:CGPoint) -> Double {
        //排除特殊情况,三个点一条线
        if (p1.x == p2.x && p2.x == p3.x) || ( p1.y == p2.x && p2.x == p3.x){
            return 0
        }
        
        let a = fabs(p1.x - p2.x)
        let b = fabs(p1.y - p2.y)
        let c = fabs(p3.x - p2.x)
        let d = fabs(p3.y - p2.y)
        
        if (a < 1.0 && b < 1.0) || (c < 1.0 && d < 1.0){
            return 0
        }
        let e = a*c+b*d
        let f = sqrt(a*a+b*b)
        let g = sqrt(c*c+d*d)
        let r = Double(acos(e/(f*g)))
        return r        //弧度值
        
//        return (180*r/Double.pi)      //角度值

    }
View Code

 

最后得到三个点,即箭头的三个点坐标

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
swiftdelegate自定义发布时间:2022-07-13
下一篇:
IOS开发入门之一从零开始学习Swift发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap