Swift 画线控件(支持xib和storyboard)
来源:互联网 发布:视觉识别软件 编辑:程序博客网 时间:2024/05/29 16:24
因项目中经常用到各种分割线,索性封装了一个画线的控件。
[github] 路径
特性
- 支持在xib中使用
- 支持设置线的颜色
- 支持设置线的粗细
- padding属性
- 支持横线和竖线
- 支持画虚线
- 可设置虚线间隔
- 可设置虚线线段的长度
效果预览
代码如下
//// CUILine.swift// JYHealth//// Created by luoyang on 2017/11/30.// Copyright © 2017年 JY. All rights reserved.//import UIKit@IBDesignableclass CUILine: UIView { // line @IBInspectable var lineWidth: CGFloat = 1 @IBInspectable var lineColor: UIColor = UIColor.black // padding @IBInspectable var paddingLeft: CGFloat = 0 @IBInspectable var paddingRight: CGFloat = 0 @IBInspectable var paddingTop: CGFloat = 0 @IBInspectable var paddingBottom: CGFloat = 0 // direct @IBInspectable var isHorizontal: Bool = true // dash @IBInspectable var isDash: Bool = false @IBInspectable var dashPointWidth: CGFloat = 3.0 @IBInspectable var dashSpace: CGFloat = 1.0 override func draw(_ rect: CGRect) { super.draw(rect) // Drawing code // 获取上下文对象 let context = UIGraphicsGetCurrentContext() var bx: CGFloat = 0, by: CGFloat = 0, ex: CGFloat = 0, ey: CGFloat = 0; if isHorizontal { bx = paddingLeft by = CGFloat(Int(rect.size.height)/2) ex = rect.size.width - paddingRight ey = by } else { bx = CGFloat(Int(rect.size.width)/2) by = paddingTop ex = bx ey = rect.size.height - paddingBottom } // 画中间虚线 let path = CGMutablePath() let begin = CGPoint(x: bx, y: by), end = CGPoint(x: ex, y: ey) path.move(to: begin) path.addLine(to: end) // 2、 添加路径到图形上下文 context!.addPath(path) // 3、 设置状态 context!.setLineWidth(lineWidth / UIScreen.main.scale) context!.setStrokeColor(lineColor.cgColor) if isDash { context!.setLineDash(phase: 0, lengths: [dashPointWidth, dashSpace]) } // 4、 绘制图像到指定图形上下文 context!.drawPath(using: .fillStroke) }}
xib中用法
step1: 拖一个UIView到界面上
step2: 如下图设置该UIView的class为CUILine
step3: Attributes inspector可设置线的各种属性参数,如下图
阅读全文
0 0
- Swift 画线控件(支持xib和storyboard)
- Swift 圆形渐变进度条 支持事件 支持XIB和StoryBoard
- 获取XIB和Storyboard生成的控件
- swift 自定义控件在StoryBoard(xib)里使用的属性
- Storyboard和Xib混用黑魔法-swift版
- swift 代码加载xib storyboard
- storyboard 和 xib
- Xib和storyboard对比
- Xib和storyboard对比
- Xib和storyboard对比
- Xib和storyboard对比
- Xib和storyboard对比
- ios-xib和storyboard
- iOS 代码取得到 Storyboard和XIB控件,控制器
- Swift下自定义xib添加到Storyboard
- Xib和storyboard的比较
- Xib和StoryBoard的区别
- Autolayout 和 storyboard xib相关
- Spring与Quartz的整合实现定时任务调度
- spark streaming 获取数据方式
- 轻松使用git命令查看版本记录
- Oracle 时间相减得出毫秒、秒、分、时、天,,【转】
- Spring方法拦截器MethodInterceptor
- Swift 画线控件(支持xib和storyboard)
- 你真的理解AIDL中的in,out,inout么?
- export import
- Nginx能做什么?
- 看完还不懂HTTPS我直播吃翔
- 多线程定时器System.Timers.Timer
- H5 贪吃蛇原代码
- KEBA控制器通过PROFINET连接西门子PLC
- UEFI模式下win10、Ubuntu双系统安装和问题集锦