Skip to content

Latest commit

 

History

History
56 lines (42 loc) · 1.16 KB

playground-tips.md

File metadata and controls

56 lines (42 loc) · 1.16 KB

Swift Playground tips

  • Attaching live view to playgrounds
import PlaygroundSupport
import UIKit

let view = UIView(frame: 300.toSize.toRectWithZeroOrigin)
view.backgroundColor = .blue
PlaygroundPage.current.liveView = view
  • Running Playgrounds as continuous apps
import UIKit
import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true

class TappableView : UIView{

  @objc func handleTaps(_ sender: UITapGestureRecognizer){
    PlaygroundPage.current.finishExecution()
  }

  override init(frame: CGRect) {
    super.init(frame: frame)
    let recognizer = UITapGestureRecognizer(target: self, action:
      #selector(TappableView.handleTaps(_:)))
    addGestureRecognizer(recognizer)
  }

  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

}

extension Double{
  var toSize: CGSize{
    return .init(width: self, height: self)
  }
}

extension CGSize{
  var toRectWithZeroOrigin: CGRect{
    return CGRect(origin: .zero, size: self)
  }
}

let view = TappableView(frame: 300.toSize.toRectWithZeroOrigin)
view.backgroundColor = .blue
PlaygroundPage.current.liveView = view