CALayer equivalent in SwiftUI

Viewed 5858

UIViews are known to be layer backed, but not sure about SwiftUI View. Is there a concept of layer in SwiftUI which can be added as sublayer, animated, everything you could do with CALayer?

3 Answers

In the "Building Custom Views with SwiftUI" talk, they demonstrated that Views and Drawing models/interfaces have essentially been combined under the hood in the framework. Therefore, a View has layer like attributes but built onto the View itself rather than a layer abstraction.

Source: https://developer.apple.com/videos/play/wwdc2019/237/

import SwiftUI
import AVFoundation

struct DrawingView: UIViewRepresentable {

    private let view = UIView()

    var layer: CALayer? {
        view.layer
    }

    func makeUIView(context: Context) -> UIView {
        view
    }

    func updateUIView(_ uiView: UIView, context: Context) { }
}

After checking the SwiftUI's all methods by jumping to defination of View like this:

enter image description here

In that file, i can see that all UIControls, all methods, which as developer we can access are listed there. Here, i found that there is no any layer like concept. But all the properties of layer - like cornerRadius, borderWidth, borderColor, shadow etc all are already defined there. So i think there are less probable chance of having such concept in SwiftUI.

Still not sure for my this answer.

Related