Swift - 绘制背景线条

效果

源码

//
// BackgroundLineView.swift
// LineBackgroundView
//
// Created by YouXianMing on 16/8/14.
// Copyright © 2016年 YouXianMing. All rights reserved.
// import UIKit // MARK: Public class : BackgroundLineView class BackgroundLineView: UIView { // MARK: Properties. /// Line width, default is 5.
var lineWidth : CGFloat { get {return backgroundView.lineWidth} set(newVal) { backgroundView.lineWidth = newVal
backgroundView.setNeedsDisplay()
}
} /// Line gap, default is 3.
var lineGap : CGFloat { get {return backgroundView.lineGap} set(newVal) { backgroundView.lineGap = newVal
backgroundView.setNeedsDisplay()
}
} /// Line color, default is grayColor.
var lineColor : UIColor { get {return backgroundView.lineColor} set(newVal) { backgroundView.lineColor = newVal
backgroundView.setNeedsDisplay()
}
} /// Rotate value, default is 0.
var rotate : CGFloat { get {return backgroundView.rotate} set(newVal) { backgroundView.rotate = newVal
backgroundView.setNeedsDisplay()
}
} convenience init(frame: CGRect, lineWidth : CGFloat, lineGap : CGFloat, lineColor : UIColor, rotate : CGFloat) { self.init(frame : frame)
self.lineWidth = lineWidth
self.lineGap = lineGap
self.lineColor = lineColor
self.rotate = rotate
} // MARK: Override system method. override func layoutSubviews() { super.layoutSubviews()
setupBackgroundView()
} override init(frame: CGRect) { super.init(frame: frame) self.layer.masksToBounds = true
self.addSubview(backgroundView)
} required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented")
} // MARK: Private value & func. private let backgroundView = LineBackground(length: ) private func setupBackgroundView() { let drawLength = sqrt(self.bounds.size.width * self.bounds.size.width + self.bounds.size.height * self.bounds.size.height)
backgroundView.frame = CGRectMake(, , drawLength, drawLength)
backgroundView.center = CGPointMake(self.bounds.size.width / 2.0, self.bounds.size.height / 2.0)
backgroundView.setNeedsDisplay()
}
} // MARK: Private class : LineBackground private class LineBackground : UIView { private var rotate : CGFloat =
private var lineWidth : CGFloat =
private var lineGap : CGFloat =
private var lineColor : UIColor = UIColor.grayColor() override init(frame: CGRect) { super.init(frame: frame)
self.backgroundColor = UIColor.clearColor()
} required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented")
} convenience init(length : CGFloat) { self.init(frame : CGRectMake(, , length, length))
} override func drawRect(rect: CGRect) { super.drawRect(rect) if self.bounds.size.width <= || self.bounds.size.height <= { return
} let context = UIGraphicsGetCurrentContext()
let width = self.bounds.size.width
let height = self.bounds.size.width
let drawLength = sqrt(width * width + height * height)
let outerX = (drawLength - width) / 2.0
let outerY = (drawLength - height) / 2.0
let tmpLineWidth = lineWidth <= ? : lineWidth
let tmpLineGap = lineGap <= ? : lineGap var red : CGFloat =
var green : CGFloat =
var blue : CGFloat =
var alpha : CGFloat = CGContextTranslateCTM(context, 0.5 * drawLength, 0.5 * drawLength)
CGContextRotateCTM(context, rotate)
CGContextTranslateCTM(context, -0.5 * drawLength, -0.5 * drawLength) lineColor.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
CGContextSetRGBFillColor(context, red, green, blue, alpha) var currentX = -outerX while currentX < drawLength { CGContextAddRect(context, CGRectMake(currentX, -outerY, tmpLineWidth, drawLength))
currentX += tmpLineWidth + tmpLineGap
} CGContextFillPath(context)
}
}

使用

//
// ViewController.swift
// LineBackgroundView
//
// Created by YouXianMing on 16/8/14.
// Copyright © 2016年 YouXianMing. All rights reserved.
// import UIKit class ViewController: UIViewController { let tmpView = BackgroundLineView(frame: CGRectMake(, , , ), lineWidth: , lineGap: ,
lineColor: UIColor.blackColor().colorWithAlphaComponent(0.045), rotate: CGFloat(M_PI_4)) override func viewDidLoad() { super.viewDidLoad() tmpView.center = self.view.center
self.view.addSubview(tmpView)
}
}

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. AC自动机算法学习

    KMP+TRIE int val[1000100][31],tot; int tr[1000100]; int fail[1000100]; struct AC_Trie{ void clean(){ ...

  2. AndroidStudio3.0以上版本的坑

    原文:https://blog.csdn.net/ytfunnysite/article/details/78864556 1.Error:Failed to resolve: com.android ...

  3. java okhttp 发送图片

    @RequestMapping(value="/demo2", method=RequestMethod.POST) @ResponseBody public String dem ...

  4. 001.FTP简介及相关文件

    一 FTP简介 FTP(File Transfer Protocol)文件传输协议,用于Internet上控制文件的双向传输. 下载:远程主机拷贝文件至本地: 上传:本地主机拷贝文件至远程. 二 FT ...

  5. hdu 3033(好题,分组背包)

    I love sneakers! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. INSTALL_FAILED_CONFLICTING_PROVIDER

    主要是由于调试的环境中已有一个同名的Provider存在. 解决方法是修改AndroidManifest.xml中的 <provider android:name=".apps.App ...

  7. 懒人的福利?教你用set维护斜率优化凸包

    斜率优化题目大家肯定都做得不少了,有一些题目查询插入点的x坐标和查询斜率都不单调,这样就需要维护动态凸包并二分斜率.(例如bzoj1492) 常规的做法是cdq分治或手写平衡树维护凸包,然而如果我不愿 ...

  8. nodejs备忘总结(一) -- node和express安装与配置,新建简单项目(附安装配置过程中遇到问题的解决方法)

    安装node 本文以安装node_v8.9.0为例(win10环境),下载node-v8.9.0-x64.msi插件 下载后,安装,安装目录默认为C:\Program Files\nodejs 配置环 ...

  9. Vue项目开发之打包后背景图片路径错误的坑

    在开发vue项目的过程中,使用浏览器进行预览的时候所有图片的路径是没有任何问题的,但是在打包后传到服务器上,在微信端查看背景图片时,background的图片竟然不显示,img标签里的图片却是正常展示 ...

  10. ARM 汇编与C调用的若干问题(一般函数调用情况)

    ARM 汇编与C之间的函数调用需要符合ATPCS,建议函数的形参不超过4个,如果形参个数少于或等于4,则形参由R0,R1,R2,R3四个寄存器进行传递:若形参个数大于4,大于4的部分必须通过堆栈进行传 ...