Skip to content

Instantly share code, notes, and snippets.

View benigumocom's full-sized avatar
🏠
🙆

chanzmao benigumocom

🏠
🙆
View GitHub Profile
import Foundation
import SwiftUI
import AVFoundation
struct SystemSoundWheelPicker: View {
@State private var sounds: [SystemSoundID] = []
@State private var playingID = -1
var body: some View {
Picker("", selection: $playingID) {
@benigumocom
benigumocom / serialize-async-functions.swift
Last active March 6, 2024 02:35
【Swift】非同期関数の直列実行 - withCheckedContinuation 👉 https://android.benigumo.com/20240305/sereial-async/
import SwiftUI
import AVFoundation
struct TestView: View {
var body: some View {
Button("Play1") {
AudioServicesPlaySystemSound(1000)
}
Divider()
@benigumocom
benigumocom / SystemSoundGrid.swift
Last active March 9, 2024 08:39
【Swift】SystemSoundID 一覧がないのですが 👉 https://android.benigumo.com/20240304/system-sound-id/
@Observable final class Sound: Identifiable {
var id: SystemSoundID
var duration: Double
var playing: Bool
var valid: Bool { self.duration > 0.01 }
init(_ id: Int, _ duration: Double, _ playing: Bool) {
self.id = SystemSoundID(id)
self.duration = duration
import SwiftUI
@Observable final class ScheduledTimer {
var elapsed = TimeInterval.zero
private var timer: Timer?
func start() {
timer = Timer.scheduledTimer(withTimeInterval: 0.001, repeats: true) { _ in
self.elapsed += 0.001
}
@benigumocom
benigumocom / CustomViewModifier.swift
Last active February 21, 2024 09:16
【SwiftUI】カスタム ViewModifier の使い方と使いどころ 🤔 👉 https://android.benigumo.com/20240221/custom-viewmodifier/
import SwiftUI
struct TestView: View {
var body: some View {
VStack {
Text("top")
Text("bottom")
}
import Foundation
// inspiered by
// lifegame/swift/lifegame.swift at master · tex2e/lifegame
// https://github.com/tex2e/lifegame/blob/master/swift/lifegame.swift
@Observable final class LifeGame {
var field: [[Int]] = []
var generation = 0
@benigumocom
benigumocom / convert_and_chunk_array.swift
Last active February 11, 2024 14:34
Convert 1D array to 2D array and vice versa, chunk array by n pieces each 👉 https://android.benigumo.com/20240211/handle-array/
let d2: [[String]] = [
// x (col) →
["あ", "い", "う", "え", "お"],
["か", "き", "く", "け", "こ"],
["さ", "し", "す", "せ", "そ"]
]
print(d2)
// [["あ", "い", "う", "え", "お"], ["か", "き", "く", "け", "こ"], ["さ", "し", "す", "せ", "そ"]]
@benigumocom
benigumocom / showtable.swift
Last active February 13, 2024 01:55
【Swift】配列をコンソールにテーブル形式で表示する 👉 https://android.benigumo.com/20240213/show-table/
extension Array where Element: Collection, Element.Index == Int {
func showTable() {
transposed()
.map { $0.map { "\($0)" } }
.map {
let max = $0.map { $0.countAsHalfWidth }.max()!
return $0.map { $0.padding(length: max) }
}
.transposed()
.map { "| " + $0.joined(separator: " | ") + " |" }
@benigumocom
benigumocom / Pong.swift
Last active February 23, 2024 15:02
【 SwiftUI 】 Pong Wars を SwiftUI に移植してみた 👉 https://android.benigumo.com/20240201/pong-wars/
import Foundation
@Observable final class Pong {
var squares: [Square] = []
var balls: [Ball] = []
var dayCount = 0
var nightCount = 0
var generation = 0
private var cols = 0
@benigumocom
benigumocom / transpose-matrix.swift
Last active February 1, 2024 02:23
【Swift】2次元配列 で 転置行列 ( transpose matrix ) 👉 https://android.benigumo.com/20240201/transpose-matrix/
extension Collection where Element: Collection,
Self.Index == Int, Element.Index == Int {
func transposed1() -> [[Element.Element]] {
let cols = 0 ..< (self.first?.count ?? 0)
let rows = 0 ..< self.count
var result: [[Element.Element]] = []
for col in cols {
var newRow: [Element.Element] = []
for row in rows {