Changes in ImageViewer.

This commit is contained in:
Marcin Czachursk 2023-01-14 20:45:49 +01:00
parent 13c4bc85c3
commit 3acdb018f6
3 changed files with 58 additions and 54 deletions

View File

@ -42,6 +42,9 @@ struct VernissageApp: App {
.tint(self.tintColor) .tint(self.tintColor)
.preferredColorScheme(self.theme) .preferredColorScheme(self.theme)
.task { .task {
UIPageControl.appearance().currentPageIndicatorTintColor = UIColor.label
UIPageControl.appearance().pageIndicatorTintColor = UIColor.secondaryLabel
let defaultSettings = ApplicationSettingsHandler.shared.getDefaultSettings() let defaultSettings = ApplicationSettingsHandler.shared.getDefaultSettings()
if let tintColor = TintColor(rawValue: Int(defaultSettings.tintColor)) { if let tintColor = TintColor(rawValue: Int(defaultSettings.tintColor)) {

View File

@ -39,9 +39,9 @@ struct StatusView: View {
exifCreatedDate: $exifCreatedDate, exifCreatedDate: $exifCreatedDate,
exifLens: $exifLens) exifLens: $exifLens)
.onTapGesture { .onTapGesture {
withoutAnimation { // withoutAnimation {
self.showImageViewer.toggle() self.showImageViewer.toggle()
} // }
} }
VStack(alignment: .leading) { VStack(alignment: .leading) {

View File

@ -11,8 +11,9 @@ struct ImagesViewer: View {
@State var selectedAttachmentId: String = String.empty() @State var selectedAttachmentId: String = String.empty()
@Environment(\.dismiss) private var dismiss @Environment(\.dismiss) private var dismiss
// Opacity usied during fadein/fadeoff animations. // Opacity usied during close dialog animation.
@State private var opacity = 0.6 @State private var opacity = 1.0
private let closeDragDistance = 140.0
// Zoom. // Zoom.
@State private var zoomScale = 1.0 @State private var zoomScale = 1.0
@ -34,48 +35,24 @@ struct ImagesViewer: View {
.resizable() .resizable()
.aspectRatio(contentMode: .fit) .aspectRatio(contentMode: .fit)
.tag(attachment.id) .tag(attachment.id)
.offset(x: currentOffset.width) .offset(currentOffset)
.scaleEffect(finalAmount + currentAmount) .scaleEffect(finalAmount + currentAmount)
.gesture((finalAmount + currentAmount) > 1.0 ? dragGesture : nil) .opacity(self.opacity)
//.gesture((finalAmount + currentAmount) > 1.0 ? dragGesture : nil)
.gesture(dragGesture)
.gesture(magnificationGesture) .gesture(magnificationGesture)
.gesture(doubleTapGesture) .gesture(doubleTapGesture)
.gesture(tapGesture) .gesture(tapGesture)
} }
} }
} }
.opacity(self.opacity)
.tabViewStyle(PageTabViewStyle()) .tabViewStyle(PageTabViewStyle())
.overlay(alignment: .topTrailing, content: {
Button {
self.close()
} label: {
Image(systemName: "xmark")
.foregroundColor(.white)
.padding(8)
.background(Color.mainTextColor.opacity(0.3))
.clipShape(Circle())
.padding()
}
})
}
.onAppear {
withAnimation(.linear(duration: 0.2)) {
opacity = 1.0
}
} }
} }
private func close() { private func close() {
withAnimation(.linear(duration: 0.3)) {
opacity = 0.1
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
withoutAnimation {
dismiss() dismiss()
} }
}
}
var magnificationGesture: some Gesture { var magnificationGesture: some Gesture {
MagnificationGesture() MagnificationGesture()
@ -84,26 +61,8 @@ struct ImagesViewer: View {
} }
.onEnded { amount in .onEnded { amount in
let finalMagnification = finalAmount + currentAmount let finalMagnification = finalAmount + currentAmount
// self.revertToPrecalculatedMagnification(magnification: finalMagnification)
if finalMagnification < 1.0 { self.resetMagnification(magnification: finalMagnification)
// When image is small we are returning to starting point.
withAnimation(.default) {
finalAmount = 1.0
currentAmount = 0
// Also we have to move image to orginal position.
currentOffset = CGSize.zero
}
} else if finalMagnification > 2.0 {
// When image is magnified to much we are rturning to 1.5 maginification.
withAnimation(.default) {
finalAmount = 1.5
currentAmount = 0
}
} else {
finalAmount = finalMagnification
currentAmount = 0
}
} }
} }
@ -119,14 +78,27 @@ struct ImagesViewer: View {
} }
var dragGesture: some Gesture { var dragGesture: some Gesture {
DragGesture() DragGesture(minimumDistance:20)
.onChanged { amount in .onChanged { amount in
self.currentOffset = CGSize(width: amount.translation.width + self.accumulatedOffset.width, self.currentOffset = CGSize(width: amount.translation.width + self.accumulatedOffset.width,
height: amount.translation.height + self.accumulatedOffset.height) height: amount.translation.height + self.accumulatedOffset.height)
let pictureOpacity = (self.closeDragDistance - self.currentOffset.height) / self.closeDragDistance
self.opacity = pictureOpacity >= 0 ? pictureOpacity : 0
} .onEnded { amount in } .onEnded { amount in
self.currentOffset = CGSize(width: amount.translation.width + self.accumulatedOffset.width, self.currentOffset = CGSize(width: amount.translation.width + self.accumulatedOffset.width,
height: amount.translation.height + self.accumulatedOffset.height) height: amount.translation.height + self.accumulatedOffset.height)
self.accumulatedOffset = self.currentOffset self.accumulatedOffset = self.currentOffset
if self.accumulatedOffset.height < self.closeDragDistance {
withAnimation(.default) {
self.currentOffset = CGSize.zero
self.accumulatedOffset = CGSize.zero
self.opacity = 1.0
}
} else {
self.close()
}
} }
} }
@ -135,6 +107,35 @@ struct ImagesViewer: View {
self.close() self.close()
}) })
} }
private func revertToPrecalculatedMagnification(magnification: Double) {
if magnification < 1.0 {
// When image is small we are returning to starting point.
withAnimation(.default) {
finalAmount = 1.0
currentAmount = 0
// Also we have to move image to orginal position.
currentOffset = CGSize.zero
}
} else if magnification > 2.0 {
// When image is magnified to much we are rturning to 1.5 maginification.
withAnimation(.default) {
finalAmount = 1.5
currentAmount = 0
}
} else {
finalAmount = magnification
currentAmount = 0
}
}
private func resetMagnification(magnification: Double) {
withAnimation(.default) {
finalAmount = 1.0
currentAmount = 0
}
}
} }
struct ImagesViewer_Previews: PreviewProvider { struct ImagesViewer_Previews: PreviewProvider {