Skip to content

Instantly share code, notes, and snippets.

@DarrenHurst
Created January 2, 2024 13:39
Show Gist options
  • Save DarrenHurst/a80e4d7cb926e5289b12174dd270fcde to your computer and use it in GitHub Desktop.
Save DarrenHurst/a80e4d7cb926e5289b12174dd270fcde to your computer and use it in GitHub Desktop.
Generic Button Swiftui
import Foundation
import SwiftUI
@available(iOS 16.0, *)
struct RenderButton: View, Identifiable {
var id : UUID = UUID()
var text: String?
var image: Image?
var shape: (any Shape)? = Rectangle()
var action: () -> Void
@State var animate: Bool = false
var body: some View {
ZStack {
Button(action: action) {
if text != nil {
Text(text ?? "Back") .foregroundColor(Config().textColor).padding(Config().padding)
}
image?
.foregroundColor(Config().textColor)
.padding(Config().padding)
}
.background(
shape?
.stroke(Config().backgroundBorder, style: StrokeStyle(lineWidth: Config().borderWidth))
.opacity(Config().backgroundOpacity)
.anyView)
.background(Config().background.opacity(Config().backgroundOpacity))
.accessibilityLabel(text ?? "Back")
.cornerRadius(Config().borderCornerRadius)
.mask(shape?.opacity(Config().backgroundOpacity).anyView)
.shadow(radius: 5.0)
.onAppear() {
animate = true
}
}
}
}
struct Config {
let background = LinearGradient(colors: [.white,.gray,.gray], startPoint: UnitPoint.top, endPoint: .bottom)
let backgroundBorder = Color.black
let backgroundOpacity = 0.9
let borderWidth = 3.0
let borderCornerRadius = 0.0
let borderControlWidth = 4.0
let padding = 15.0
let textColor = Color.white
let textColorOff = Color.white
}
@available(iOS 16.0, *)
struct RenderButtonPreview: PreviewProvider {
static let back = NSLocalizedString("Back", tableName: nil, bundle: Bundle.module, value: "", comment: "")
static var previews: some View {
HStack{
RenderButton(text: "Rectangle") {}
RenderButton(text: "Capsule", shape: Capsule()) {}
RenderButton(image: Image(systemName:"house"), shape: Circle()) {}
}
}
}
@DarrenHurst
Copy link
Author

animating the stroke on the Circle() works nice. but the other shapes do not rotate .. possible animate from one LG to another.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment