Skip to content

Instantly share code, notes, and snippets.

View onevcat's full-sized avatar
🐭

Wei Wang onevcat

🐭
View GitHub Profile
@onevcat
onevcat / Delegate.swift
Last active December 23, 2022 07:21
An auto-weak delegate for handle modern delegate pattern.
import Foundation
/// A class that keeps a weakly reference for `self` when implementing `onXXX` behaviors.
/// Instead of remembering to keep `self` as weak in a stored closure:
///
/// ```swift
/// // MyClass.swift
/// var onDone: (() -> Void)?
/// func done() {
/// onDone?()
@onevcat
onevcat / PartialScroll.swift
Created October 6, 2022 10:15
PartialScroll
import SwiftUI
struct ContentView: View {
@State var offset: CGFloat = 0.0
@State var previousOffset: CGFloat = 0.0
init() {
let appearance = UINavigationBarAppearance()

用户体验和布局进阶

PokeMaster app 现在已经是一个具有完整功能的 SwiftUI app 了。麻雀虽小,五脏俱全,在这个示例 app 里,我们涉及到了一个一般性的 iOS app 所需要的大部分内容:

  • 如何构建内容展示的列表
  • 如何构建用户交互的表单
  • 如何进行网络请求并把内容展示出来
  • 如何响应用户的手势
  • 如何在不同页面之间进行导航
  • 以及,如何通过一定的架构将所有上面的内容整合起来
import Foundation
import Combine
protocol Resumable {
func resume()
}
extension Subscribers {
class ResumableSink<Input, Failure: Error>: Subscriber, Cancellable, Resumable {
let receiveCompletion: (Subscribers.Completion<Failure>) -> Void
```swift
actor GiftCache {
private(set) var cachedGifts: [GiftId: CachedGift] = [:]
// this method would be caller by other parts
func add(gift: CachedGift) {
cachedGifts[gift.giftId] = gift
//...
}
@onevcat
onevcat / orientation-in-framework.m
Created October 28, 2014 02:43
Swizzle to solve Landscape mode in a Portrait app. Can be used in third party framework. If you are working on an app, you can only modify the app delegate to achieve it.
#pragma mark - Magic
// Need for supporting orientation when not supported in host app plist.
// Swizzle original -application:supportedInterfaceOrientationsForWindow: to change supported orientation in runtime.
-(void) swizzleSupportedInterfaceOrientationsForWindow
{
Class applicationDelegateClass = [[UIApplication sharedApplication].delegate class];
Class sdkClass = [self class];
SEL originalSelector = @selector(application:supportedInterfaceOrientationsForWindow:);
SEL swizzledSelector = @selector(las_application:supportedInterfaceOrientationsForWindow:);
@onevcat
onevcat / CoW.swift
Created February 7, 2022 00:21
CoW wrapper
// https://github.com/pointfreeco/swift-composable-architecture/discussions/488#discussioncomment-591715
@propertyWrapper
public struct CoW<T> {
private final class Ref {
var val: T
init(_ v: T) { val = v }
}
private var ref: Ref
import UIKit
import PlaygroundSupport
struct ToDoItem {
let id: UUID
let title: String
init(title: String) {
self.id = UUID()
self.title = title
@onevcat
onevcat / Runtime.swift
Created March 16, 2021 15:30
Runtime
protocol P: AnyObject { }
class A: P {}
Runtime.classes(conformTo: P.Type.self) // [MyApp.A]
class Runtime {
public static func allClasses() -> [AnyClass] {
let numberOfClasses = Int(objc_getClassList(nil, 0))
if numberOfClasses > 0 {
@onevcat
onevcat / Jitouch-Restarter
Created February 15, 2016 04:14
This gist kill Jitouch and then restart it. It solves problems when using Jitouch with Magic Trackpad 2
pkill -f "Jitouch"
sleep 5
nohup ~/Library/PreferencePanes/Jitouch.prefPane/Contents/Resources/Jitouch.app/Contents/MacOS/Jitouch > /dev/null 2>&1 &