Skip to content

Instantly share code, notes, and snippets.

@gewill
Created December 14, 2022 03:28
Show Gist options
  • Save gewill/bda0eb0a790959a4575e1db922707a6d to your computer and use it in GitHub Desktop.
Save gewill/bda0eb0a790959a4575e1db922707a6d to your computer and use it in GitHub Desktop.
Custom button style use PrimitiveButtonStyle on tvOS
import SwiftUI
struct FixedSizeButtonStyle: PrimitiveButtonStyle {
@FocusState private var isFocused: Bool
var size: CGSize
var cornerRadius: CGFloat
func makeBody(configuration: Configuration) -> some View {
configuration.label
.frame(width: size.width, height: size.height)
.foregroundColor(.accentColor)
.background(
RoundedRectangle(cornerRadius: cornerRadius, style: .continuous)
.stroke()
.foregroundStyle(.linearGradient(colors: [.white.opacity(0.5), .clear, .white.opacity(0.5), .clear], startPoint: .topLeading, endPoint: .bottomTrailing))
)
.background(
RoundedRectangle(cornerRadius: cornerRadius, style: .continuous)
.fill((isFocused ? Color.white : Color.gray).opacity(0.5).gradient)
)
.scaleEffect(isFocused ? 1.2 : 1)
.animation(.easeOut(duration: 0.2), value: isFocused)
.focusable()
.focused($isFocused)
.onTapGesture {
configuration.trigger()
}
}
}
extension Button {
func fixedSizeButtonStyle(size: CGSize = CGSize(width: 122, height: 80), cornerRadius: CGFloat = 20) -> some View {
buttonStyle(FixedSizeButtonStyle(size: size, cornerRadius: cornerRadius))
.frame(width: size.width, height: size.height)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment