装饰视图,用于装饰内容视图,KBDecorationView主要的功能是提供一个装饰框,可以指定装饰框型的形状,通过蒙版来实现,绘制蒙版的形状即可指定装饰框的形状
[TOC]
在Podfile
文件中加入
pod 'KBDecorationView', '~> 1.5.0'
使用非常简单
let myView = ...
let decorationView = KBDecorationView(contentView: myView)
view.addSubview(decorationView)
KBDecorationView使用AutoLayout
设置内容的内边距
decorationView.contentInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10);
或者在初始化时指定内边距
let decorationView = KBDecorationView(contentView: myView, contentInsets: UIEdgeInsets(top: 10, left: 15, bottom: 10, right: 15)
默认的装饰框是一个圆角矩形,有两种方式来自定义装饰框的形状
第一种是继承KBDecorationView,然后重写setupMaskLayer(_:)
方法
class MyView: KBDecorationView {
var cornerRadius: CGFloat = 8
override func setupMaskLayer(_ maskLayer: CAShapeLayer) {
maskLayer.path = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).cgPath
}
}
第二种方法是通过maskLayerSetupHandler
闭包,maskLayerSetupHandler
的优先级比setupMaskLayer
优先级高
decorationView.maskLayerSetupHandler = { (maskLayer) in
maskLayer.path = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).cgPath;
}
此项目采用MIT开源协议,点击查看详情