J'essaie de créer une vue qui inclut un calque de forme, qui peut être prévisualisé dans Xcode. Je n'arrive pas à le faire fonctionner complètement même si la plupart fonctionnent. Spécifiquement je ne peux pas comprendre comment je peux placer la propriété de course de la couche de forme puisqu'elle doit être placée avant que la couche soit ajoutée autrement aucun trait n'est rendu. Mais si je l'ai mis avant, la propriété inspectable ne fonctionne plus. Voici jusqu'où je suis:Aperçu CAShapeLayer utilisant IBDesignable et IBInspectable
import Foundation
import UIKit
@IBDesignable
class CustomView: UIView
{
@IBInspectable var layerBackgroundColor: UIColor? {
didSet {
if let actualColor = layerBackgroundColor {
layer.backgroundColor = actualColor.cgColor
} else {
layerBackgroundColor = nil
}
}
}
@IBInspectable var strokeColor: UIColor? = UIColor.red {
didSet {
if let actualColor = strokeColor {
self.shapeLayer.strokeColor = actualColor.cgColor
} else {
shapeLayer.strokeColor = nil
}
}
}
private var shapeLayer: CAShapeLayer
override func prepareForInterfaceBuilder()
{
let width = self.bounds.width
let height = self.bounds.height
shapeLayer = CAShapeLayer()
shapeLayer.frame = CGRect(x: 0, y: 0, width: width, height: height)
let path = CGMutablePath()
let middle = width/2
path.move(to:CGPoint(x:middle, y:20))
path.addLine(to:CGPoint(x:middle, y:100))
layerBackgroundColor = UIColor.gray
// If this is not set no stroke will be rendered!
strokeColor = UIColor.red
shapeLayer.path = path
shapeLayer.fillColor = nil
shapeLayer.lineJoin = kCALineJoinRound
shapeLayer.lineCap = kCALineCapRound
shapeLayer.lineWidth = 5
layer.addSublayer(shapeLayer)
}
override init(frame: CGRect)
{
shapeLayer = CAShapeLayer()
super.init(frame:frame)
}
required init?(coder aDecoder: NSCoder)
{
fatalError("init(coder:) has not been implemented")
}
}
Thx (il était très déjà en retard ..)! – Nils