Swift - 绘制背景线条

效果

源码

  1. //
  2. // BackgroundLineView.swift
  3. // LineBackgroundView
  4. //
  5. // Created by YouXianMing on 16/8/14.
  6. // Copyright © 2016年 YouXianMing. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. // MARK: Public class : BackgroundLineView
  12.  
  13. class BackgroundLineView: UIView {
  14.  
  15. // MARK: Properties.
  16.  
  17. /// Line width, default is 5.
  18. var lineWidth : CGFloat {
  19.  
  20. get {return backgroundView.lineWidth}
  21.  
  22. set(newVal) {
  23.  
  24. backgroundView.lineWidth = newVal
  25. backgroundView.setNeedsDisplay()
  26. }
  27. }
  28.  
  29. /// Line gap, default is 3.
  30. var lineGap : CGFloat {
  31.  
  32. get {return backgroundView.lineGap}
  33.  
  34. set(newVal) {
  35.  
  36. backgroundView.lineGap = newVal
  37. backgroundView.setNeedsDisplay()
  38. }
  39. }
  40.  
  41. /// Line color, default is grayColor.
  42. var lineColor : UIColor {
  43.  
  44. get {return backgroundView.lineColor}
  45.  
  46. set(newVal) {
  47.  
  48. backgroundView.lineColor = newVal
  49. backgroundView.setNeedsDisplay()
  50. }
  51. }
  52.  
  53. /// Rotate value, default is 0.
  54. var rotate : CGFloat {
  55.  
  56. get {return backgroundView.rotate}
  57.  
  58. set(newVal) {
  59.  
  60. backgroundView.rotate = newVal
  61. backgroundView.setNeedsDisplay()
  62. }
  63. }
  64.  
  65. convenience init(frame: CGRect, lineWidth : CGFloat, lineGap : CGFloat, lineColor : UIColor, rotate : CGFloat) {
  66.  
  67. self.init(frame : frame)
  68. self.lineWidth = lineWidth
  69. self.lineGap = lineGap
  70. self.lineColor = lineColor
  71. self.rotate = rotate
  72. }
  73.  
  74. // MARK: Override system method.
  75.  
  76. override func layoutSubviews() {
  77.  
  78. super.layoutSubviews()
  79. setupBackgroundView()
  80. }
  81.  
  82. override init(frame: CGRect) {
  83.  
  84. super.init(frame: frame)
  85.  
  86. self.layer.masksToBounds = true
  87. self.addSubview(backgroundView)
  88. }
  89.  
  90. required init?(coder aDecoder: NSCoder) {
  91.  
  92. fatalError("init(coder:) has not been implemented")
  93. }
  94.  
  95. // MARK: Private value & func.
  96.  
  97. private let backgroundView = LineBackground(length: )
  98.  
  99. private func setupBackgroundView() {
  100.  
  101. let drawLength = sqrt(self.bounds.size.width * self.bounds.size.width + self.bounds.size.height * self.bounds.size.height)
  102. backgroundView.frame = CGRectMake(, , drawLength, drawLength)
  103. backgroundView.center = CGPointMake(self.bounds.size.width / 2.0, self.bounds.size.height / 2.0)
  104. backgroundView.setNeedsDisplay()
  105. }
  106. }
  107.  
  108. // MARK: Private class : LineBackground
  109.  
  110. private class LineBackground : UIView {
  111.  
  112. private var rotate : CGFloat =
  113. private var lineWidth : CGFloat =
  114. private var lineGap : CGFloat =
  115. private var lineColor : UIColor = UIColor.grayColor()
  116.  
  117. override init(frame: CGRect) {
  118.  
  119. super.init(frame: frame)
  120. self.backgroundColor = UIColor.clearColor()
  121. }
  122.  
  123. required init?(coder aDecoder: NSCoder) {
  124.  
  125. fatalError("init(coder:) has not been implemented")
  126. }
  127.  
  128. convenience init(length : CGFloat) {
  129.  
  130. self.init(frame : CGRectMake(, , length, length))
  131. }
  132.  
  133. override func drawRect(rect: CGRect) {
  134.  
  135. super.drawRect(rect)
  136.  
  137. if self.bounds.size.width <= || self.bounds.size.height <= {
  138.  
  139. return
  140. }
  141.  
  142. let context = UIGraphicsGetCurrentContext()
  143. let width = self.bounds.size.width
  144. let height = self.bounds.size.width
  145. let drawLength = sqrt(width * width + height * height)
  146. let outerX = (drawLength - width) / 2.0
  147. let outerY = (drawLength - height) / 2.0
  148. let tmpLineWidth = lineWidth <= ? : lineWidth
  149. let tmpLineGap = lineGap <= ? : lineGap
  150.  
  151. var red : CGFloat =
  152. var green : CGFloat =
  153. var blue : CGFloat =
  154. var alpha : CGFloat =
  155.  
  156. CGContextTranslateCTM(context, 0.5 * drawLength, 0.5 * drawLength)
  157. CGContextRotateCTM(context, rotate)
  158. CGContextTranslateCTM(context, -0.5 * drawLength, -0.5 * drawLength)
  159.  
  160. lineColor.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  161. CGContextSetRGBFillColor(context, red, green, blue, alpha)
  162.  
  163. var currentX = -outerX
  164.  
  165. while currentX < drawLength {
  166.  
  167. CGContextAddRect(context, CGRectMake(currentX, -outerY, tmpLineWidth, drawLength))
  168. currentX += tmpLineWidth + tmpLineGap
  169. }
  170.  
  171. CGContextFillPath(context)
  172. }
  173. }

