首先使用BezierPaths,想知道这个功能是如何实际应用的.目前,贝塞尔路径在图像的框架内移动,而不是在屏幕上绘制.
有更好的方法吗?
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()
}
解决方法
结束这样做:
func drawLineFromPoint(start : CGPoint,inView view:UIView) {
//design the path
var path = UIBezierPath()
path.movetoPoint(start)
path.addLinetoPoint(end)
//design path in layer
var shapeLayer = CAShapeLayer()
shapeLayer.path = path.CGPath
shapeLayer.strokeColor = lineColor.CGColor
shapeLayer.linewidth = 1.0
view.layer.addSublayer(shapeLayer)
}