工程日记之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 ...
随机推荐
- python 字符串实例:检查并判断密码字符串的安全强度
检查并判断密码字符串的安全强度 import string def check(pwd): #密码必须至少包含六个字符 if not isinstance(pwd,str) or len(pwd)&l ...
- Spring 配置Beans
<bean id="UserDao" class="com.jikexueyuan.dao.impl.UserDaoImpl"> <prope ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-fast-forward
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- windows下修改pip安装源的办法
之前的随笔里有写过关于Mac OS和Linux的,现在需要用到Windows的系统, 修改方法:路径----> C:\Users\用户名\AppData\Roaming,在Roaming文件夹下 ...
- P1083 是否存在相等的差
P1083 是否存在相等的差 转跳点:
- POJ2392:Space Elevator
Space Elevator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9244 Accepted: 4388 De ...
- JS 三大难点
1,作用域链 2,原型链 3,闭包
- Vulkan SDK 之Render Pass
Create a Render Pass A render pass describes the scope of a rendering operation by specifying the co ...
- 136-PHP 使用类名访问static修饰的类方法
<?php class test{ //定义一个类 public static function class_info(){ //定义类方法 return '这是一个用于测试的类.'; } } ...
- eclipse中svn重新设置账户
查看svn版本:windows > preference > Team > SVN 1.如果svn插件是svnkit版 只需找到.keyring文件,一般目录是:eclipse安装目 ...