博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Swift - 绘制背景线条
阅读量:4599 次
发布时间:2019-06-09

本文共 5251 字,大约阅读时间需要 17 分钟。

Swift - 绘制背景线条

 

效果

 

源码

////  BackgroundLineView.swift//  LineBackgroundView////  Created by YouXianMing on 16/8/14.//  Copyright © 2016年 YouXianMing. All rights reserved.//import UIKit// MARK: Public class : BackgroundLineViewclass 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: 0) 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(0, 0, drawLength, drawLength) backgroundView.center = CGPointMake(self.bounds.size.width / 2.0, self.bounds.size.height / 2.0) backgroundView.setNeedsDisplay() }}// MARK: Private class : LineBackgroundprivate class LineBackground : UIView { private var rotate : CGFloat = 0 private var lineWidth : CGFloat = 5 private var lineGap : CGFloat = 3 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(0, 0, length, length)) } override func drawRect(rect: CGRect) { super.drawRect(rect) if self.bounds.size.width <= 0 || self.bounds.size.height <= 0 { 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 <= 0 ? 5 : lineWidth let tmpLineGap = lineGap <= 0 ? 3 : lineGap var red : CGFloat = 0 var green : CGFloat = 0 var blue : CGFloat = 0 var alpha : CGFloat = 0 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 UIKitclass ViewController: UIViewController {    let tmpView = BackgroundLineView(frame: CGRectMake(0, 0, 300, 300), lineWidth: 4, lineGap: 4,                                     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)    }}

 

转载于:https://www.cnblogs.com/YouXianMing/p/5769947.html

你可能感兴趣的文章
Android 状态栏通知Notification、NotificationManager详解
查看>>
Sublime Text 3中使用正则表达式删除空行
查看>>
UIApplicationDelegate协议
查看>>
再谈iOS 7的手势滑动返回功能
查看>>
Jmeter测试dubbo接口填坑
查看>>
python小练——找出指定目录下小于指定字节的文件,输出到文本文件
查看>>
渐渐磨砺--16年11月封闭总结
查看>>
[zz]GDB调试精粹及使用实例
查看>>
数据库的创建和删除
查看>>
【消息队列MQ】各类MQ比较
查看>>
最简单的三层实例【插入据
查看>>
设计模式学习笔记——Prototype原型模式
查看>>
pom.xml里有红叉报错的解决办法
查看>>
Perl last和next的用法区别
查看>>
Selenium 管理 Cookies
查看>>
exceptionfunction[LeetCode]Permutations
查看>>
Linux(2)_常用命令2
查看>>
自定义分页
查看>>
[转]DELPHI——调试(1)
查看>>
JS秒数转成分秒时间格式
查看>>