Skip to content

Instantly share code, notes, and snippets.

View xiaoxidong's full-sized avatar
🎯
Focusing

XiaoDong xiaoxidong

🎯
Focusing
View GitHub Profile
@MatthewWaller
MatthewWaller / SwiftUI+ChatGPT.swift
Last active August 9, 2023 03:19
SwiftUI Meets ChatGPT
import SwiftUI
struct ContentView: View {
@StateObject var service = ChatGPTService()
@State var textResponse = ""
var body: some View {
VStack {
Text("Bot says: \(textResponse)")
Button {
Task {
@dduan
dduan / SwiftChartPong.swift
Last active August 25, 2024 18:43
Pong Game implemented with Swift Charts.
import SwiftUI
import Charts
import Combine
import Foundation
final class GameState: ObservableObject {
struct Player {
var position: Int
var halfSize: Int = 150
@FradSer
FradSer / LongPressButton.swift
Created November 28, 2021 08:49
A iOS flashlight like button in SwiftUI.
//
// LongPressButton.swift
// Pachino
//
// Created by Frad LEE on 6/28/21.
//
import SwiftUI
// MARK: - LongPressButton
JSON:
https://unpkg.com/[email protected]/emoji.json
class EmojiManager: ObservableObject {
@Published var emojis: [EmojiObject] = []
init() {
decodeJSON()
private struct OnFirstAppear: ViewModifier {
let perform: () -> Void
let `else`: () -> Void
@State private var firstTime = true
func body(content: Content) -> some View {
content.onAppear {
if firstTime {
firstTime = false
@brunow
brunow / gist:69763de7793d1d01e2f1f124cec55d4c
Created April 14, 2021 05:41
SwiftUI os specific modifier
/// After reading an article from Antoine v.d. SwiftLee  @twannl about conditional view modifier, I had an idea to improve my code I write for specific OS.
/// https://www.avanderlee.com/swiftui/conditional-view-modifier
extension Bool {
static var iOS: Bool {
#if os(iOS)
return true
#else
return false
#endif
@helje5
helje5 / SparkleCommands.swift
Last active January 11, 2023 00:37
How to hookup Sparkle in SwiftUI
//
// SparkleCommands.swift
// Past for iChat
//
// Created by Helge Heß on 08.04.21.
//
import SwiftUI
#if SPARKLE && canImport(Sparkle)
@steipete
steipete / View.swift
Created April 4, 2021 13:43
Accept dropping a file onto SwiftUI (both iOS and macOS)
.onDrop(of: [.fileURL], isTargeted: nil) { providers in
if let loadableProvider = providers.first(where: { $0.canLoadObject(ofClass: URL.self) }) {
_ = loadableProvider.loadObject(ofClass: URL.self) { fileURL, _ in
if let fileURL = fileURL, fileURL.pathExtension.lowercased() == "zip" {
self.logger.info("Dropped \(fileURL.path)")
DispatchQueue.main.async {
importer.open(zipArchiveURL: fileURL)
}
}
}
@steipete
steipete / KeyCommand.swift
Last active January 2, 2024 13:26
Add Keyboard Shortcuts to SwiftUI on iOS 13 when using `UIHostingController`. Requires using KeyboardEnabledHostingController as hosting class) See https://steipete.com/posts/fixing-keyboardshortcut-in-swiftui/
//
// KeyCommand.swift
// Adds Keyboard Shortcuts to SwiftUI on iOS 13
// See https://steipete.com/posts/fixing-keyboardshortcut-in-swiftui/
// License: MIT
//
// Usage: (wrap view in `KeyboardEnabledHostingController`)
// Button(action: {
// print("Button Tapped!!")
// }) {
struct ContentView: View {
var body: some View {
Image(nsImage: NSImage(named: .init(NSImage.applicationIconName))!)
.resizable()
.frame(width: 100, height: 100)
.tapWithHighlight(onTap: {
print("Tap")
}, onLongPress: {
print("Long Press")
})