Skip to content

Commit

Permalink
finger tracking improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
gh123man committed May 24, 2022
1 parent d6f6203 commit e5b703d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
16 changes: 16 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"object": {
"pins": [
{
"package": "Introspect",
"repositoryURL": "https://github.com/siteline/SwiftUI-Introspect.git",
"state": {
"branch": null,
"revision": "f2616860a41f9d9932da412a8978fec79c06fe24",
"version": "0.1.4"
}
}
]
},
"version": 1
}
38 changes: 15 additions & 23 deletions Sources/Refresher/Refresher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ public struct RefreshableScrollView<Content: View, RefreshView: View>: View {
@State var state = RefresherState()
@State var distance: CGFloat = 0
@State var rawDistance: CGFloat = 0
@State var canRefresh = true
private var style: Style
private var config: Config

@State private var uiScrollView: UIScrollView?
@State private var showRefreshControl = true
@State private var isRefresherVisible = true
@State private var isFingerDown = false

init(
axes: Axis.Set = .vertical,
Expand Down Expand Up @@ -127,12 +127,13 @@ public struct RefreshableScrollView<Content: View, RefreshView: View>: View {
return 0
}

private var isTracking: Bool {
guard let scrollView = uiScrollView else { return false }
return scrollView.isTracking
}

private var showRefreshControls: Bool {
guard let scrollView = uiScrollView else {
return false
}

return scrollView.isTracking || showRefreshControl
return isFingerDown || isRefresherVisible
}

@ViewBuilder
Expand Down Expand Up @@ -196,31 +197,22 @@ public struct RefreshableScrollView<Content: View, RefreshView: View>: View {
}

private func offsetChanged(_ val: CGFloat) {
guard showRefreshControls else {
return
}

isFingerDown = isTracking
distance = val - headerInset

if distance < 1 {
canRefresh = true
}

state.dragPosition = normalize(from: 0, to: config.refreshAt, by: distance)

if case .refreshing = state.mode { return }
if !canRefresh { return }

guard distance > 0 else {
set(mode: .notRefreshing)
showRefreshControl = false
guard distance > 0, showRefreshControls else {
state.mode = .notRefreshing
isRefresherVisible = false
return
}
showRefreshControl = true

isRefresherVisible = true

if distance >= config.refreshAt {
UIImpactFeedbackGenerator(style: .medium).impactOccurred()
set(mode: .refreshing)
canRefresh = false

refreshAction {
DispatchQueue.main.asyncAfter(deadline: .now() + config.holdTime) {
Expand Down

0 comments on commit e5b703d

Please sign in to comment.