需求描述

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;

  }

}

另外

但是如果组件里的元素比较多,布局比较复杂。那用纯代码写就比较麻烦了。对于这种复杂的自定义组件,我们可以结合 XIB 文件来实现。

使用XIB的方式可以省去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中设置)的更多相关文章

  1. Swift - 继承UIView实现自定义可视化组件(附记分牌样例)

    在iOS开发中,如果创建一个自定义的组件通常可以通过继承UIView来实现.下面以一个记分牌组件为例,演示了组件的创建和使用,以及枚举.协议等相关知识的学习. 效果图如下:    组件代码:Score ...

  2. Swift自定义UINavigationController(背景颜色、背景图片、返回按钮设置、字体大小等)

    1.0  自定义UINavigationController时,背景图片.颜色等只需要设置一次,所以我们可以重写  initializa  这个方法来实现我们想要的效果 override  class ...

  3. 工程日记之HelloSlide(3):如何使用Core Data数据库,以及和sqlite之间的对应关系

    Core Data 和 SQLite 是什么关系 core data是对sqlite的封装,因为sqlite是c语言的api,然而有人也需要obj-c的api,所以有了core data ,另外,co ...

  4. 工程日记之HelloSlide(2) : UITextView中如何根据给定的长宽,计算最合适的字体大小

    需求描述 一般的需求是将UITextview的大小自适应文本高度,会做出随文本内容增加,文字框不断增大的效果: 本文反其道而行之,在给定文字框大小的情况下:字数越多,字体越小: 需求来源: 考虑将文字 ...

  5. 【转】Android学习基础自定义Checkbox组件

    原文网址:http://forum.maiziedu.com/thread-515-1-1.html heckbox组件是一种可同时选中多项的基础控件,即复选框,在android学习中,Checkbo ...

  6. iOS中 xib自定义View在storyboard中的使用

    1,创建UIView 的SubClass 命名为MyView 2, new一个名为MyView的xib p1 3,配置xib的属性 p2 4,为View 添加背景色,添加一个按钮并定制按钮约束,这里我 ...

  7. drf三大认证:认证组件-权限组件-权限六表-自定义认证组件的使用

    三大认证工作原理简介 认证.权限.频率 源码分析: from rest_framework.views import APIView 源码分析入口: 内部的三大认证方法封装: 三大组件的原理分析: 权 ...

  8. [IOS]swift自定义uicollectionviewcell

    刚刚接触swift以及ios,不是很理解有的逻辑,导致某些问题.这里分享一下swift自定义uicollectionviewcell 首先我的viewcontroller不是直接继承uicollect ...

  9. swift 自定义弹框

    // //  ViewController.swift //  animationAlert // //  Created by su on 15/12/9. //  Copyright © 2015 ...

随机推荐

  1. 小程序导航组件navigator活学活用

    小程序开发中必不可少的组件navigator,虽然使用频率非常高,但是却没多少人能灵活运用. 先说navigator组件的用处: 它的主要用处是跳转执行,跳转可分为当前页面内跳转.前往页面外部的跳转. ...

  2. android中的简单animation(三)accelerate(加速),decelerate(减速),anticipate,overshoot,bounce

    animation_3.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...

  3. [Codeforces]1263B PIN Code

    题目 A PIN code is a string that consists of exactly 444 digits. Examples of possible PIN codes: 70137 ...

  4. Day7 - K - Biorhythms POJ - 1006

    Some people believe that there are three cycles in a person's life that start the day he or she is b ...

  5. 034、Java中自增之++在前面的写法

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  6. mysql concat与concat_ws区别

    select concat('大','小') as size from 表 查询出结果为:大小 select concat('大',NULL) as size from 表 查询出结果为:null c ...

  7. leetcode1019 Next Greater Node In Linked List

    """ We are given a linked list with head as the first node. Let's number the nodes in ...

  8. 字符设备驱动之LED驱动

    实现 ①编写驱动框架 ②编写硬件实现代码 (在Linux系统下操作硬件,需要操作虚拟地址,因此需要先把物理地址转换为虚拟地址 ioremap()) 如何实现单个灯的操作: 实现方法之一--操作次设备号 ...

  9. Centos7搭建SVN服务

    1.安装 subversion ​[root@CentOS var]# yum -y install subversion 2.创建 svn 版本库,初始化相关配置文件 ​[root@CentOS v ...

  10. websocket与http

    偶然在知乎上看到一篇回帖,瞬间觉得之前看的那么多资料都不及这一篇回帖让我对 websocket 的认识深刻有木有.所以转到我博客里,分享一下.比较喜欢看这种博客,读起来很轻松,不枯燥,没有布道师的阵仗 ...