第一次使用BezierPath时,想知道这个函数实际上是如何实现的。当前,Bezier路径在图像帧内移动,而不是在屏幕上绘图。
有更好的方法吗?
func drawLineFromPoint(start : CGPoint, toPoint end:CGPoint, ofColor lineColor: UIColor, inView view:UIView) {
var maxWidth = abs(start.x - end.x)
var maxHeight = abs(start.y - end.y)
var contextSize : CGSize!
if maxWidth == 0 {
contextSize = CGSize(width: 1, height: maxHeight)
}else {
contextSize = CGSize(width: maxWidth, height: 1)
}
//design the path
UIGraphicsBeginImageContextWithOptions(contextSize, false, 0)
var path = UIBezierPath()
path.lineWidth = 1.0
lineColor.set()
//draw the path and make visible
path.moveToPoint(start)
path.addLineToPoint(end)
path.stroke()
//create image from path and add to subview
var image = UIGraphicsGetImageFromCurrentImageContext()
var imageView = UIImageView(image: image)
view.addSubview(imageView)
UIGraphicsEndImageContext()
}
相似问题