使用

  1. //
  2. // ViewController.swift
  3. // LineBackgroundView
  4. //
  5. // Created by YouXianMing on 16/8/14.
  6. // Copyright © 2016年 YouXianMing. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class ViewController: UIViewController {
  12.  
  13. let tmpView = BackgroundLineView(frame: CGRectMake(, , , ), lineWidth: , lineGap: ,
  14. lineColor: UIColor.blackColor().colorWithAlphaComponent(0.045), rotate: CGFloat(M_PI_4))
  15.  
  16. override func viewDidLoad() {
  17.  
  18. super.viewDidLoad()
  19.  
  20. tmpView.center = self.view.center
  21. self.view.addSubview(tmpView)
  22. }
  23. }

Swift - 绘制背景线条的更多相关文章

  1. 代码SketchPaintCode绘制

    作者:codeGlider 在我的上一篇文章中 swift10分钟实现炫酷的导航控制器跳转动画,有一个swift logo的形状 上一篇文章的动画 我说的就是中间用来做遮罩的形状. 它不是图片是用一段 ...

  2. 用Sketch和PaintCode快速得到绘制代码

    http://www.cocoachina.com/ios/20150901/13155.html 作者:codeGlider 授权本站转载. 在我的上一篇文章中 swift10分钟实现炫酷的导航控制 ...

  3. ASP.NET图形验证码的生成

    效果: 调用方法: int[] r = QAPI.VerifImage.RandomList();//取得随机数种子列 );//产生验证码字符 pictureBox1.Image = QAPI.Ver ...

  4. Swift - LineChart绘制折线图

    LineChart,就使用Core Graphics和QuartzCore框架中的CAShapeLayer绘制.这样执行效率明显比堆砌UIView的方法效率高--占用资源少,执行快. 看看CALaye ...

  5. iOS swift使用xib绘制UIView

    目标:用xib绘制一个UIView,在某个ViewController中调用. 三个文件:ViewController.Swift    DemoView.swift     DemoView.xib ...

  6. iOS绘制坐标图,折线图-Swift

    坐标图,经常会在各种各样的App中使用,最常用的一种坐标图就是折线图,根据给定的点绘制出对应的坐标图是最基本的需求.由于本人的项目需要使用折线图,第一反应就是搜索已经存在的解决方案,因为这种需求应该很 ...

  7. Swift - EasingAnimation绘制圆环动画

    Swift - EasingAnimation绘制圆环动画 效果 源码 https://github.com/YouXianMing/Swift-Animations // // CircleView ...

  8. iOS圆角view的Swift实现(利用Core Graphics绘制)

    iOS圆角view的Swift实现(利用Core Graphics绘制) 因为app的列表用用到了圆形图片的头像,所以去探究并思考了一下这个问题.首先这个问题有两个方向的解决方案: 把图片弄成圆形的. ...

  9. [Swift通天遁地]八、媒体与动画-(5)使用开源类库绘制文字、图形、图像、图表、SVG(可缩放矢量图形)

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

随机推荐

  1. DDD领域模型企业级系统Unity(五)

    添加程序集: 写一个接口: public interface IPlayer { void Play(); } 两个实现类: public class NewPlay : IPlayer { publ ...

  2. 前端架构之路:Windows下安装Nodejs步骤

      最近打算把我们的微信端用Vue.js重构,为什么选择Vue.js,一是之前使用的是传统的asp.net mvc,多页面应用用户体验比单页面要差.二是使用过Angular.js,感觉对开发人员要求较 ...

  3. 【LeetCode】74. Search a 2D Matrix

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Write an efficient algorithm that searches f ...

  4. MySQL子查询,派生表和通用表达式

    一:子查询 1.介绍 在另一个查询(外部查询)中嵌套另一个查询语句(内部查询),并使用内部查询的结果值作为外部查询条件. 2.子查询在where中 SELECT customerNumber, che ...

  5. ubuntu18.04 安装mysql不出现设置 root 帐户的密码问题(装)

    ubuntu18.04 安装mysql不出现设置 root 帐户的密码问题      https://blog.csdn.net/NeptuneClouds/article/details/80995 ...

  6. P2651 添加括号III

    P2651 添加括号III无论怎么添加,a2一定是分母,其他的可以是分子,所以最后看看,(a1*a3*..*an)%a2==0即可 #include<iostream> #include& ...

  7. 002.NTP服务端搭建

    一 安装及准备 1.1 安装NTP [root@server ~]# yum -y install ntp #也可下载之后rpm安装,或者源码安装 1.2 NTP服务地址 http://www.ntp ...

  8. [NOIp2003提高组]神经网络

    OJ题号:洛谷1038 思路:拓扑排序,注意细节.1.题目中求和运算$C_i=\displaystyle{\sum_{(j,i)\in E}W_{ji}C_j-U_i}$中$U_i$在求和运算外,只要 ...

  9. 邻接矩阵实现图的存储,DFS,BFS遍历

    图的遍历一般由两者方式:深度优先搜索(DFS),广度优先搜索(BFS),深度优先就是先访问完最深层次的数据元素,而BFS其实就是层次遍历,每一层每一层的遍历. 1.深度优先搜索(DFS) 我一贯习惯有 ...

  10. python tcp 实时抓包

    问题:之前我们系统上线后,因为是旧的系统,没有加统计的功能,比如用户喜欢那个页面,是哪些用户再访问,接口的负载能力等等. 解决办法:1,现有代码更改,添加功能.现有代码侵入太多,工作量比较大 2,想到 ...