第一次使用BezierPaths,想知道这个函数实际上应该如何实现。目前贝塞尔路径在图像的框架内移动,而不是在屏幕上绘图。用UIBezierPath绘制一条线
有没有更好的方法来做到这一点?
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()
}
其他的,更直接的方法,而不是使用图像这样的,被使用'CAShapeLayer '或定制'drawRect',如下所述:http://stackoverflow.com/a/16846770/1271826 – Rob 2014-10-30 20:18:25