Skip to content

Instantly share code, notes, and snippets.

View tomasen's full-sized avatar
😞

SHEN SHENG tomasen

😞
View GitHub Profile
@tomasen
tomasen / WatchTabView.swift
Last active January 16, 2022 04:47
WatchTabView inside NavigationView demo on WatchOS
// Created by SHEN SHENG on 1/16/22.
// This code is a part of the Wordbook iOS app.
// Wordbook is an App to help memorize and learn new English words.
// website: https://www.wordbook.cool
struct WatchMasterView: View {
@FocusState private var focusTab: Int?
@State private var currentTab = 0
var body: some View {
@tomasen
tomasen / iOSLookAlikeTabView.swift
Last active January 16, 2022 04:20
Look-alike TabView inside NavigationView in iOS
// Created by SHEN SHENG on 1/16/22.
// This code is a part of the Wordbook iOS app.
// Wordbook is an App to help memorize and learn new English words.
// website: https://www.wordbook.cool
@State private var tabSelection = 1
var body: some View {
NavigationView {
VStack{
@tomasen
tomasen / simularWordSizes.swift
Created December 25, 2021 17:19
simularWordSizes for WordCloudView
func simularWordSizes(a: [CGSize], b: [CGSize]) -> Bool {
if a == b {
return true
}
if a.count != b.count {
return false
}
var diff : CGFloat = 0
for i in 0...(a.count-1) {
@tomasen
tomasen / WordCloudPositionCache.swift
Last active December 25, 2021 17:19
WordCloudStateCache for WordCloudView
private var positionCache = WordCloudPositionCache()
func calcPositions(canvasSize: CGSize, itemSizes: [CGSize]) -> [CGPoint] {
var pos = [CGPoint](repeating: CGPoint.zero, count: itemSizes.count)
if canvasSize.height == 0 {
return pos
}
if positionCache.canvasSize == canvasSize {
return positionCache.positions
@tomasen
tomasen / calcPositions.swift
Last active December 25, 2021 16:59
calcPositions for WordCloudView
func checkIntersects(rect: CGRect, rects: [CGRect]) -> Bool {
for r in rects {
if rect.intersects(r) {
return true
}
}
return false
}
func checkOutsideBoundry(canvasSize: CGSize, rect: CGRect) -> Bool {
@tomasen
tomasen / findSafePlace.swift
Created December 25, 2021 16:46
find a place by brutal force for WordCloudView
func findSafePlace(for size: CGSize, avoid occupiedAreas: [CGRect]) -> CGRect {
// keep trying random places until it fit
for _ in 0...100 {
let randomRect = CGRect(origin: CGPoint(x: CGFloat.random(in: 0...canvasSize.width),
y: CGFloat.random(in: 0...canvasSize.height)),
size: size)
var collision = false
for rect in occupiedAreas {
if rect.intersects(randomRect) {
collision = true
@tomasen
tomasen / WordSizeGetter.swift
Last active December 25, 2021 16:36
WordSizeGetter in WordCloudView demo
struct WordCloudView: View {
private let words: [WordElement] = [WordElement].generate(forSwiftUI: true)
@State private var canvasRect = CGRect()
@State private var wordSizes: [CGSize]
init() {
self._wordSizes = State(initialValue:[CGSize](repeating: CGSize.zero, count: words.count))
}
@tomasen
tomasen / RectGetter.swift
Created December 25, 2021 16:05
SwiftUI RectGetter example
struct RectGetterDemoView: View {
@State private var canvasRect = CGRect()
var body: some View {
Text("Demo").background(RectGetter($canvasRect))
}
}
struct RectGetter: View {
@tomasen
tomasen / WordCloudImageView.swift
Last active December 25, 2021 15:36
word cloud image view demo
var body: some View {
Image(uiImage: wordCloudImage(words))
.resizable()
.aspectRatio(contentMode: .fit)
.padding(10)
}
func wordCloudImage(_ words: [WordElement]) -> UIImage {
UIGraphicsBeginImageContextWithOptions(canvasSize, false, 1.0)
// Singleton makes sure there is only one "single" instance across the system.
// by binding to a tcp resource as specified by addr, eg. "127.0.0.1:51337".
func Singleton(addr string) {
go singletonServer(addr)
for {
// wait and confirm that server was started successfully
pid, err := getSingletonPID(addr)
if err == nil && pid == strconv.Itoa(os.Getpid()) {
return
}