美文网首页
swift-绘制虚线

swift-绘制虚线

作者: 一蓑丨烟雨 | 来源:发表于2017-09-12 18:37 被阅读53次

参考:http://www.jianshu.com/p/f9009968c735

效果图:

虚线边框.png

代码:

    //绘制虚线边框
    func addDashdeBorderLayer(byView view:UIView, color:UIColor,lineWidth width:CGFloat){
        let shapeLayer = CAShapeLayer()
        let size = view.frame.size
        
        let shapeRect = CGRectMake(10, 10, size.width-20, size.height-20)
        shapeLayer.bounds = shapeRect
        shapeLayer.position = CGPoint(x: size.width*0.5, y: size.height*0.5)
        shapeLayer.fillColor = UIColor.clear.cgColor
        shapeLayer.strokeColor = color.cgColor
        shapeLayer.lineWidth = width
        shapeLayer.lineJoin = kCALineJoinRound
        //
        shapeLayer.lineDashPattern = [3,4]
        let path = UIBezierPath(roundedRect: shapeRect, cornerRadius: 5)
        shapeLayer.path = path.cgPath
        view.layer.addSublayer(shapeLayer)
        
    }

效果图2:

虚线2.png

代码:

    func addDashdeBorderLayer(by view:UIView){
        let imgV:UIImageView = UIImageView(frame: CGRect(x: 0, y: view.sizeHeight-5, width: view.sizeWidth, height: 5))
        view.addSubview(imgV)
        UIGraphicsBeginImageContext(imgV.frame.size)
        
        let context = UIGraphicsGetCurrentContext()
        context?.setLineCap(CGLineCap.square)
        
        let lengths:[CGFloat] = [10,30,]
        context?.setStrokeColor(UIColor.red.cgColor)
        context?.setLineWidth(2)
        context?.setLineDash(phase: 0, lengths: lengths)
        context?.move(to: CGPoint(x: 0, y: 3))
        context?.addLine(to: CGPoint(x: view.sizeWidth, y: 3))
        context?.strokePath()
        
        context?.setStrokeColor(UIColor.blue.cgColor)
        context?.setLineWidth(2)
        context?.setLineDash(phase: 0, lengths: lengths)
        context?.move(to: CGPoint(x: 20, y: 3))
        context?.addLine(to: CGPoint(x: view.sizeWidth, y: 3))
        context?.strokePath()
        imgV.image = UIGraphicsGetImageFromCurrentImageContext()
     //结束
        UIGraphicsEndImageContext()
    }

相关文章

  • swift-绘制虚线

    参考:http://www.jianshu.com/p/f9009968c735 效果图: 代码: 效果图2: 代码:

  • Swift-虚线绘制

    iOS中有时候会遇到虚线绘制,关于绘制虚线有两种方式,一种是通过Context绘制图片通过UIImageView展...

  • iOS 绘制虚线

    /** ** lineView: 需要绘制成虚线的view ** lineLength: 虚线的宽度 ** ...

  • iOS开发绘制虚线的方法

    iOS开发绘制虚线的方法 方法一:通过Quartz 2D 在 UIView drawRect:方法进行绘制虚线 -...

  • IOS绘制虚线的方法总结

    一、重写drawRect方法 二、采用CAShapeLayer方式绘制虚线 三、经济实惠型:采用贴图的方式绘制虚线...

  • iOS两种绘制虚线的方法

    个人CSND 一、绘制单条的虚线 二、给一个控件添加虚线 1、绘制单条的虚线 样式1:------- 2、给一个控...

  • Androidz中Drawable绘制虚线

    Android绘制虚线有很多种方式,常用的就是通过drawable资源绘制虚线。示例代码如下: android:s...

  • 绘制虚线

    CAShapeLayer *shapeLayer = [CAShapeLayer layer]; [shape...

  • 绘制虚线

    方法一 ···· ····lengths的值{10,10}表示先绘制10个点,再跳过10个点,如此反复,如图: 如...

  • 绘图相关(平滑过渡色、镂空效果、部分圆角等)

    1.绘制虚线根据图像的边缘绘制虚线 2.贝塞尔画出曲线(可以是直线,改变线型就会变成虚线) 3.画正方形(部分圆角...

网友评论

      本文标题:swift-绘制虚线

      本文链接:https://www.haomeiwen.com/subject/bgfhsxtx.html