LoginSignup
8
9

More than 5 years have passed since last update.

Playgroundsでビューの表示

Posted at

はじめに

  • Playgroundsは、Xcode上でコードを即時評価できるツールです
  • アプリ内で実装する前に、アルゴリズムを確認するなどが可能です
  • ここではもう一歩踏み込んで、PlaygroundSupportモジュールを導入してビューをリアルタイムに表示する方法を確認します
    • Xcode 7まではXCPlaygroundモジュールでしたが、そっちはdeprecatedになってます

検証環境

  • Xcode 8.0
  • Swift 3.0

コード例

LiveViewSample.playground
import UIKit
import PlaygroundSupport    // このモジュールが必要

let frame = CGRect(x: 0, y: 0, width: 320, height: 320)
let image = UIImage(named: "macbook.jpeg")
let imageView = UIImageView(frame: frame)
imageView.contentMode = .scaleAspectFit
imageView.image = image

// 表のスレッドの処理が終わっても、別スレッドの処理を継続可能とする
PlaygroundPage.current.needsIndefiniteExecution = true

// アシスタントエディタ上に、対象のビューを表示
PlaygroundPage.current.liveView = imageView
  • このサンプルは、GitHubに上げてあります

実行例

liveview.png

その他

  • 今回は試していませんが、ジェスチャーなどにも対応しているようなので、ちょっとしたインタラクティブ性は出せそうです

参考

8
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9