有时候app上办理一些业务需要手动签名
分析:签名无非是把手指滑动屏幕的痕迹记录下来,手指在屏幕上滑动系统自带touchesBegan touchesMoved touchesEnded 等方法,把每次划过的痕迹及线条保存起来,在view中重新绘画
//手指触屏开始
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
}
//手指移动时候发出
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch : UITouch = NSSet(set: touches).anyObject() as? UITouch{
let point = touch.location(in:self)
pointArray?.append(point)//每条线点的集合
self.setNeedsDisplay()
}
}
//MARK: - -手指离开屏幕
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let array = pointArray{
lineArray?.append(array)//线的集合
widthArray?.append(widthCount)
colorArray?.append(colorCount)
pointArray?.removeAll()
}
}
override func draw(_ rect: CGRect) {
//获取上下文
guard let context = UIGraphicsGetCurrentContext() else {
return
}
context.beginPath()
context.setLineWidth(4)
//线条拐角的样式 设为平滑
context.setLineJoin(CGLineJoin.round)
//线条开始的样式 设为平滑
context.setLineCap(CGLineCap.round)
//查看lineArray数组里是否有线条,有就将之前画的重绘,没有只画当前线条
if lineArray!.count > 0{
for i in 0..<lineArray!.count{
let array = lineArray![I]
if array.count>0{
context.beginPath()
let startPoint : CGPoint = array[0]
context.move(to: startPoint)
for j in 1..<array.count{
let myPoint : CGPoint = array[j]
context.addLine(to: myPoint)
}
//获取colorArray数组里的要绘制线条的颜色
let stroColor = colors![colorArray![I]]
context.setStrokeColor(stroColor.cgColor)
//获取WidthArray数组里的要绘制线条的宽度
let lineWidth = widths![widthArray![I]]
context.setLineWidth(lineWidth)
//保存自己画的
context.strokePath()
}
}
}
//画当前的线
if pointArray!.count>0{
context.beginPath()
let startPoint : CGPoint = pointArray![0]
context.move(to: startPoint)
for j in 1..<pointArray!.count{
let myPoint : CGPoint = pointArray![j]
context.addLine(to: myPoint)
}
let stroColor = colors![colorCount]
context.setStrokeColor(stroColor.cgColor)
let lineWidth = widths![widthCount]
context.setLineWidth(lineWidth)
//保存自己画的
context.strokePath()
}
}

网友评论