Skip to content

Instantly share code, notes, and snippets.

View xiaoxidong's full-sized avatar
🎯
Focusing

XiaoDong xiaoxidong

🎯
Focusing
View GitHub Profile
@mecid
mecid / Calendar.swift
Last active December 19, 2025 11:09
SwiftUI Calendar view using LazyVGrid
import SwiftUI
extension Calendar {
func generateDates(
inside interval: DateInterval,
matching components: DateComponents
) -> [Date] {
var dates: [Date] = []
dates.append(interval.start)
@SatoTakeshiX
SatoTakeshiX / scaleAndOrient.swift
Created May 17, 2020 07:13
Scale and Orient Image
func scaleAndOrient(image: UIImage) -> UIImage {
// Set a default value for limiting image size.
let maxResolution: CGFloat = 640
guard let cgImage = image.cgImage else {
print("UIImage has no CGImage backing it!")
return image
}
@wassupdoc
wassupdoc / popoversample
Last active May 7, 2021 04:45
SwiftUI Popover
class PopupViewController:UIViewController{
let frame:CGRect
let content:AnyView
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
@wassupdoc
wassupdoc / WithPopover.swift
Last active February 20, 2024 08:48 — forked from ccwasden/WithPopover.swift
Custom size popover in iOS SwiftUI
// -- Usage
struct Content: View {
@State var open = false
@State var popoverSize = CGSize(width: 300, height: 300)
var body: some View {
WithPopover(
showPopover: $open,
popoverSize: popoverSize,
@mecid
mecid / AnimatableVector.swift
Last active July 8, 2025 16:53
High-performance Animatable Vector for SwiftUI
import SwiftUI
import enum Accelerate.vDSP
struct AnimatableVector: VectorArithmetic {
static var zero = AnimatableVector(values: [0.0])
static func + (lhs: AnimatableVector, rhs: AnimatableVector) -> AnimatableVector {
let count = min(lhs.values.count, rhs.values.count)
return AnimatableVector(values: vDSP.add(lhs.values[0..<count], rhs.values[0..<count]))
}
@mecid
mecid / AnimatableCGPointVector.swift
Created June 17, 2020 23:40
The type that holds array of CGPoints and conforms to VectorArithmetic
import SwiftUI
struct AnimatableCGPointVector: VectorArithmetic {
static var zero = AnimatableCGPointVector(values: [.zero])
static func - (lhs: AnimatableCGPointVector, rhs: AnimatableCGPointVector) -> AnimatableCGPointVector {
let values = zip(lhs.values, rhs.values)
.map { lhs, rhs in lhs.animatableData - rhs.animatableData }
.map { CGPoint(x: $0.first, y: $0.second) }
return AnimatableCGPointVector(values: values)
@smic
smic / BorderlessWindow.swift
Last active July 7, 2023 20:19
Extension to create borderless windows in SwiftUI
import SwiftUI
extension CGRect {
fileprivate func point(anchor: UnitPoint) -> CGPoint {
var point = self.origin
point.x += self.size.width * anchor.x
#if os(macOS)
point.y += self.size.height * (1 - anchor.y)
#else
point.y += self.size.height * anchor.y
import Cocoa
import SwiftUI
public extension View {
func bindWindow(_ window: Binding<NSWindow?>) -> some View {
return background(WindowAccessor(window))
}
}
public struct WindowAccessor: NSViewRepresentable {
import SwiftUI
@main
struct ExampleCommandsApp: App {
var body: some Scene {
DocumentGroup(
newDocument: ExampleCommandsDocument()
) { file in
ContentView(
@hishnash
hishnash / ExampleWindowReaderApp.swift
Created December 21, 2020 05:59
Access to the underlying UIWindow & NSWindow in swiftUI gives access to window methods and attributes.
//
// ExampleWindowReaderApp.swift
// Shared
//
// Created by Matthaus Woolard on 21/12/2020.
//
import SwiftUI
@main