第一次使用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