Skip to content

Instantly share code, notes, and snippets.

@uchcode
uchcode / RegularPolygonShape.swift
Last active July 5, 2020 08:29
Regular Polygon Shape in SpriteKit
import SpriteKit
func CGPolygonPath(radius: CGFloat, vertices: CGFloat) -> CGPath {
let path = CGMutablePath()
let angle = 360.0 / vertices
var i = CGFloat(1.0); while i <= vertices {
let degrees = angle * i + 90.0
let radians = degrees * .pi / 180.0
let point = CGPoint(
x: cos(radians) * radius,
@uchcode
uchcode / ImageViewer.swift
Last active January 11, 2023 10:49
Developing a Document-Based App in SwiftUI
import SwiftUI
struct ContentView: View {
@ObservedObject var browser = { () -> DocumentBrowserObject in
let dbo = DocumentBrowserObject (
forOpeningFilesWithContentTypes: [
"public.png",
"public.jpeg"
]
)
@uchcode
uchcode / safariview.swift
Created March 28, 2020 15:55
SwiftUI with UIViewController Design Pattern.
import SwiftUI
import SafariServices
struct DemoView: View, Hostable {
@EnvironmentObject var hostedObject: HostingObject<DemoView>
var address: String = "https://example.com"
func present() { // UIKit code
let safari = SFSafariViewController(url: URL(string: address)!)
import SwiftUI
public struct Polygon: Shape {
public let sides: Int
public init(sides: Int) { self.sides = sides }
public func path(in rect: CGRect) -> Path {
let diameter = Double(min(rect.size.width, rect.size.height))
let radius = diameter / 2.0
let rad = Double.pi / 180.0
let center = CGPoint(x: rect.size.width / 2.0, y: rect.size.height / 2.0)
import UIKit
class ViewController: UIViewController {
var text: String?
override func loadView() {
super.loadView()
view.backgroundColor = .systemBackground
let label = UILabel()
view.addSubview(label)
label.text = text
@uchcode
uchcode / navigation.swift
Created January 28, 2020 16:20
SwiftUI NavigationView example for iPad
import SwiftUI
var hostingController: UIViewController?
func getUISplitViewController() -> UISplitViewController {
if hostingController?.children.count == 0 {
fatalError()
}
guard let split = hostingController?.children[0] as? UISplitViewController else {
fatalError()
@uchcode
uchcode / clock.swift
Created May 7, 2019 03:58
Swift Playgrounds Analog Clock Example.
import SpriteKit
import PlaygroundSupport
class Scene: SKScene {
var hands: [String: SKShapeNode] = [:]
override func didMove(to view: SKView) {
let center = CGPoint(x: frame.midX, y: frame.midY)
let radius = CGFloat(450)
let face = SKShapeNode(circleOfRadius: radius)
@uchcode
uchcode / jsoa.js
Last active February 15, 2019 16:46
JSOA: JSON Array
/*
"background-color:red;font-size:32px;"
{backgroundColor:"red",fontSize:"32px"}
[{backgroundColor:"red"},{fontSize:"32px"}]
[["background-color","red"],["font-size","32px"]]
*/
const kebab_case = s => s.replace(/[A-Z]/g,"-$&").toLowerCase();
const camel_case = s => s.replace(/(\-)([a-z])/g,(...a)=>a[2].toUpperCase());
const assign = (a,b) => Object.assign(a,b);
@uchcode
uchcode / style.js
Last active February 15, 2019 00:56
css converter
/*
"background-color:red;font-size:32px;"
{backgroundColor:"red",fontSize:"32px"}
[{backgroundColor:"red"},{fontSize:"32px"}]
*/
const kebab_case = s => s.replace(/[A-Z]/g,"-$&").toLowerCase();
const camel_case = s => s.replace(/(\-)([a-z])/g,(...a)=>a[2].toUpperCase());
const CASCADE = {};
@uchcode
uchcode / converter.html
Last active February 14, 2019 01:37
hyperapp web components example
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=2">
<meta name="apple-mobile-web-app-capable" content="yes">
<style>body{margin:0;padding:0;}</style>
<script type="module">
document.addEventListener('touchmove',e=>e.preventDefault(),{passive:false})
import { h, app } from 'https://unpkg.com/hyperapp?module'
!function() {