ios - 快速绘制手绘形状

标签 ios swift

我想画一些形状,比如某人的签名或其他一些手绘形状。 我是 UIBezierPath 的新手。我尝试了以下代码,但它没有按我想要的那样工作。

怎么可能? 时间差

   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)

最佳答案

这里是一个基本的绘图代码,你应该添加UIImageView来绘制它,基本上你需要在前一个触摸点和当前点之间绘制一条线,使用线帽。圆形

import UIKit

class BasicDrawingViewController: UIViewController {

    var lastPoint = CGPoint.zero
    var paintColor : UIColor = UIColor.black
    var lineWidth : CGFloat = 20.0
    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: false)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
//Draw logic
extension BasicDrawingViewController{

    func drawBetweenPoints(point1:CGPoint,point2:CGPoint){
        UIGraphicsBeginImageContext(self.imageView.bounds.size)
        let context = UIGraphicsGetCurrentContext()

        self.imageView.image?.draw(in: self.imageView.bounds)
        context?.move(to: point1)
        context?.addLine(to: point2)
        context?.setLineCap(.round)
        context?.setStrokeColor(self.paintColor.cgColor)
        context?.setLineWidth(lineWidth)
        context?.strokePath()
        self.imageView.image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        debugPrint("Began")
        if let touch = touches.first{
            let point = touch.location(in: self.imageView)
            self.drawBetweenPoints(point1: point, point2: point)
            self.lastPoint = point
        }
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        debugPrint("Move")
        if let touch = touches.first{
            let newPoint = touch.location(in: self.imageView)
            self.drawBetweenPoints(point1: self.lastPoint, point2: newPoint)
            self.lastPoint = newPoint
        }

    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        debugPrint("Ended")
        if let touch = touches.first{
            let point = touch.location(in: self.imageView)
            debugPrint(point)
        }
    }
}

Storyboard设置

enter image description here enter image description here

结果

enter image description here

关于ios - 快速绘制手绘形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48421786/

相关文章:

ios - 如何以编程方式将不同单元格的不同图像添加到 tableView (Swift 3)

ios - ImageView 一直处于侧边状态

ios - 具有 iOS 自动布局约束的 UIScrollView : Wrong size for subviews

Swift 动画 - 按钮后面的圆圈

ios - Firebase + Swift TableView 按日期排序

swift - 使用来自同一类的类 var 时省略类名

ios - 如果 NSUserDefaults 返回 nil,应用程序将崩溃

ios - 企业应用程序 (IOS) 上的证书即将到期

ios - 如何将 FacebookSDK v4.6 与 Xcode 7 beta 结合使用

ios - Realm Swift - 使用 List 属性过滤对象(多个)