Skip to content

Instantly share code, notes, and snippets.

@daniloc
Created May 3, 2020 01:21
Show Gist options
  • Save daniloc/0c35c35ecdb3771a2915bb72c3d5c7e5 to your computer and use it in GitHub Desktop.
Save daniloc/0c35c35ecdb3771a2915bb72c3d5c7e5 to your computer and use it in GitHub Desktop.
A quick study of making weird, custom UI components with SwiftUI
import SwiftUI
struct SliderView: View {
@State var height: CGFloat = .infinity
@State var sliderOpacity: Double = 0
let action: (() -> Void)
var slide: some Gesture {
DragGesture(minimumDistance: 0, coordinateSpace: .local)
.onChanged {
self.height = $0.location.y
self.sliderOpacity = 1
if self.height < 10 {
self.action()
}
}
.onEnded { _ in
self.height = .infinity
self.sliderOpacity = 0
}
}
var decoration: some View {
VStack(spacing: 0) {
Rectangle()
.fill(Color.black)
.frame(height: 20)
Rectangle()
.fill(Color.yellow)
.frame(height: 50)
Rectangle()
.fill(Color.black)
.frame(height: 40)
}.padding(.top, 300)
}
var body: some View {
ZStack {
Rectangle()
.fill(Color(hue: (48 / 360), saturation: 1, brightness: 0.25))
VStack {
Spacer()
.frame(maxHeight: height)
.layoutPriority(1)
Rectangle()
.fill(Color.yellow)
.opacity(sliderOpacity)
.animation(.easeInOut)
}
decoration
}.frame(width: 100)
.gesture(slide)
.cornerRadius(40)
}
}
struct SliderView_Previews: PreviewProvider {
static var previews: some View {
ZStack {
Color.black
.edgesIgnoringSafeArea(.all)
SliderView(action: { print("Bam!")})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment