LoginSignup
2
4

More than 3 years have passed since last update.

UIKit類似Frameworkを自作してみる

Last updated at Posted at 2015-07-01

iOS13以後

  • Userにソース公開しない部分 アプリにマルチウィンド機能が追加されたのでApplicationはメンバにsceneDelegateを持つようになった。そいつを初期化するためのSceneDelegate.swiftファイルがプロジェクトに追加された。
main.swift
protocol ApplicationDelegate {                                                  
  func didFinishLaunchingWithOptions(a: Application) -> Bool
}
protocol SceneDelegate {                                                  
  func willConnectTo(: UIScene) -> Bool
}

class Application {
// ここで、プロトコルに従うクラスのインスタンスを用意する。
  var s: UIWindowScene?
  var delegate: ApplicationDelegate!
  var sceneDelegate: UIWindowSceneDelegate!

    func start() -> String {
      s = UIWindowScene()
      delegate.didFinishLaunchingWithOptions(self)
      sceneDelegate.willConnectTo(s)
    return "OK"
    }
    func statusBarHidden() -> Void {
      print("Hidden done")
    }
    func statusBarOrientation() -> Void {
      print("Rotation done")
    }
}

let application = Application()
application.delegate = AppDelegate() //entry point & run loop, この辺りはStoryboardが行う。
application.sceneDelegate = SceneDelegate()
application.start()
// applicationが自分のstatusBarHiddenその他メソッドを実行するチャンスを、
// AppDelegateのインスタンスへ与える
  • Userが実装する部分
AppDelegate.swift
class AppDelegate: ApplicationDelegate {
  func didFinishLaunchingWithOptions(application: Application) -> Bool {
    application.statusBarOrientation()
    return true
  }
}
  • 以下が追加、window変数がAppDelegate.swiftから移動
SceneDelegate.swift
class SceneDelegate: WindowSceneDelegate {
  var window: UIWindow?  // ここに移動                                      
  func willConnectTo(s: UIScene) -> Bool {
    // some window initialize procedure
    s.windows.insert(window, at: windows.startIndex)
    s.someMethod()
    return true
  }
}

iOS12以前(iOS12含む)

  • Userにソース公開しない部分
main.swift
protocol ApplicationDelegate {                                                  
  func didFinishLaunchingWithOptions(a: Application) -> Bool
}

class Application {
// ここで、プロトコルに従うクラスのインスタンスを用意する。
  var delegate: ApplicationDelegate!
    func start() -> String {
      delegate.didFinishLaunchingWithOptions(self)
    return "OK"
    }
    func statusBarHidden() -> Void {
      print("Hidden done")
    }
    func statusBarOrientation() -> Void {
      print("Rotation done")
    }
}

let application = Application()
application.delegate = AppDelegate()
application.start()
// applicationが自分のstatusBarHiddenその他メソッドを実行するチャンスを、
// AppDelegateのインスタンスへ与える
  • Userが実装する部分
AppDelegate.swift
class AppDelegate: ApplicationDelegate {
  var window: UIWindow?                                        
  func didFinishLaunchingWithOptions(application: Application) -> Bool {
    application.statusBarOrientation()
    return true
  }
}
  • コンパイル&実行方法 【swiftcコマンドの引数に与えるソースの順番が重要!main.swiftは最後の引数にする。】

swiftc AppDelegate.swift main.swift
./main

参考:プロトコルとデリゲートのとても簡単なサンプルについて

編集履歴:[2015/07/01 1st Manuscript]
編集履歴:[2020/04/27 revised for iOS13]

2
4
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
2
4