我想绘制一些形状,如某人的签名或其他一些写意形状。 我是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)
答案 0 :(得分:3)
你好@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)
}
答案 1 :(得分:3)
这是一个基本的绘图代码,你应该添加UIImageView
来绘制它,基本上你需要在前一个触摸点和当前点之间画一条线,使用线帽.round
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)
}
}
}
故事板设置
<强>结果强>