Last active
August 6, 2024 17:17
-
-
Save jverkoey/5d7268292e5bc82a4346c080b7079735 to your computer and use it in GitHub Desktop.
Threads: seamless carousel in SwiftUI
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 SeamlessCarouselDemo: View { | |
@State private var seamless = false | |
@ViewBuilder | |
func image(_ name: String) -> some View { | |
Image(name) | |
.resizable(resizingMode: .stretch) | |
.aspectRatio(contentMode: .fit) | |
.clipShape(.rect(cornerRadius: seamless ? 0 : 8)) | |
} | |
var body: some View { | |
ScrollView(.horizontal) { | |
HStack(spacing: seamless ? 0 : 8) { | |
image("image1x1") | |
image("image2x1") | |
} | |
.gesture( | |
MagnifyGesture() | |
.onChanged { value in | |
if !seamless, value.magnification < 0.95 { | |
withAnimation(.easeIn(duration: 0.2)) { | |
seamless = true | |
} | |
} else if seamless, value.magnification > 1.05 { | |
withAnimation(.easeOut(duration: 0.2)) { | |
seamless = false | |
} | |
} | |
} | |
) | |
.clipShape(.rect(cornerRadius: 8)) | |
.padding() | |
.frame(maxHeight: 300) | |
} | |
.sensoryFeedback(.impact(weight: .heavy), trigger: seamless) | |
} | |
} | |
#Preview { | |
SeamlessCarouselDemo() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment