Swiftpack.co - p-x9/SDCALayer as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by p-x9.
p-x9/SDCALayer 0.1.0
Server-Driven CALayer [WIP]
⭐️ 3
🕓 6 days ago
iOS linux macOS iOS
.package(url: "https://github.com/p-x9/SDCALayer.git", from: "0.1.0")


Server-Driven CALayer




Supported Layer

  • CALayer
  • CAShapeLayer
  • CATextLayer
  • CAScrollLayer
  • CAGradientLayer

JSON to CALayer

let json: String = ""

let model = SDCALayer.load(fromJSON: json)

let layer: CALayer = model?.convertToLayer()

YAML to CALayer

let model = SDCALayer.load(fromYAML: yaml)

CALayer to JSON

let layer = CAShapeLayer()
/* ~ customize layer  ~ */

let model = SDCALayer(model: layer.codable())

let json: String = model?.json

CALayer to YAML

let yaml: String = model?.yaml

Formats of Layer Model

Layer's models are defined in the following directory. Models

    "frame": [
    "cornerRadius": 5.0,
    "borderColor": {
        "code": "#FF0088"
    // other properties

Since we need to know the actual class of the Layer, we need to receive a model with the class name and a model for each class, as follows

    "class": "CAShapeLayer",
    "layerModel": {
        // CAShapeLayer Model

By using the p-x9/IndirectlyCodable library, CALayer indirectly conforms to the Codable protocol, allowing inter-conversion with json.

Support for custom layer classes

Suppose we have the following customized layer class.

class AALayer: CALayer {
    var newProperty: String? = "AAAA"

create layer model like this.

click to expand
class JAALayer: JCALayer {
    typealias Target = AALayer // alias for target layer class

    // Coding key (codable)
    private enum CodingKeys: String, CodingKey {
        case newProperty

    // Target class name to exact layer class
    // You must specify the class name including the product name and package name.
    // (ex. MyApp.AALayer)
    public override class var targetTypeName: String {
        String(reflecting: Target.self)

    override init() {

    // Decodable
    public required init(from decoder: Decoder) throws {
        try super.init(from: decoder)

        let container = try decoder.container(keyedBy: CodingKeys.self)

        newProperty = try container.decodeIfPresent(String.self, forKey: .newProperty)

    public required convenience init(with object: CALayer) {

        reverseApplyProperties(with: object)

    // Encodable
    public override func encode(to encoder: Encoder) throws {
        try super.encode(to: encoder)

        var container = encoder.container(keyedBy: CodingKeys.self)

        try container.encode(newProperty, forKey: .newProperty)

    // apply properties to taget from model
    // model -> target
    public override func applyProperties(to target: CALayer) {
        super.applyProperties(to: target)

        guard let target = target as? AALayer else { return }

        target.newProperty = newProperty

    // apply properties to model from target
    // targe -> model
    public override func reverseApplyProperties(with target: CALayer) {
        super.reverseApplyProperties(with: target)

        guard let target = target as? AALayer else { return }

        newProperty = target.newProperty

    public override func convertToLayer() -> CALayer? {
        let layer = AALayer()

        self.applyProperties(to: layer)

        return layer

Finally, specify the model class in the layer class extension

extension AALayer {
    public typealias Target = JAALayer

    public override class var codableTypeName: String {
        String(reflecting: Target.self)


Websocket HotReload

Start the server, change the json, save it, and it will be reflected in the app. install calayer-ws app to yor iphone (or simulator) and connect your server.

python ./server/ws-hotreload-server.py "<path to json>"

example json file is here.

python ./server/ws-hotreload-server.py "./Example/json/star.json"


MIT License


Stars: 3
Last commit: 6 days ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

6 days ago

What's Changed

Full Changelog: https://github.com/p-x9/SDCALayer/compare/0.0.1...0.1.0

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics