Skip to content

Instantly share code, notes, and snippets.

@niftycode
Last active January 18, 2023 00:03
Show Gist options
  • Save niftycode/bb4ef15885046aeabd40 to your computer and use it in GitHub Desktop.
Save niftycode/bb4ef15885046aeabd40 to your computer and use it in GitHub Desktop.
How to create a basic CALayer object in a playground (Swift 4)
//: # Introduction to CALayer
//: (Xcode 9 and Swift 4)
import UIKit
import PlaygroundSupport
//: From Apples's Support Documentation:
//:
//: > The CALayer class manages image-based content and allows you to perform animations on that content. Layers are often used to provide the backing store for views but can also be used without a view to display content.
//:
// Create a view
var demoView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
// Add green background
demoView.backgroundColor = UIColor(red: 0.63, green: 0.84, blue: 0.35, alpha: 1.0)
// Make this demoView in the Playground visible
PlaygroundPage.current.liveView = demoView
// Open the Assitant Editor to see the result in the Playground
// --> View --> Assistant Editor --> Show Assistant Editor
// Create a layer object
var layer = CALayer()
// Set the properties
layer.bounds = CGRect(x: 0, y: 0, width: 190, height: 190)
layer.position = CGPoint(x: 300/2, y: 300/2)
layer.backgroundColor = UIColor.white.cgColor
layer.borderWidth = 3
layer.borderColor = UIColor.black.cgColor
// Add the layer
demoView.layer.addSublayer(layer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment