swiftui grid
1.0.0
カスタムスタイルを備えたSwiftuiグリッドビューレイアウト。
iOS、macos、watchos、tvosのその他の例については、 GridDemo.xcodeprojを開きます

ScrollView {
Grid ( colors ) {
Rectangle ( )
. foregroundColor ( $0 )
}
}
. gridStyle (
ModularGridStyle ( columns : . min ( 100 ) , rows : . fixed ( 100 ) )
)
ScrollView {
Grid ( 1 ... 69 , id : . self ) { index in
Image ( " ( index ) " )
. resizable ( )
. scaledToFit ( )
}
}
. gridStyle (
StaggeredGridStyle ( . horizontal , tracks : 8 , spacing : 4 )
) トラック設定を使用すると、特定のユースケースにグリッド動作をカスタマイズできます。モジュラーグリッドとずらしたグリッドの両方がトラック値を使用してレイアウトを計算します。モジュラーレイアウトでは、両方の列と行がトラックです。
public enum Tracks : Hashable {
case count ( Int )
case fixed ( CGFloat )
case min ( CGFloat )
}グリッドは、親ビューによって提供されるサイズの等しい画分に分割されます。
ModularGridStyle ( columns : 3 , rows : 3 )
StaggeredGridStyle ( tracks : 8 )アイテムのサイズは、特定の幅または高さまで固定されています。
ModularGridStyle ( columns : . fixed ( 100 ) , rows : . fixed ( 100 ) )
StaggeredGridStyle ( tracks : . fixed ( 100 ) )最小アイテムの幅または高さを尊重する自動layout。
ModularGridStyle ( columns : . min ( 100 ) , rows : . fixed ( 100 ) )
StaggeredGridStyle ( tracks : . min ( 100 ) ) アイテムのサイズと位置を設定します
struct CardsView : View {
@ State var selection : Int = 0
var body : some View {
ScrollView {
Grid ( 0 ..< 100 ) { number in
Card ( title : " ( number ) " )
. onTapGesture {
self . selection = number
}
}
. padding ( )
. overlayPreferenceValue ( GridItemBoundsPreferencesKey . self ) { preferences in
RoundedRectangle ( cornerRadius : 16 )
. strokeBorder ( lineWidth : 4 )
. foregroundColor ( . white )
. frame (
width : preferences [ self . selection ] . width ,
height : preferences [ self . selection ] . height
)
. position (
x : preferences [ self . selection ] . midX ,
y : preferences [ self . selection ] . midY
)
. animation ( . linear )
}
}
}
} マスターブランチへのフォーク/プルリクエストを介してお気軽に寄付してください。機能をリクエストしたり、バグをレポートしたい場合は、新しい問題を開始してください。
このプロジェクトが便利だと思う場合は、GitHubスポンサーになることを検討してください。