SwiftUI_ViewModifier_Tips

こんにちはフリーランスの永田です。1月からSwiftUIを案件で実施するので、日夜Swiftも調査しています。それとAndroidとAWSを進めています。

今回はネストが深くなるSwiftUIをシンプルに運用するアイデアを共有します。

ネストとは プログラムにおける { } こういった記号が増えてしまい、階層が増える状態です。

        VStack {
            // smoething
            HStack {
                // smoething
                ZStack {
                // smoething
                }
            }
        }

挙動

コードリンク

gist:e4158a471a0e8d2943d7dcd5e14ba4c5

ViewModifierの直訳

https://developer.apple.com/documentation/swiftui/viewmodifier

A modifier that you apply to a view or another view modifier, producing a different version of the original value.


ビューまたは別のビュー修飾子に適用し、元の値の異なるバージョンを生成する修飾子。

SwiftUIではGenericsを多く活用されています。Genericsは整数や文字などの決まった型はなく、使用する側が型を定義する手法です。

バージョン

iOSはモバイル OSXはマックPC  tvOSはAppleTV watchOSはAppleWatch

@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)

protocolでViewの型の命名 

Bodyの型はViewと命名します。protocol(インターフェース)として使用します。

associatedtype Body : View

指定機能

content型を取得して、Viewを返却するといったコードです。

この引数と戻り値の間で、Viewに機能を設定して、返却します。

func body(content: Self.Content) -> Self.Body

protocolで型の命名 

typealiasはprotocol(インターフェース)をContent型と命名します。

typealias Content

拡張型使用メソッド

extensionは Viewに対して、機能を拡張します。TはGenericsで型は使用側が決定します。返却する型として、ModifiedContentは次のセクションで解説します。

<Self, T>のSelfはキーワードです。protocol(インターフェース)を拡張している場合に使用します。

ModifiedContent構造体

contentは先ほど定義したprotocol(インターフェース)です。Modifierは定義を検索したら、?で正体がカプセル化して見えないようになっています。使用側から推測するとmodifierの部分はTとして、Genericsの抽象化しているので、グループ内に準拠する@inlinable public の公開されているprotocol(インターフェース)だと推測します。

public var content: Content
public var modifier: Modifier
@inlinable public init(content: Content, modifier: Modifier)

このような部分が@inlinable public です。

コードリンク

gist:e4158a471a0e8d2943d7dcd5e14ba4c5

自作ライブリーの Exampleに実装

https://github.com/daisukenagata/BothSidesCamera/blob/master/Example/BothSidesCamera/ContentView.swift

SwiftUIを進める上で、副産物的に知見とアイデアが浮かんだので形にしました。

これからも継続していきます。何卒、よろしくお願い申し上げます。

来年も皆様、良い年になりますように。

以上、貴重なお時間お読みくださいまして、誠にありがとうございます。


投稿日

カテゴリー:

,

投稿者:

タグ:

コメント

“SwiftUI_ViewModifier_Tips” への4件のフィードバック

  1. kiet giang melbourneのアバター

    Hey! Someone in my Myspace group shared this website with
    us so I came to take a look. I’m definitely loving the information. I’m
    book-marking and will be tweeting this to my followers!
    Terrific blog and excellent style and design.

  2. Ap2020.Milldiv.ddns.netのアバター

    And that final thing is vital, because no matter
    how good a party is, nothing makes a celebration great ike the perfect,
    personal party decorations you choose. As the first part of the
    Lyrics break, no doubt that she is tattling of a
    past kinship (. Buut forr guys like Colin Farrell or David Beckam , a bad
    boy look can bettrer be achieved which has a shaved head.

    1. dbankのアバター
      dbank

      And that final thing is vital, because no matter
      how good a party is, nothing makes a celebration great ike the perfect,
      personal party decorations you choose. As the first part of the
      Lyrics break, no doubt that she is tattling of a
      past kinship (. Buut forr guys like Colin Farrell or David Beckam , a bad
      boy look can bettrer be achieved which has a shaved head.

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です