-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1164: first pass at new image viewer
- Loading branch information
1 parent
8bf33de
commit 9465df8
Showing
9 changed files
with
211 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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
20 changes: 20 additions & 0 deletions
20
Nos/Assets/Colors.xcassets/button-close-background.colorset/Contents.json
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"colors" : [ | ||
{ | ||
"color" : { | ||
"color-space" : "srgb", | ||
"components" : { | ||
"alpha" : "1.000", | ||
"blue" : "0x24", | ||
"green" : "0x0E", | ||
"red" : "0x16" | ||
} | ||
}, | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
Nos/Assets/Colors.xcassets/button-close-foreground.colorset/Contents.json
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"colors" : [ | ||
{ | ||
"color" : { | ||
"color-space" : "srgb", | ||
"components" : { | ||
"alpha" : "1.000", | ||
"blue" : "0xC6", | ||
"green" : "0x81", | ||
"red" : "0x9A" | ||
} | ||
}, | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
Nos/Assets/Colors.xcassets/image-background.colorset/Contents.json
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"colors" : [ | ||
{ | ||
"color" : { | ||
"color-space" : "srgb", | ||
"components" : { | ||
"alpha" : "1.000", | ||
"blue" : "0x16", | ||
"green" : "0x07", | ||
"red" : "0x0D" | ||
} | ||
}, | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import SDWebImageSwiftUI | ||
import SwiftUI | ||
|
||
/// A viewer for images. Supports full-screen zoom and panning. | ||
struct ImageViewer: View { | ||
/// The URL of the image to display. | ||
let url: URL | ||
|
||
@Environment(\.dismiss) var dismiss | ||
|
||
@State private var scale: CGFloat = 1.0 | ||
@State private var lastScale: CGFloat = 1.0 | ||
@State private var offset: CGSize = .zero | ||
@State private var lastOffset: CGSize = .zero | ||
@State private var imageSize: CGSize = .zero | ||
|
||
private let maxScale: CGFloat = 10.0 | ||
private let minScale: CGFloat = 1.0 | ||
|
||
var body: some View { | ||
ZStack { | ||
Color.imageBackground | ||
|
||
GeometryReader { geometry in | ||
WebImage(url: url) | ||
.onSuccess { image, _, _ in | ||
imageSize = image.size | ||
} | ||
.resizable() | ||
.aspectRatio(contentMode: .fit) | ||
.scaleEffect(scale) | ||
.offset(x: offset.width, y: offset.height) | ||
.gesture( | ||
MagnificationGesture() | ||
.onChanged { value in | ||
withAnimation { | ||
scale = lastScale * value | ||
} | ||
} | ||
.onEnded { value in | ||
withAnimation { | ||
let newScale = lastScale * value | ||
if newScale > maxScale { | ||
scale = maxScale | ||
} else if newScale < minScale { | ||
scale = minScale | ||
} | ||
|
||
lastScale = scale | ||
} | ||
} | ||
) | ||
.gesture( | ||
DragGesture() | ||
.onChanged { value in | ||
offset = CGSize( | ||
width: lastOffset.width + value.translation.width, | ||
height: lastOffset.height + value.translation.height | ||
) | ||
} | ||
.onEnded { _ in | ||
lastOffset = offset | ||
} | ||
) | ||
.frame(width: geometry.size.width, height: geometry.size.height) | ||
.clipped() | ||
.onTapGesture(count: 2) { | ||
withAnimation { | ||
if scale == minScale { | ||
scale = 4.0 | ||
} else { | ||
scale = minScale | ||
} | ||
lastScale = scale | ||
} | ||
} | ||
} | ||
.ignoresSafeArea() | ||
|
||
ZStack(alignment: .topLeading) { | ||
Color.clear | ||
|
||
Button( | ||
action: { | ||
dismiss() | ||
}, | ||
label: { | ||
Image(systemName: "xmark") | ||
.symbolVariant(.fill.circle) | ||
.symbolRenderingMode(.palette) | ||
.foregroundStyle(Color.buttonCloseForeground, Color.buttonCloseBackground) | ||
.font(.title) | ||
} | ||
) | ||
.padding() | ||
} | ||
} | ||
.ignoresSafeArea() | ||
} | ||
} | ||
|
||
#Preview { | ||
ImageViewer( | ||
url: URL( | ||
string: "https://image.nostr.build/9640e78f03afc4927d80a15fd1c4bd1404dc654a8663efb92cc9ee1b8b0719a3.jpg" | ||
)! | ||
) | ||
} | ||
|
||
#Preview { | ||
ImageViewer( | ||
url: URL( | ||
string: "https://images.unsplash.com/photo-1716783841007-7de314270444" | ||
)! | ||
) | ||
} |
File renamed without changes.
This file contains 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
This file contains 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