Skip to content

Instantly share code, notes, and snippets.

@berikv
berikv / gist:f17b6e18a8de011ec71f
Created March 27, 2015 19:46
Is user a simulator
let simulator = UIDevice.currentDevice().model.rangeOfString("Simulator") != nil
@berikv
berikv / ExponentialMovingAverage.swift
Last active December 30, 2022 18:25
Exponential Moving Average
func exponentialMovingAverage(currentAverage: Double, newValue: Double, smoothing: Double) -> Double {
return smoothing * newValue + (1 - smoothing) * currentAverage
}
// Usage:
// var a = 3
// a = exponentialMovingAverage(a, 8, 0.5)
// Swift 2
@berikv
berikv / dispatch_after_swift.codesnippet
Last active August 29, 2015 14:17
Xcode snippet for dispatch_after in Swift. Move this file to ~/Library/Developer/Xcode/UserData/CodeSnippets. Use Finder CMD+Shift+G
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDECodeSnippetCompletionPrefix</key>
<string>dispatch_after</string>
<key>IDECodeSnippetCompletionScopes</key>
<array>
<string>CodeExpression</string>
</array>
@berikv
berikv / example.txt
Created February 20, 2015 10:55
LLDB helper for printing the position of a view inside it's window
(lldb) posinwindow 0x796a0e60
(CGPoint) $58 = (x=711, y=205.5)
@berikv
berikv / log.swift
Last active August 29, 2015 14:13
Simple logging in swift
func log<T>(message: T, fileName: String = __FILE__, lineNumber: Int = __LINE__, args: CVarArgType...) {
let date = NSDate()
let processInfo = NSProcessInfo()
let appName = processInfo.processName
let combined = String(format: String(stringInterpolationSegment: message), arguments: args)
println("\(date) \(appName) [\(fileName.lastPathComponent):\(lineNumber)] \(combined)")
}
@berikv
berikv / networkActivityIndicator
Last active August 29, 2015 14:09
Multiple concurrent networkActivityIndicators
var applicationActivityIndicators = 0
extension UIApplication {
func startActivityIndicator() {
applicationActivityIndicators++
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
}
func stopActivityIndicator() {
applicationActivityIndicators--
if applicationActivityIndicators <= 0 {
@berikv
berikv / gist:6765119
Last active December 24, 2015 07:39
Check iOS version
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
// Load resources for iOS 6.1 or earlier
} else {
// Load resources for iOS 7 or later
}
// But only in XCode 5
@berikv
berikv / heading.py
Last active May 6, 2018 21:24 — forked from timtrueman/heading.py
- Fix wrap to allow for bigger angles - Replace the hardcoded declination correction with an argument
def wrap(angle):
while angle > 2 * pi:
angle -= 2 * pi
while angle < 0:
angle += 2 * pi
return angle
def magnetometer_readings_to_tilt_compensated_heading(bx, by, bz, phi, theta, declinationRadians=0):
""" Takes in raw magnetometer values, pitch and roll and turns it into a tilt-compensated heading value ranging from -pi to pi (everything in this function should be in radians).
The correct value for declinationRadians can be found online: http://magnetic-declination.com/countries.php note that this value is in radians, not in degrees.
@berikv
berikv / UIResponder+sendAction.h
Last active December 15, 2015 07:10
UIResponder sendAction
//
// UIResponder+sendAction.h
// Berik Visschers
//
// Created by Berik Visschers on 2013-03-22.
// Copyright (c) 2013 Xaton. All rights reserved.
//
#import <UIKit/UIKit.h>
@berikv
berikv / updateRunningAverage
Last active December 15, 2015 02:19
Running average as implemented in the Linux/Unix kernels (shown with the `w` command). Usages can be frame rate, accelerometer, etc.
// A factor of 0.5 means that every newValue will have a 50% impact on the average
// Usage: self.average = updateRunningAverage(self.average, newValue, 0.3);
#define updateRunningAverage(average, newValue, factor) (((newValue) * (factor)) + ((average) * (1.0 - (factor))))