Skip to content

Instantly share code, notes, and snippets.

import Foundation
// XORShiftのRandomNumberGenerator
// 参考: https://ja.wikipedia.org/wiki/Xorshift
struct XORShift: RandomNumberGenerator {
private var y: UInt64
init(seed: UInt64 = 2463534242) {
y = seed > 0 ? seed : 2463534242
@HonmaMasaru
HonmaMasaru / NowPlayingIcon.swift
Last active June 11, 2022 02:03
Apple Musicで再生中の曲に表示されるアレ
import SwiftUI
import PlaygroundSupport
struct NowPlayingIcon: View {
var body: some View {
ZStack(alignment: .bottom) {
HStack(spacing: 12) {
ForEach(0..<5) { i in
Bar(delay: Double(i) * 0.2) // (1.0秒 / 5.0本)
}
@HonmaMasaru
HonmaMasaru / times.swift.gyb
Last active April 28, 2022 01:34
gybをやってみる
// times.swift.gyb
%{
# https://yoshinblog.com/blogs/426E9F5D-8CA4-49A7-AC6D-54AC22429B46
# curl "https://raw.githubusercontent.com/apple/swift/master/utils/gyb.py" -o "gyb.py"
# ./gyb.py times.swift.gyb -o times.swift --line-directive=
}%
% for sign in ['','U']:
% for intType in ['','8','16','32','64']:
/// Extension that adds a few additional functionalities to ${sign}Int${intType}
@HonmaMasaru
HonmaMasaru / 3ColorStripeTile.swift
Last active April 16, 2022 09:47
3Color Stripe Tile
import UIKit
import PlaygroundSupport
final class ViewController: UIViewController {
private let colors: [UIColor] = [.cyan, .magenta, .yellow]
override func viewDidLoad() {
super.viewDidLoad()
let img = makeTile(colors: colors, size: 50)!
.resizableImage(withCapInsets: .zero, resizingMode: .tile)
@HonmaMasaru
HonmaMasaru / NetImage.swift
Last active April 2, 2022 00:10
ネットから画像を取得
import SwiftUI
import PlaygroundSupport
/// ネットから画像を取得
struct NetImage: View {
@ObservedObject private var observer: Observer
init(url: URL) {
observer = Observer(url)
}
@HonmaMasaru
HonmaMasaru / divide.swift
Last active April 2, 2022 00:15
配列の分割
// ↓ こっちの方が良い
// https://gist.github.com/sumitokamoi/22b8f30c2c1a3ef93cb1f03d4a7e8066
func divide<Element>(array: Array<Element>, partition: Int) -> [Array<Element>] {
var r: [Array<Element>] = []
stride(from: 0, to: array.count, by: partition).forEach { i in
let e = i + partition < array.count ? i + partition : array.count
r.append(Array(array[i..<e]))
}
return r
@HonmaMasaru
HonmaMasaru / SieveOfEratosthenes.swift
Last active April 2, 2022 00:37
エラトステネスの篩
import Foundation
// エラトステネスの篩
// https://ja.wikipedia.org/?curid=147374
// パターン1
func sieve1(_ max: Int) -> [Int] {
let max = max + 1
import UIKit
import UserNotifications
extension UNNotificationAttachment {
static func build(identifier: String, image: UIImage?, options: [AnyHashable: Any]? = nil) -> UNNotificationAttachment? {
guard let data = image?.pngData() else { return nil }
let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("\(identifier).png")
do { try data.write(to: url) } catch { return nil }
return try? UNNotificationAttachment(identifier: identifier, url: url, options: options)
}
import UIKit
protocol SegueKey: RawRepresentable where RawValue == String {}
extension UIViewController {
func performSegue<Key: SegueKey>(withIdentifier identifier: Key, sender: Any?) {
performSegue(withIdentifier: identifier.rawValue, sender: sender)
}
}
import UIKit
protocol ConfigBase {
associatedtype Key: RawRepresentable, CaseIterable where Key.RawValue == String
var userDefaults: UserDefaults { get }
func set(_ value: Any?, forKey key: Key)
func set(_ value: Float, forKey key: Key)
func set(_ value: Double, forKey key: Key)