Last active
November 11, 2023 02:04
-
-
Save prafullakumar/55b3df672e839b499216d6b856a2bdc3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct RefreshControl: View { | |
var coordinateSpace: CoordinateSpace | |
var onRefresh: ()->Void | |
@State var refresh: Bool = false | |
var body: some View { | |
GeometryReader { geo in | |
if (geo.frame(in: coordinateSpace).midY > 50) { | |
Spacer() | |
.onAppear { | |
if refresh == false { | |
onRefresh() ///call refresh once if pulled more than 50px | |
} | |
refresh = true | |
} | |
} else if (geo.frame(in: coordinateSpace).maxY < 1) { | |
Spacer() | |
.onAppear { | |
refresh = false | |
///reset refresh if view shrink back | |
} | |
} | |
//Add Animation | |
} | |
} | |
} | |
struct PullToRefreshDemo: View { | |
var body: some View { | |
ScrollView { | |
RefreshControl(coordinateSpace: .named("RefreshControl")) { | |
//refresh view here | |
} | |
Text("Some view...") | |
}.coordinateSpace(name: "RefreshControl") | |
} | |
} | |
struct PullToRefreshDemo_Previews: PreviewProvider { | |
static var previews: some View { | |
PullToRefreshDemo() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment