工程日记之HelloSlide(1):Swift自定义可视化组件的方法(继承UIView和在StoryBoard中设置)
需求描述
HelloSlide是把文本自动转化成幻灯片的软件,在幻灯片中我们有SmartArt:各种各样的几何形状,并且可以自定义大小和颜色,放在幻灯片不同的位置。
为了在我们的软件中实现类似的效果,我封装了一些自定义的组件,因为暂时只需要几何形状,我通过直接继承UIView来实现
代码
class ArcView:UIView{
var mystrokecolor:UIColor //设置笔触颜色
var color : UIColor //设置填充颜色
init(frame:CGRect,color:UIColor,strokecolor:UIColor){
self.mystrokecolor = strokecolor
self.color = color
super.init(frame:frame)
self.backgroundColor = UIColor.clear //要将背景色设置为透明
}
required init(coder acoder:NSCoder) {
fatalError("some error in Xcode")
}
override func draw(_ rect: CGRect){
super.draw(rect)
guard let context = UIGraphicsGetCurrentContext() else{
return
}
let path = CGMutablePath()
path.move(to: CGPoint(x:self.bounds.minX,y:self.bounds.minY)) //设置起点
path.addLine(to: CGPoint(x:self.bounds.maxX,y:self.bounds.minY))
path.addLine(to: CGPoint(x:self.bounds.maxX,y:self.bounds.maxY))
path.addLine(to: CGPoint(x:self.bounds.minX,y:self.bounds.maxY))
path.addLine(to: CGPoint(x:self.bounds.minX,y:self.bounds.minY))
//完成path的绘制
//path.addRect(T##rect: CGRect##CGRect)
context.addPath(path)
// 设置笔触样式
context.setStrokeColor(mystrokecolor.cgColor)
context.setLineWidth()
context.setFillColor(color.cgColor)
context.strokePath()
}
}
解释
1 用的Core Graphics框架。Core Graphics提供CGMutablePath的底层API,而UIBezierPath事实上是对CGMutablePath的封装
2 我们使用的 UIKit 库中所有 UI 组件其实都是由 CoreGraphics 绘制实现的。所以使用 Core Graphics 可以实现比 UIKit 更底层的功能。
3 我们开放了两个属性:笔触颜色和填充颜色,用来自定义颜色;其他的如空间大小等可以直接继承自UIView中的属性
深度需求探索
如果我们希望把这个组件放到Storyboard上面,需要做一点调整
@IBDesignable:用来标识自定义组件类,这样在StoryBoard中就能能实时更新视图。
@IBInspectable:用来标识属性,这样在Attribute Inspector(属性检查器)中查看设置该属性。
代码
class ArcView:UIView{
var mystrokecolor:UIColor
//设置笔触颜色
var color : UIColor //设置填充颜色
init(frame:CGRect,color:UIColor,strokecolor:UIColor){
self.mystrokecolor = strokecolor
self.color = color
super.init(frame:frame)
self.backgroundColor = UIColor.clear //要将背景色设置为透明
}
required init(coder acoder:NSCoder) {
fatalError("some error in Xcode")
}
override func draw(_ rect: CGRect){
super.draw(rect)
guard let context = UIGraphicsGetCurrentContext() else{
return
}
let path = CGMutablePath()
path.move(to: CGPoint(x:self.bounds.minX,y:self.bounds.minY)) //设置起点
path.addLine(to: CGPoint(x:self.bounds.maxX,y:self.bounds.minY))
path.addLine(to: CGPoint(x:self.bounds.maxX,y:self.bounds.maxY))
path.addLine(to: CGPoint(x:self.bounds.minX,y:self.bounds.maxY))
path.addLine(to: CGPoint(x:self.bounds.minX,y:self.bounds.minY))
//完成path的绘制
//path.addRect(T##rect: CGRect##CGRect)
context.addPath(path)
// 设置笔触样式
context.setStrokeColor(mystrokecolor.cgColor)
context.setLineWidth()
context.setFillColor(color.cgColor)
context.strokePath()
}
}
// 把上述这个view放到一个IBDesignable类型的class中去
@IBDesignable class myview : UIView{
private var thisview : ArcView()
@IBInspectable var strokecolor : UIColor = .white{
didSet{
self.thisview.strokeColor = strokecolor;
}
}
另外
initWithFrame:和layoutSubviews中添加子控件和设置子控件尺寸的步骤,还有在view controller里面设置view的frame,因为添加子控件和设置子控件的尺寸以及整个view的尺寸在xib中就已经完成。(注意整个view的位置还没有设置,需要在controller里面设置。)参考:https://www.hangge.com/blog/cache/detail_1394.html
https://www.jianshu.com/p/7e47da62899c
工程日记之HelloSlide(1):Swift自定义可视化组件的方法(继承UIView和在StoryBoard中设置)的更多相关文章
- Swift - 继承UIView实现自定义可视化组件(附记分牌样例)
在iOS开发中,如果创建一个自定义的组件通常可以通过继承UIView来实现.下面以一个记分牌组件为例,演示了组件的创建和使用,以及枚举.协议等相关知识的学习. 效果图如下: 组件代码:Score ...
- Swift自定义UINavigationController(背景颜色、背景图片、返回按钮设置、字体大小等)
1.0 自定义UINavigationController时,背景图片.颜色等只需要设置一次,所以我们可以重写 initializa 这个方法来实现我们想要的效果 override class ...
- 工程日记之HelloSlide(3):如何使用Core Data数据库,以及和sqlite之间的对应关系
Core Data 和 SQLite 是什么关系 core data是对sqlite的封装,因为sqlite是c语言的api,然而有人也需要obj-c的api,所以有了core data ,另外,co ...
- 工程日记之HelloSlide(2) : UITextView中如何根据给定的长宽,计算最合适的字体大小
需求描述 一般的需求是将UITextview的大小自适应文本高度,会做出随文本内容增加,文字框不断增大的效果: 本文反其道而行之,在给定文字框大小的情况下:字数越多,字体越小: 需求来源: 考虑将文字 ...
- 【转】Android学习基础自定义Checkbox组件
原文网址:http://forum.maiziedu.com/thread-515-1-1.html heckbox组件是一种可同时选中多项的基础控件,即复选框,在android学习中,Checkbo ...
- iOS中 xib自定义View在storyboard中的使用
1,创建UIView 的SubClass 命名为MyView 2, new一个名为MyView的xib p1 3,配置xib的属性 p2 4,为View 添加背景色,添加一个按钮并定制按钮约束,这里我 ...
- drf三大认证:认证组件-权限组件-权限六表-自定义认证组件的使用
三大认证工作原理简介 认证.权限.频率 源码分析: from rest_framework.views import APIView 源码分析入口: 内部的三大认证方法封装: 三大组件的原理分析: 权 ...
- [IOS]swift自定义uicollectionviewcell
刚刚接触swift以及ios,不是很理解有的逻辑,导致某些问题.这里分享一下swift自定义uicollectionviewcell 首先我的viewcontroller不是直接继承uicollect ...
- swift 自定义弹框
// // ViewController.swift // animationAlert // // Created by su on 15/12/9. // Copyright © 2015 ...
随机推荐
- Day 4 -E - Catenyms POJ - 2337
A catenym is a pair of words separated by a period such that the last letter of the first word is th ...
- Day4 - D - Watchcow POJ - 2230
Bessie's been appointed the new watch-cow for the farm. Every night, it's her job to walk across the ...
- pyhton matplotlib可视化图像基础(二维函数图、柱状图、饼图、直方图以及折线图)
//2019.07.22pyhton中matplotlib模块的应用pyhton中matplotlib是可视化图像库的第三方库,它可以实现图像的可视化,输出不同形式的图形1.可视化图形的输出和展示需要 ...
- P1045 快速排序
P1045 快速排序 转跳点:
- 6 —— node —— 响应一个完整的页面
const http = require('http'); const fs = require('fs'); const server = http.createServer(); server ...
- eclipse中使用jstl
错误提示为"can not find the tag library for http://java.sun.com/jsp/jstl/core" 这是我在练习把axis2和普通j ...
- swoole在线聊天学习笔记
<?php $http=); $http->on('request',function(swoole_http_request $request,swoole_http_response ...
- 138-PHP static后期静态绑定(一)
<?php class test{ //创建test类 public function __construct(){ self::getinfo(); //后期静态绑定 } public sta ...
- XML 之 语法详解
一.文档规则 .区分大小写. .属性值必须加引号(单引号.双引号都可以),一般情况下建议使用使用双引号. .所有标记必须有结束符号. .所有空标记必须关闭. .必须有且仅有一根元素. .解析空白字符时 ...
- Python 内置类型 dict, list,线程安全吗
近段时间发现一个 Python 连接数据库的连接是线程不安全的,结果惹得我哪哪儿都怀疑变量的多线程是否安全的问题,今天终于找到了正确答案,那就是 Python 内置类型 dict,list ,tupl ...