Refine fullscreen player drag dismissal
This commit is contained in:
1 parent
d565ec0299
commit
4ab6ff0e28
2 files changed
+48
-26
No files matched your search
@@ -9,6 +9,8 @@ struct FullscreenPlayerView: View {
|
||||
@State private var playIconScale: CGFloat = 1
|
||||
@State private var dragOffset: CGFloat = 0
|
||||
@State private var isFinishingDragDismissal = false
|
||||
@State private var hasCheckedDismissDragStart = false
|
||||
@State private var isActiveDismissDrag = false
|
||||
|
||||
private enum SliderKind {
|
||||
case progress
|
||||
@@ -29,6 +31,12 @@ struct FullscreenPlayerView: View {
|
||||
var body: some View {
|
||||
GeometryReader { geometry in
|
||||
let size = geometry.size
|
||||
let artworkLength = artworkSize(in: size)
|
||||
let playerOrigin = geometry.frame(in: .global).origin
|
||||
let activeDragArea = CGRect(
|
||||
origin: playerOrigin,
|
||||
size: CGSize(width: size.width, height: size.height * 0.3)
|
||||
)
|
||||
|
||||
ZStack(alignment: .topLeading) {
|
||||
LinearGradient(
|
||||
@@ -41,35 +49,10 @@ struct FullscreenPlayerView: View {
|
||||
.fill(.white.opacity(0.55))
|
||||
.frame(width: 58, height: 4)
|
||||
.position(x: size.width * 0.5, y: size.height * 0.08)
|
||||
.contentShape(Rectangle().inset(by: -18))
|
||||
.gesture(
|
||||
DragGesture(minimumDistance: 12, coordinateSpace: .global)
|
||||
.onChanged { value in
|
||||
guard !isFinishingDragDismissal else { return }
|
||||
dragOffset = max(0, value.translation.height)
|
||||
if dragOffset >= size.height * 0.85 {
|
||||
finishDragDismissal(screenHeight: size.height)
|
||||
}
|
||||
}
|
||||
.onEnded { value in
|
||||
guard !isFinishingDragDismissal else { return }
|
||||
let projectedDistance = max(
|
||||
value.translation.height,
|
||||
value.predictedEndTranslation.height
|
||||
)
|
||||
if projectedDistance >= size.height * 0.25 {
|
||||
finishDragDismissal(screenHeight: size.height)
|
||||
} else {
|
||||
withAnimation(.spring(response: 0.38, dampingFraction: 0.86)) {
|
||||
dragOffset = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
.accessibilityLabel("Swipe down to close player")
|
||||
|
||||
artwork
|
||||
.frame(width: artworkSize(in: size), height: artworkSize(in: size))
|
||||
.frame(width: artworkLength, height: artworkLength)
|
||||
.clipShape(RoundedRectangle(cornerRadius: 14, style: .continuous))
|
||||
.shadow(color: .black.opacity(0.16), radius: 26, y: 18)
|
||||
.position(x: size.width * 0.5, y: size.height * 0.31)
|
||||
@@ -126,6 +109,9 @@ struct FullscreenPlayerView: View {
|
||||
}
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
.clipped()
|
||||
.simultaneousGesture(
|
||||
dragToDismissGesture(screenHeight: size.height, activeArea: activeDragArea)
|
||||
)
|
||||
.offset(y: dragOffset)
|
||||
}
|
||||
.ignoresSafeArea()
|
||||
@@ -297,6 +283,41 @@ struct FullscreenPlayerView: View {
|
||||
}
|
||||
}
|
||||
|
||||
private func dragToDismissGesture(screenHeight: CGFloat, activeArea: CGRect) -> some Gesture {
|
||||
DragGesture(minimumDistance: 12, coordinateSpace: .global)
|
||||
.onChanged { value in
|
||||
guard !isFinishingDragDismissal else { return }
|
||||
if !hasCheckedDismissDragStart {
|
||||
hasCheckedDismissDragStart = true
|
||||
isActiveDismissDrag = activeArea.contains(value.startLocation)
|
||||
}
|
||||
guard isActiveDismissDrag else { return }
|
||||
dragOffset = max(0, value.translation.height)
|
||||
if dragOffset >= screenHeight * 0.85 {
|
||||
finishDragDismissal(screenHeight: screenHeight)
|
||||
}
|
||||
}
|
||||
.onEnded { value in
|
||||
defer {
|
||||
hasCheckedDismissDragStart = false
|
||||
isActiveDismissDrag = false
|
||||
}
|
||||
guard !isFinishingDragDismissal else { return }
|
||||
guard isActiveDismissDrag else { return }
|
||||
let projectedDistance = max(
|
||||
value.translation.height,
|
||||
value.predictedEndTranslation.height
|
||||
)
|
||||
if projectedDistance >= screenHeight * 0.25 {
|
||||
finishDragDismissal(screenHeight: screenHeight)
|
||||
} else {
|
||||
withAnimation(.spring(response: 0.38, dampingFraction: 0.86)) {
|
||||
dragOffset = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func artworkSize(in size: CGSize) -> CGFloat {
|
||||
size.width * 0.86
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ struct PlayerBottomAccessoryView: View {
|
||||
.fullScreenCover(isPresented: $isFullscreenPresented) {
|
||||
FullscreenPlayerView()
|
||||
.environmentObject(player)
|
||||
.presentationBackground(.clear)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user