工程日记之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 ...
随机推荐
- 026、MySQL取字符串左边,取字符串右边,取字符串中间,取文本开始位置
#取文本左边 ); #田 ); #田攀 ); #田攀5 #取文本右边 ); # ); # ); #攀52 #取文本中间 '); #田攀 '); #攀5 #从字符串s中获取s1的开始位置 不忘初心,如果 ...
- 09.swoole学习笔记--创建进程
<?php //进程数组 $workers=[]; //创建进程的数据量 $worker_num=; //创建启动进程 ;$i<$worker_num;$i++){ //创建单独新进程 $ ...
- 八、JavaScript之执行语句
一.代码如下 二.运行结果如下 <!DOCTYPE html> <html> <meta http-equiv="Content-Type" cont ...
- 153-PHP htmlentities函数
<?php //定义一个HTML代码字符串 $str=<<<HTM <a href=#><b><i>到一个网址的链接</i>&l ...
- 116-PHP调用类成员函数
<?php class ren{ //定义人类 public function walk(){ //定义人类的成员方法 echo '我会走路.'; } } $ren=new ren(); //实 ...
- Spring 实战4学习笔记(转)
http://blog.csdn.net/21aspnet/article/details/51386557 1.IOC装配Bean 参考[spring实战4 2.2],作者提倡无XML配置化. 1. ...
- FFT各种模板
丑陋敬请谅解: 求两列数的卷积: 递归版: #include <stdio.h> #include <algorithm> #include <math.h> us ...
- 修改element-ui里table中悬浮框中三角号的颜色及透明度设置
.el-tooltip__popper,.el-tooltip__popper.is-dark{background:rgba(0,0,0,0.6) !important;} .el-tooltip_ ...
- mysql 索引使用教程
1.什么索引 索引是一种特殊的文件(InnoDB数据表上的索引是表空间的一个组成部分),它们包含着对数据表里所有记录的位置信息.更通俗的说,数据库索引好比是一本书前面的目录,能加快数据库的查询速度. ...
- [BJDCTF2020]ZJCTF,不过如此
0x00 知识点 本地文件包含伪协议 ?text=php://input //执行 post: I have a dream ?file=php://filter/read/convert.base6 ...