This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var filters: [String: [CGFloat]] = [:] | |
func filter(_ val: CGFloat, for count: Int, as id: String) -> CGFloat { | |
if var filter = filters[id] { | |
filter.insert(val, at: 0) | |
let lastVal: CGFloat | |
if filter.count > count { | |
lastVal = filter.popLast()! | |
} else { | |
lastVal = filter.last! | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func range(_ val: CGFloat, low: CGFloat, high: CGFloat, clamp: Bool = true, toLow: CGFloat = 0.0, toHigh: CGFloat = 1.0) -> CGFloat { | |
var norm = (val - low) / (high - low) | |
if clamp { norm = min(max(norm, 0.0), 1.0) } | |
return toLow + norm * (toHigh - toLow) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func circleCenter(_ a: CGPoint, _ b: CGPoint, _ c: CGPoint) -> CGPoint { | |
let dya = b.y - a.y | |
let dxa = b.x - a.x | |
let dyb = c.y - b.y | |
let dxb = c.x - b.x | |
let sa = dya / dxa | |
let sb = dyb / dxb | |
let x = (sa * sb * (a.y - c.y) + sb * (a.x + b.x) - sa * (b.x + c.x)) / (2 * (sb - sa)) | |
let y = -1 * (x - (a.x + b.x) / 2) / sa + (a.y + b.y) / 2 | |
return CGPoint(x: x, y: y) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var allTouches: [UITouch] = [] | |
func add(_ touches: Set<UITouch>) -> [UITouch] { | |
for newTouch in touches { | |
var exist = false | |
for oldTouch in allTouches { | |
if oldTouch == newTouch { | |
exist = true | |
break | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func animate(for duration: CGFloat, loop: @escaping (CGFloat) -> (), done: @escaping () -> ()) { | |
let startTime = Date() | |
RunLoop.current.add(Timer(timeInterval: 1.0 / Double(UIScreen.main.maximumFramesPerSecond), repeats: true, block: { t in | |
let elapsedTime = CGFloat(-startTime.timeIntervalSinceNow) | |
let fraction = min(elapsedTime / duration, 1.0) | |
loop(fraction) | |
if fraction == 1.0 { | |
done() | |
t.invalidate() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GIF2MP4.swift | |
// | |
// Created by PowHu Yang on 2017/1/24. | |
// Copyright © 2017 PowHu Yang. All rights reserved. | |
// | |
import UIKit | |
import Foundation | |
import AVFoundation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum AnimationEase { | |
case linear | |
case easeIn | |
case easeInOut | |
case easeOut | |
} | |
func animate(for duration: CGFloat, ease: AnimationEase = .linear, loop: @escaping (CGFloat) -> (), done: @escaping () -> ()) { | |
let startTime = Date() | |
RunLoop.current.add(Timer(timeInterval: 1.0 / Double(UIScreen.main.maximumFramesPerSecond), repeats: true, block: { t in | |
let elapsedTime = CGFloat(-startTime.timeIntervalSinceNow) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var filterCache: [CGFloat] = [] | |
func filter(_ value: CGFloat, for seconds: CGFloat) -> CGFloat { | |
filterCache.append(value) | |
guard filterCache.count > 1 else { return value } | |
let frameCount = Int(seconds * CGFloat(UIScreen.main.maximumFramesPerSecond)) | |
if filterCache.count > frameCount { | |
filterCache.remove(at: 0) | |
} | |
var filtered: CGFloat = 0.0 | |
for val in filterCache { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var filterCaches: [String: [CGFloat]] = [:] | |
func filter(_ value: CGFloat, for seconds: CGFloat, smooth: Bool = true, id: String) -> CGFloat { | |
guard filterCaches[id] != nil else { | |
filterCaches[id] = [value] | |
return value | |
} | |
filterCaches[id]!.append(value) | |
let frameCount = Int(seconds * CGFloat(60)) | |
if filterCaches[id]!.count > frameCount { | |
filterCaches[id]!.remove(at: 0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let rampPix = GradientPIX(res: .fullscreen) | |
rampPix.style = .angle | |
rampPix.colorSteps[1].color = .black | |
rampPix.colorSteps.append(ColorStep(.liveWave(for: 24.0), .white)) | |
let anglePix = rampPix._quantize(by: 1 / 8)._edge(100) | |
let flipXPix = anglePix + anglePix._flipX() | |
let flipYPix = flipXPix + flipXPix._flipY() | |
let rampExtraPix = GradientPIX(res: .fullscreen) |
OlderNewer