Skip to content

Instantly share code, notes, and snippets.

View peantunes's full-sized avatar

Pedro Antunes peantunes

  • Trainline
  • London, UK
View GitHub Profile
struct RecursivelyGlowInTheDarkViewModifier: ViewModifier {
let colors: [Color]
func body(content: Content) -> some View {
var modifiedColors = colors // First create a local variable
let nextColor: Color?
if !modifiedColors.isEmpty { // if it is not empty, remove the first element to use it
nextColor = modifiedColors.remove(at: 0)
} else { // or set as nil
nextColor = nil
}
@peantunes
peantunes / GlowInTheDarkViewModifier-first.swift
Last active January 10, 2024 04:57
Glow in the dark View Modifier for SwiftUI
struct GlowInTheDarkViewModifier: ViewModifier {
let active: Bool
@ViewBuilder func body(content: Content) -> some View {
if active {
content
.shadow(color: .yellow, radius: 5)
.shadow(color: .white, radius: 5)
.shadow(color: .green, radius: 5)
.shadow(color: .blue, radius: 5)
.shadow(color: .green, radius: 5)
private struct GroundReflectionViewModifier: ViewModifier {
let offsetY: CGFloat
func body(content: Content) -> some View {
content // base content
.background( // using background to duplicate the content with the View
content // using the same content here to duplicate
// this will create a flip in the axis Y with the anchor in the bottom
.scaleEffect(x: 1.0, y: -1.0, anchor: .bottom)
// adding the opacity
.opacity(0.3)
@peantunes
peantunes / GroundReflectionViewModifier-final.swift
Last active May 11, 2021 21:03
Reflection effect for SwiftUI
private struct GroundReflectionViewModifier: ViewModifier {
let offsetY: CGFloat
func body(content: Content) -> some View {
content
.background(
content
.mask(
LinearGradient(
gradient: Gradient(stops: [.init(color: .white, location: 0.0), .init(color: .clear, location: 0.6)]),
startPoint: .bottom,
@peantunes
peantunes / AquaButtonViewModifier.swift
Last active May 10, 2021 23:08
ViewModifier version to create the aqua effect
private struct AquaButtonViewModifier: ViewModifier {
let color: Color
//Content type is inferred from the View this is trying to modify
func body(content: Content) -> some View {
content
// adding a gradient background based on the color used in the parameter
.background(LinearGradient(gradient: Gradient(colors: [.white, color.opacity(0.6), .white]), startPoint: .top, endPoint: .bottom))
// rounded corners
.cornerRadius(12)
// adding the borders using overlay with a RoundedRectangle
@peantunes
peantunes / AquaButtonView.swift
Created May 10, 2021 22:30
Aqua button version using View
import SwiftUI
struct AquaButtonView<Content>: View where Content: View {
let color: Color
let content: () -> Content
var body: some View {
content()
.background(
LinearGradient(
gradient: Gradient(
private struct XXLTextViewModifier: ViewModifier {
let size: CGFloat
func body(content: Content) -> some View {
content
.font(.system(size: size*40))
}
}
extension View {
func xxlText(_ size: CGFloat = 2) -> some View {
struct XXLTextView: View {
let text: String
let size: CGFloat
var body: some View {
Text(text)
.font(.system(size: size*40))
}
}
struct XXLTextViewModifier_Previews: PreviewProvider {