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 | |
// MARK: GLOBAL FUNCTION | |
func withOptionalAnimation<Result>(_ animation: Animation? = .default, _ body: () throws -> Result) rethrows -> Result { | |
// Evaluate the parameter you want to check: ReduceMotion in this case | |
if UIAccessibility.isReduceMotionEnabled { | |
// The body gets executed without animation | |
return try body() | |
} else { | |
// The body gets executed with animation |
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
struct Astronaut: Codable, Identifiable { | |
let id: String | |
let name: String | |
let description: String | |
static let allAstronauts = Bundle.main.decode([Astronaut].self, from: "astronauts.json") | |
static let example = allAstronauts[0] | |
} |
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 Foundation | |
extension FileManager { | |
static var documentsDirectory: URL { | |
URL.documentsDirectory | |
} | |
func decode<T: Codable>(from url: URL) throws -> T { | |
let data = try Data(contentsOf: url) | |
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 Foundation | |
struct Card: Identifiable, Codable { | |
var id = UUID() | |
let prompt: String | |
let answer: String | |
} | |
@MainActor class ViewModel: ObservableObject { | |
@Published var cards = [Card]() |
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 ContentView: View { | |
@AppStorage("tapCount") private var tapCount = 0 | |
var body: some View { | |
Button("Tap count: \(tapCount)") { | |
tapCount += 1 | |
} | |
} |
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 | |
extension VerticalAlignment { | |
enum MidAccountAndName: AlignmentID { | |
static func defaultValue(in context: ViewDimensions) -> CGFloat { | |
context[.top] | |
} | |
} | |
static let midAccountAndName = VerticalAlignment(MidAccountAndName.self) |
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 | |
extension ShapeStyle where Self == Color { | |
static var darkBackground: Color { | |
Color(red: 0.1, green: 0.1, blue: 0.2) | |
} | |
} | |
// HOW TO USE IT | |
struct ContentView: View { |
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 SimpleHaptics: View { | |
var body: some View { | |
Text("Hello, World!") | |
.onTapGesture { | |
simpleSuccess() | |
} | |
} | |
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 | |
import CoreHaptics | |
struct CustomHaptics: View { | |
// We use @State here because we want the engine property be alive even if this view gets recreated. | |
@State private var engine: CHHapticEngine? | |
var body: some View { | |
Text("Hello, World!") | |
.onAppear(perform: prepareHaptics) |
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 AccessibilityExample: View { | |
var body: some View { | |
ZStack { | |
// Background | |
Image(decorative: "wood") | |
.resizable() | |
.ignoresSafeArea() | |
OlderNewer