Skip to content

Commit 107b8e5

Browse files
committed
🔮 Erase different type of views to AnyView to avoid Group
1 parent f0f87cd commit 107b8e5

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

‎SwiftUI2048.xcodeproj/project.pbxproj‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
FA00BCB722ACF79A00400C82 /* AppKitSupports.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA6C7B9F22AA14E900760D4F /* AppKitSupports.swift */; };
1515
FA132EFA22A94D9400BD9743 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = FA132EF922A94D9400BD9743 /* MainMenu.xib */; };
1616
FA132F2022A980EE00BD9743 /* AppKitSupportsLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA132F1E22A97FA200BD9743 /* AppKitSupportsLoader.swift */; };
17+
FA3EC97622AE3F9000D43738 /* SwiftUIExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA3EC97522AE3F9000D43738 /* SwiftUIExtensions.swift */; };
18+
FA3EC97722AE402500D43738 /* SwiftUIExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA3EC97522AE3F9000D43738 /* SwiftUIExtensions.swift */; };
1719
FA633E1F22A77D1C00DE6288 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA633E1E22A77D1C00DE6288 /* AppDelegate.swift */; };
1820
FA633E2522A77D1D00DE6288 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = FA633E2422A77D1D00DE6288 /* Assets.xcassets */; };
1921
FA633E2822A77D1D00DE6288 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = FA633E2722A77D1D00DE6288 /* Preview Assets.xcassets */; };
@@ -80,6 +82,7 @@
8082
FA132EF922A94D9400BD9743 /* MainMenu.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = MainMenu.xib; sourceTree = "<group>"; };
8183
FA132EFC22A95F9100BD9743 /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/AppKit.framework; sourceTree = DEVELOPER_DIR; };
8284
FA132F1E22A97FA200BD9743 /* AppKitSupportsLoader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppKitSupportsLoader.swift; sourceTree = "<group>"; };
85+
FA3EC97522AE3F9000D43738 /* SwiftUIExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftUIExtensions.swift; sourceTree = "<group>"; };
8386
FA633E1B22A77D1C00DE6288 /* 2048.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = 2048.app; sourceTree = BUILT_PRODUCTS_DIR; };
8487
FA633E1E22A77D1C00DE6288 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
8588
FA633E2422A77D1D00DE6288 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
@@ -190,6 +193,7 @@
190193
children = (
191194
FA132EF822A90EFD00BD9743 /* SwiftUI2048.entitlements */,
192195
FA6F2B2022A81832009C2D3B /* FunctionalUtils.swift */,
196+
FA3EC97522AE3F9000D43738 /* SwiftUIExtensions.swift */,
193197
FA633E3522A7926200DE6288 /* Models */,
194198
FA633E3222A77D3400DE6288 /* Views */,
195199
FA633E1E22A77D1C00DE6288 /* AppDelegate.swift */,
@@ -453,6 +457,7 @@
453457
FA6F2B2122A81832009C2D3B /* FunctionalUtils.swift in Sources */,
454458
FA633E3922A79AFA00DE6288 /* GameLogic.swift in Sources */,
455459
FA633E3422A77D5700DE6288 /* BlockView.swift in Sources */,
460+
FA3EC97622AE3F9000D43738 /* SwiftUIExtensions.swift in Sources */,
456461
FA633E3D22A79B3C00DE6288 /* BlockGridView.swift in Sources */,
457462
FA633E1F22A77D1C00DE6288 /* AppDelegate.swift in Sources */,
458463
FA633E3722A7928500DE6288 /* BlockMatrix.swift in Sources */,
@@ -474,6 +479,7 @@
474479
buildActionMask = 2147483647;
475480
files = (
476481
FA8D101522AD137D0056756D /* GameMainHostingView.swift in Sources */,
482+
FA3EC97722AE402500D43738 /* SwiftUIExtensions.swift in Sources */,
477483
FA8D101222AD0F3E0056756D /* FunctionalUtils.swift in Sources */,
478484
FA8D100C22AD0F1F0056756D /* BlockMatrix.swift in Sources */,
479485
FA8D100D22AD0F1F0056756D /* GameLogic.swift in Sources */,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// SwiftUIExtensions.swift
3+
// 2048
4+
//
5+
// Created by Hongyu on 6/10/19.
6+
// Copyright © 2019 Cyandev. All rights reserved.
7+
//
8+
9+
import SwiftUI
10+
11+
extension View {
12+
13+
func eraseToAnyView() -> AnyView {
14+
return AnyView(self)
15+
}
16+
17+
}

‎SwiftUI2048/Views/GameView.swift‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,13 @@ struct GameView : View {
114114
.edgesIgnoringSafeArea(.all)
115115
}
116116

117-
var body: some View {
118-
Group {
119-
if gestureEnabled {
120-
content.gesture(gesture, including: .all)
121-
} else {
122-
content
123-
}
117+
var body: AnyView {
118+
if gestureEnabled {
119+
return content
120+
.gesture(gesture, including: .all)
121+
.eraseToAnyView()
124122
}
123+
return content.eraseToAnyView()
125124
}
126125

127126
}

0 commit comments

Comments
 (0)