在 Swift 中创建对角线自定义 UIView [英] Create a Diagonal Custom UIView in Swift
问题描述
我正在 swift 中设计自定义 UIimageview.我想使用类似于这个
编码应该很快.任何帮助表示赞赏.谢谢
创建一个 CAShapeLayer
并为其提供一个 path
和一个 fillColor
:
@IBDesignable公共类角度视图:UIView {@IBInspectable public var fillColor: UIColor = .blue { didSet { setNeedsLayout() } }变量点:[CGPoint] = [.零,CGPoint(x: 1, y: 0),CGPoint(x: 1, y: 1),CGPoint(x: 0, y: 0.5)] { didSet { setNeedsLayout() } }私有惰性 var shapeLayer: CAShapeLayer = {让_shapeLayer = CAShapeLayer()self.layer.insertSublayer(_shapeLayer, at: 0)返回_shapeLayer}()覆盖公共 func layoutSubviews() {shapeLayer.fillColor = fillColor.cgColor守卫点数.2 其他 {shapeLayer.path = nil返回}让路径 = UIBezierPath()path.move(to: convert(relativePoint: points[0]))对于points.dropFirst()中的点{path.addLine(to: convert(relativePoint: point))}路径.close()shapeLayer.path = path.cgPath}私有函数转换(相对点点:CGPoint)->CG点{return CGPoint(x: point.x * bounds.width + bounds.origin.x, y: point.y * bounds.height + bounds.origin.y)}}
现在,我让这个
我还使用了相对坐标(值范围从 0 到 1)并有一种方法可以将它们转换为实际坐标,但是如果需要,您可以对坐标进行硬编码.但是使用它作为从 0 到 1 的值,您将拥有一个可以参与自动布局的角度视图,而无需担心更改特定坐标值.
最后,一些看似微不足道的小事,但我在 layoutSubviews
中构建了 path
:这样,随着视图大小的变化(无论是通过自动布局还是编程更改),视图将被正确重新渲染.同样,通过对 fillColor
和 points
使用 didSet
,如果您更改其中任何一个,视图将重新渲染.>
您可以随意更改此设置,但希望这说明了具有自定义 path
的 CAShapeLayer
的基本思想.
如果你使用insertSublayer
,你可以把它和AngleView的其他子视图结合起来,例如:
I am working on designing custom UIimageview in swift. I want to create a UIimageview using beizerpath similar to this
The coding should be in swift. Any help is appreciated. Thanks
Create a CAShapeLayer
and supply it a path
and a fillColor
:
@IBDesignable
public class AngleView: UIView {
@IBInspectable public var fillColor: UIColor = .blue { didSet { setNeedsLayout() } }
var points: [CGPoint] = [
.zero,
CGPoint(x: 1, y: 0),
CGPoint(x: 1, y: 1),
CGPoint(x: 0, y: 0.5)
] { didSet { setNeedsLayout() } }
private lazy var shapeLayer: CAShapeLayer = {
let _shapeLayer = CAShapeLayer()
self.layer.insertSublayer(_shapeLayer, at: 0)
return _shapeLayer
}()
override public func layoutSubviews() {
shapeLayer.fillColor = fillColor.cgColor
guard points.count > 2 else {
shapeLayer.path = nil
return
}
let path = UIBezierPath()
path.move(to: convert(relativePoint: points[0]))
for point in points.dropFirst() {
path.addLine(to: convert(relativePoint: point))
}
path.close()
shapeLayer.path = path.cgPath
}
private func convert(relativePoint point: CGPoint) -> CGPoint {
return CGPoint(x: point.x * bounds.width + bounds.origin.x, y: point.y * bounds.height + bounds.origin.y)
}
}
Now, I made this designable (so if you put it in a separate framework target, you can add this view right in your storyboard and see it rendered there). It still works if you're not using storyboards. It just can be convenient to do so:
I also used relative coordinates (with values ranging from zero to one) and have a method to convert those to actual coordinates, but you can hard code your coordinates if you want. But using this as values from zero to one, you have an angular view that can participate in auto-layout without needing to worry about changing specific coordinate values.
Finally, minor things that might seem trivial, but I construct the path
in layoutSubviews
: That way, as the view changes size (whether via auto-layout or programmatic changes), the view will be correctly re-rendered. Likewise, by using didSet
for fillColor
and points
, if you change either of those, the view will be re-rendered for you.
Feel free to change this as you see fit, but hopefully this illustrates the basic idea of just having a CAShapeLayer
with a custom path
.
If you use insertSublayer
, you can then combine this with other subviews of the AngleView, e.g.:
这篇关于在 Swift 中创建对角线自定义 UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!