ios – 用UIBezierPath画一条线

前端之家收集整理的这篇文章主要介绍了ios – 用UIBezierPath画一条线前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
首先使用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)
}
原文链接:https://www.f2er.com/iOS/337370.html

猜你在找的iOS相关文章