Skip to content

Instantly share code, notes, and snippets.

View michaelevensen's full-sized avatar

Michael Nino Evensen michaelevensen

View GitHub Profile
@RobertMenke
RobertMenke / query-param.codable.swift
Last active February 28, 2023 10:24
Swift Codable to URL Query String
import Foundation
import DictionaryCoding
/// Note: This relies on the DictionaryCoding package https://github.com/elegantchaos/DictionaryCoding
struct QueryParamEncoder {
func encode<T: Encodable>(_ item: T) throws -> String {
let encoder = DictionaryEncoder()
let encoded: [String: Any] = try encoder.encode(item)
let queryParams = encodeDictionary(encoded)
@Harry-Harrison
Harry-Harrison / ContentView.swift
Last active December 1, 2024 17:59
Haptic Feedback Vibrations in SwiftUI
// This prints a list of buttons that on tap will fire a different type of haptic vibration
import SwiftUI
struct ContentView: View {
let generator = UINotificationFeedbackGenerator()
var body: some View {
VStack(alignment: .center, spacing: 30.0) {
Button(action: {
@atereshkov
atereshkov / swift-5-resumable-timer.swift
Created August 19, 2019 14:39
Swift 5 Resumable Timer
class ResumableTimer: NSObject {
private var timer: Timer? = Timer()
private var callback: () -> Void
private var startTime: TimeInterval?
private var elapsedTime: TimeInterval?
// MARK: Init
@standinga
standinga / AudioPlayground.swift
Last active May 13, 2024 21:01
Swift Playground shows how to play multiple wav files on top of background audio file, using AVAudioPlayerNode, AVAudioMixerNode, AVAudioEngine
import AVFoundation
import UIKit
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
class AudioPlayer {
var backgroundAudioFile:AVAudioFile
var topAudioFiles: [AVAudioFile] = []
@backslash-f
backslash-f / ClampingPlayground.swift
Last active September 7, 2022 09:26
Swift Clamping Example
import Foundation
import UIKit
/// Clamps the given `value` into the range defined by `minValue` and `maxValue`. For example:
///
/// (0.0 ... 5.0).clamp(4.2) = 4.2
/// (0.0 ... 5.0).clamp(-1.3) = 0.0
/// (0.0 ... 5.0).clamp(6.4) = 5.0
///
/// Source: https://stackoverflow.com/a/46799935/584548
@Inndy
Inndy / RecordViewController.swift
Created April 30, 2017 06:26
iOS create overlay with UIWindow
import UIKit
class RecordViewController: UIViewController {
@IBAction func closeOverlay(_ sender: Any) {
self.view.window?.resignKey()
self.view.window?.isHidden = true
}
}
@artem-sherbachuk
artem-sherbachuk / FadingInOutAVPlayer.swift
Created January 19, 2017 09:43
Fading audio in and out AVPlayer
let player = AVPlayer()
func playFileAtURL(url: NSURL) {
let asset = AVAsset.assetWithURL(url) as AVAsset
let duration = asset.duration
let durationInSeconds = CMTimeGetSeconds(duration)
let item = AVPlayerItem(asset: asset)
let params = AVMutableAudioMixInputParameters(track: asset.tracks.first! as AVAssetTrack)
@bitgord
bitgord / Quick-ES6-Setup-Heroku
Created December 12, 2016 02:36
Quick setup to use ES6 with Heroku
// First download node, create new folder
// Cd into new folder, npm and git init
// Create heroku app
heroku create
// install babel cli (to do compiling), babel presets (to compile the right features), express (web server)
npm install babel-cli babel-presets-es2015 express
// Create file .babelrs and add json
//
// InAppManager.swift
//
// Created by Ellina Kuznetcova on 12/10/2016.
// Copyright © 2016 Flatstack. All rights reserved.
//
import Foundation
import StoreKit
@michaeldorner
michaeldorner / gist:746c659476429a86a9970faaa6f95ec4
Last active December 15, 2024 10:49 — forked from rgcottrell/gist:5b876d9c5eea4c9e411c
An FM Synthesizer in Swift using AVAudioEngine
import AVFoundation
import Foundation
// The single FM synthesizer instance.
private let gFMSynthesizer: FMSynthesizer = FMSynthesizer()
public class FMSynthesizer {