Skip to content

Commit

Permalink
스크롤뷰 리더 활용
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaute committed Oct 29, 2021
1 parent d231447 commit 4bdf23a
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 5 deletions.
20 changes: 16 additions & 4 deletions SwiftUILab.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
B408B34D272BCC4F00A43F7E /* ScrollReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = B408B34C272BCC4F00A43F7E /* ScrollReader.swift */; };
B417A34C272958EF005085A9 /* MapView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B417A34B272958EF005085A9 /* MapView.swift */; };
B417A34F272962CF005085A9 /* MyMapView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B417A34E272962CF005085A9 /* MyMapView.swift */; };
B41D6C73271D84300027E8C7 /* SwiftUILabApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = B41D6C72271D84300027E8C7 /* SwiftUILabApp.swift */; };
Expand All @@ -16,6 +17,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
B408B34C272BCC4F00A43F7E /* ScrollReader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScrollReader.swift; sourceTree = "<group>"; };
B417A34B272958EF005085A9 /* MapView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapView.swift; sourceTree = "<group>"; };
B417A34E272962CF005085A9 /* MyMapView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MyMapView.swift; sourceTree = "<group>"; };
B41D6C6F271D84300027E8C7 /* SwiftUILab.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwiftUILab.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand All @@ -36,6 +38,14 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
B408B34B272BCC2600A43F7E /* ScrollReader */ = {
isa = PBXGroup;
children = (
B408B34C272BCC4F00A43F7E /* ScrollReader.swift */,
);
path = ScrollReader;
sourceTree = "<group>";
};
B417A34D272958F6005085A9 /* MapView */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -69,6 +79,7 @@
B41D6C72271D84300027E8C7 /* SwiftUILabApp.swift */,
B41D6C74271D84300027E8C7 /* ContentView.swift */,
B417A34D272958F6005085A9 /* MapView */,
B408B34B272BCC2600A43F7E /* ScrollReader */,
);
path = SwiftUILab;
sourceTree = "<group>";
Expand Down Expand Up @@ -162,6 +173,7 @@
B417A34F272962CF005085A9 /* MyMapView.swift in Sources */,
B417A34C272958EF005085A9 /* MapView.swift in Sources */,
B41D6C73271D84300027E8C7 /* SwiftUILabApp.swift in Sources */,
B408B34D272BCC4F00A43F7E /* ScrollReader.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -219,7 +231,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
Expand Down Expand Up @@ -274,7 +286,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
SDKROOT = iphoneos;
Expand All @@ -301,7 +313,7 @@
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -332,7 +344,7 @@
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
2 changes: 1 addition & 1 deletion SwiftUILab/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import SwiftUI

struct ContentView: View {
var body: some View {
MapView()
ScrollReader()
}
}

Expand Down
75 changes: 75 additions & 0 deletions SwiftUILab/ScrollReader/ScrollReader.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import SwiftUI

struct ScrollReader: View {
@Namespace var topID
@Namespace var bottomID

@State private var selectedIndex = 0

var selectedBG: (Int, Int) -> Color = { currentItemIndex, selectedItemIndex in
currentItemIndex == selectedItemIndex ? Color.green : Color.yellow
}

var body: some View {
VStack {
ScrollViewReader { proxy in
ScrollView(.vertical, showsIndicators: false) {
Button("Scroll to Bottom") {
withAnimation {
proxy.scrollTo(bottomID)
}
}
.id(topID)

ForEach(0...100, id: \.self) { index in
Text("번호: \(index)")
.padding(50)
.background(selectedBG(index, selectedIndex))
.id(index)
}
.onChange(of: selectedIndex) { changedIndex in
withAnimation {
proxy.scrollTo(changedIndex, anchor: .center)
}
}

Button("Scroll to Top") {
withAnimation {
proxy.scrollTo(topID)
}
}
.id(bottomID)
}
}

HStack {
Button {
selectedIndex = 10
} label: {
Text("인덱스: 10")
.padding()
}

Button {
selectedIndex = 50
} label: {
Text("인덱스: 50")
.padding()
}

Button {
selectedIndex = 80
} label: {
Text("인덱스:80")
.padding()
}
}
}
}
}

struct ScrollReader_Previews: PreviewProvider {
static var previews: some View {
ScrollReader()
}
}

0 comments on commit 4bdf23a

Please sign in to comment.