微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – 快速绘制手绘形状

我想绘制一些形状,如某人的签名或其他一些写意形状.
我是UIBezierPath的新手.我尝试过以下代码,但它没有按照我的意愿运行.

怎么可能呢?
TIA

let path = UIBezierPath()
    path.move(to: from)
    path.addLine(to: to)


    let shapeLayer = CAShapeLayer()
    shapeLayer.path = path.cgPath
    shapeLayer.strokeColor = lineColor.cgColor
    shapeLayer.linewidth = 1.0

    view.layer.addSublayer(shapeLayer)

解决方法

你好@Rajinder为此你必须获得接触点,所以你必须使用
以下方法.
override func touchesBegan(_ touches: Set<UITouch>,with event: 
UIEvent?) 
{
    let touch = event?.alltouches?.first
    let touchLocation: CGPoint? = touch?.location(in: self.view)
    //---Declare "from" globally  as CGPoint
    from = CGPoint(x: (touchLocation?.x)!,y: (touchLocation?.y)!)

}


override func touchesMoved(_ touches: Set<UITouch>,with event: UIEvent?) {

    let touch = event?.alltouches?.first
    let touchLocation: CGPoint? = touch?.location(in: self.view)

    //--Get New touch point
    let to = CGPoint(x: (touchLocation?.x)!,y: (touchLocation?.y)!)

    //--Draw line
    drawLineFromPoint(from: from,to: to,ofColor: UIColor.red,inView: self.view)

    //--Save as older point
    from = to

}


func drawLineFromPoint(from : CGPoint,to:CGPoint,ofColor lineColor: UIColor,inView view:UIView)
 {

    //design the path
    let path = UIBezierPath()
    path.move(to: from)
    path.addLine(to: to)

    //design path in layer
    let shapeLayer = CAShapeLayer()
    shapeLayer.path = path.cgPath
    shapeLayer.strokeColor = lineColor.cgColor
    shapeLayer.linewidth = 1.0

    view.layer.addSublayer(shapeLayer)
}

原文地址:https://www.jb51.cc/iOS/331277.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