Swift - 绘制背景线条
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 - 绘制背景线条的更多相关文章
- 代码SketchPaintCode绘制
作者:codeGlider 在我的上一篇文章中 swift10分钟实现炫酷的导航控制器跳转动画,有一个swift logo的形状 上一篇文章的动画 我说的就是中间用来做遮罩的形状. 它不是图片是用一段 ...
- 用Sketch和PaintCode快速得到绘制代码
http://www.cocoachina.com/ios/20150901/13155.html 作者:codeGlider 授权本站转载. 在我的上一篇文章中 swift10分钟实现炫酷的导航控制 ...
- ASP.NET图形验证码的生成
效果: 调用方法: int[] r = QAPI.VerifImage.RandomList();//取得随机数种子列 );//产生验证码字符 pictureBox1.Image = QAPI.Ver ...
- Swift - LineChart绘制折线图
LineChart,就使用Core Graphics和QuartzCore框架中的CAShapeLayer绘制.这样执行效率明显比堆砌UIView的方法效率高--占用资源少,执行快. 看看CALaye ...
- iOS swift使用xib绘制UIView
目标:用xib绘制一个UIView,在某个ViewController中调用. 三个文件:ViewController.Swift DemoView.swift DemoView.xib ...
- iOS绘制坐标图,折线图-Swift
坐标图,经常会在各种各样的App中使用,最常用的一种坐标图就是折线图,根据给定的点绘制出对应的坐标图是最基本的需求.由于本人的项目需要使用折线图,第一反应就是搜索已经存在的解决方案,因为这种需求应该很 ...
- Swift - EasingAnimation绘制圆环动画
Swift - EasingAnimation绘制圆环动画 效果 源码 https://github.com/YouXianMing/Swift-Animations // // CircleView ...
- iOS圆角view的Swift实现(利用Core Graphics绘制)
iOS圆角view的Swift实现(利用Core Graphics绘制) 因为app的列表用用到了圆形图片的头像,所以去探究并思考了一下这个问题.首先这个问题有两个方向的解决方案: 把图片弄成圆形的. ...
- [Swift通天遁地]八、媒体与动画-(5)使用开源类库绘制文字、图形、图像、图表、SVG(可缩放矢量图形)
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
随机推荐
- fhq treap 学习笔记
序 今天心血来潮,来学习一下fhq treap(其实原因是本校有个OIer名叫fh,当然不是我) 简介 fhq treap 学名好像是"非旋转式treap及可持久化"...听上去怪 ...
- hdu 5441 (2015长春网络赛E题 带权并查集 )
n个结点,m条边,权值是 从u到v所花的时间 ,每次询问会给一个时间,权值比 询问值小的边就可以走 从u到v 和从v到u算不同的两次 输出有多少种不同的走法(大概是这个意思吧)先把边的权值 从小到大排 ...
- PHP并发操作下的加锁
最近做后台统计的工具,统计肯定是一个需要运算好久的数据 容许一个用户在一个时间内进行操作,这个时候就需要用到锁了,将这个操作过程锁起来.在用了cache的时候,cache失效可能导致瞬间的多数并发请求 ...
- 【AtCoder】ARC087
C - Good Sequence 题解 用个map愉悦一下就好了 代码 #include <bits/stdc++.h> #define fi first #define se seco ...
- 005.iSCSI客户端配置示例-Windows
一 环境 Linux作为iSCSI服务端,Windows2008R2作为iSCSI客户端 二 服务端配置过程 2.1 客户端配置 在Linux上参照之前的配置建立三个LUN卷作为共享盘,最终配置如下: ...
- jquery 点击页面流畅弹出预定文字
js代码: <script src="https://cdn.bootcss.com/jquery/2.2.1/jquery.min.js"></script&g ...
- 初识thinkphp(5)
这次主要内容是模型的基本操作 0x01:什么是模型 通过手册的阅览,笼统的说就是,把打开数据库等操作在另一个php文件中进行 以及对变量的规则具体细节,查询,取值等操作进行定义,方便在控制器中直接使用 ...
- WEP自动破解工具wesside-ng
WEP自动破解工具wesside-ng wesside-ng是aircrack-ng套件提供的一个概念验证工具.该工具可以自动扫描无线网络,发现WEP加密的AP.然后,尝试关联该AP.关联成功后, ...
- C#开发Unity游戏教程循环遍历做出判断及Unity游戏示例
C#开发Unity游戏教程循环遍历做出判断及Unity游戏示例 Unity中循环遍历每个数据,并做出判断 很多时候,游戏在玩家做出判断以后,游戏程序会遍历玩家身上大量的所需数据,然后做出判断,即首先判 ...
- mac那些事儿
OS是苹果公司开发的电脑操作系统,MAC是苹果公司开发的笔记本.台式机.IOS是苹果公司开发的移动操作系统,iPhone是苹果公司研发的智能手机系列,搭载IOS操作系统. 一.mac系统快捷键 回到桌 ...