2017-08-02 1 views
0

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") 
    } 
} 

Répondre

1

Vous créez une nouvelle ShapeLayer à l'intérieur prepareForInterfaceBuilder, de sorte que vous devez définir les propriétés de couleur de la ShapeLayer dans cette méthode. Les variables inspectables sont assignées après l'initialisation, mais avant prepareForInterfaceBuilder, la création d'un nouveau CAShapeLayer dans prepareForInterfaceBuilder est la suppression de vos affectations précédentes. Je suppose en pratique que vous allez faire le dessin personnalisé pour votre classe dans une autre méthode (puisque cette méthode n'est jamais appelée en dehors de IB).

Quoi qu'il en soit, le code suivant devrait résoudre vos problèmes avec IB, j'ai fait quatre changements, pour l'aide, ne pouvait pas comprendre cela plus hier .... Les évaluations soulignaient toutes

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? {//1 - Maybe you want to comment out assignment to UIColor.red: = 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)) 

     //2 - you probably also want to set the shapeLayer's background color to the class variable, not hardcode the class variable 
     //layerBackgroundColor = UIColor.gray 
     shapeLayer.backgroundColor = layerBackgroundColor?.cgColor 


     // If this is not set no stroke will be rendered! 
     //3 Comment the hardcoding line out 
     //strokeColor = UIColor.red 
     //4 - assign the shapeLayer's stroke color 
     shapeLayer.strokeColor = strokeColor?.cgColor 

     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") 
    } 
} 
+0

Thx (il était très déjà en retard ..)! – Nils