美文网首页swift4.0
swift-view任意位置添加边框线(实线,虚线)

swift-view任意位置添加边框线(实线,虚线)

作者: 279cb620c509 | 来源:发表于2018-08-24 15:27 被阅读416次

废话不多说,直接代码

public struct UIRectSide : OptionSet {

    public let rawValue: Int

    public static let left = UIRectSide(rawValue: 1 << 0)

    public static let top = UIRectSide(rawValue: 1 << 1)

    public static let right = UIRectSide(rawValue: 1 << 2)

    public static let bottom = UIRectSide(rawValue: 1 << 3)

    public static let all: UIRectSide = [.top, .right, .left, .bottom]

    

    public init(rawValue: Int) {

        self.rawValue = rawValue;

    }

}

extension UIView{

    

    ///画虚线边框

    func drawDashLine(strokeColor: UIColor, lineWidth: CGFloat = 1, lineLength: Int = 10, lineSpacing: Int = 5, corners: UIRectSide) {

        let shapeLayer = CAShapeLayer()

        shapeLayer.bounds = self.bounds

        shapeLayer.anchorPoint = CGPoint(x: 0, y: 0)

        shapeLayer.fillColor = UIColor.blue.cgColor

        shapeLayer.strokeColor = strokeColor.cgColor

        

        shapeLayer.lineWidth = lineWidth

        shapeLayer.lineJoin = kCALineJoinRound

        

        //每一段虚线长度 和 每两段虚线之间的间隔

        shapeLayer.lineDashPattern = [NSNumber(value: lineLength), NSNumber(value: lineSpacing)]

        

        let path = CGMutablePath()

        if corners.contains(.left) {

            path.move(to: CGPoint(x: 0, y: self.layer.bounds.height))

            path.addLine(to: CGPoint(x: 0, y: 0))

        }

        if corners.contains(.top){

            path.move(to: CGPoint(x: 0, y: 0))

            path.addLine(to: CGPoint(x: self.layer.bounds.width, y: 0))

        }

        if corners.contains(.right){

            path.move(to: CGPoint(x: self.layer.bounds.width, y: 0))

            path.addLine(to: CGPoint(x: self.layer.bounds.width, y: self.layer.bounds.height))

        }

        if corners.contains(.bottom){

            path.move(to: CGPoint(x: self.layer.bounds.width, y: self.layer.bounds.height))

            path.addLine(to: CGPoint(x: 0, y: self.layer.bounds.height))

        }

        shapeLayer.path = path

        self.layer.addSublayer(shapeLayer)

    }

    ///画实线边框

    func drawLine(strokeColor: UIColor, lineWidth: CGFloat = 1, corners: UIRectSide) {

        

        if corners == UIRectSide.all {

            self.layer.borderWidth = lineWidth

            self.layer.borderColor = strokeColor.cgColor

        }else{

            let shapeLayer = CAShapeLayer()

            shapeLayer.bounds = self.bounds

            shapeLayer.anchorPoint = CGPoint(x: 0, y: 0)

            shapeLayer.fillColor = UIColor.blue.cgColor

            shapeLayer.strokeColor = strokeColor.cgColor

            

            shapeLayer.lineWidth = lineWidth

            shapeLayer.lineJoin = kCALineJoinRound

            

            let path = CGMutablePath()

            

            if corners.contains(.left) {

                path.move(to: CGPoint(x: 0, y: self.layer.bounds.height))

                path.addLine(to: CGPoint(x: 0, y: 0))

            }

            if corners.contains(.top){

                path.move(to: CGPoint(x: 0, y: 0))

                path.addLine(to: CGPoint(x: self.layer.bounds.width, y: 0))

            }

            if corners.contains(.right){

                path.move(to: CGPoint(x: self.layer.bounds.width, y: 0))

                path.addLine(to: CGPoint(x: self.layer.bounds.width, y: self.layer.bounds.height))

            }

            if corners.contains(.bottom){

                path.move(to: CGPoint(x: self.layer.bounds.width, y: self.layer.bounds.height))

                path.addLine(to: CGPoint(x: 0, y: self.layer.bounds.height))

            }

            

            shapeLayer.path = path

            self.layer.addSublayer(shapeLayer)

            

        }

        

    }

}

附:ios UIView 任意位置 裁剪 圆角
https://www.jianshu.com/p/8810ecb9500f

相关文章

  • swift-view任意位置添加边框线(实线,虚线)

    废话不多说,直接代码 附:ios UIView 任意位置 裁剪 圆角https://www.jianshu.com...

  • 给控件添加边框

    经常会遇到给控件添加边框的需求:边框大体有两种实线和虚线 1、虚线边框的添加: 2、实现边框的添加: a、最常采用...

  • UML 类图实践

    0 简介 0.1 记忆技巧 实线-继承 虚线-实现 实线-关联 虚线-依赖 空心菱形-聚合 实心菱形-组合 时序图...

  • AndroidUi(1)-直线

    android中画线比较简单,常用的有实线和虚线 一.实线 二.虚线 Note:stroke的android:wi...

  • CSS基础样式

    border solid 实线dotted 点状虚线dashed 虚线double 双线 水平垂直方向布局 设置水...

  • UML类图与类的关系详解

    虚线箭头指向依赖; 实线箭头指向关联; 虚线三角指向接口; 实线三角指向父类; 空心菱形能分离而独立存在,是聚合;...

  • UML类图详解(转载)

    虚线箭头指向依赖; 实线箭头指向关联; 虚线三角指向接口; 实线三角指向父类; 空心菱形能分离而独立存在,是聚合;...

  • UML类图与类的关系详解

    转载文章 虚线箭头指向依赖; 实线箭头指向关联; 虚线三角指向接口; 实线三角指向父类; 空心菱形能分离而独立存在...

  • iOS 画线-圆

    画实线、虚线圆 .h .m 用法 效果图

  • 想要学习设计模式,你得先会看类图,一张图读懂UML

    虚线箭头指向依赖;实线箭头指向关联;虚线三角指向接口;实线三角指向父类;空心菱形能分离而独立存在,是聚合;实心菱形...

网友评论

    本文标题:swift-view任意位置添加边框线(实线,虚线)

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