Skip to content

Instantly share code, notes, and snippets.

@greglecki
greglecki / AccelerometerPlayerControl.swift
Last active November 26, 2015 16:30
Swift - Core Motion for control the player in portrait orientation
import CoreMotion
let motionManager = CMMotionManager()
var xAcceleration = CGFloat(0)
func setupCoreMotion() {
motionManager.accelerometerUpdateInterval = 0.2
let queue = NSOperationQueue()
motionManager.startAccelerometerUpdatesToQueue(queue, withHandler: {
accelerometerData, error in
@greglecki
greglecki / RandomCloud.swift
Created January 21, 2016 08:59
Generate a random cloud.
import UIKit
import XCPlayground
func generateRandomCloud() -> UIImage {
func randomInt(lower lower: Int, upper: Int) -> Int {
assert(lower < upper)
return lower + Int(arc4random_uniform(UInt32(upper - lower)))
}
@greglecki
greglecki / jigsawPieceMaker.swift
Created January 21, 2016 09:14
Create Jigsaw Piece
import UIKit
import XCPlayground
enum Edge {
case Outie
case Innie
case Flat
}
@greglecki
greglecki / Async Await with WhenAll.cs
Created April 14, 2016 13:35
Bundle multiple asynchronous calls which helps with performance.
var testRepo = new TestRepo();
//var start = DateTime.Now;
var t1 = testRepo.Task1();
var t2 = testRepo.Task2();
int[] r = await Task.WhenAll(t1, t2);
//var end = (DateTime.Now - start).Seconds;
return new int[] { r[0], r[1] };
/***************************/
public class TestRepo
@greglecki
greglecki / Explore swift type in terminal
Created April 15, 2016 10:45
Explore any type in the swift standard library.
// Example how to explore a Character type
echo ":type lookup Character" | swift | less
@greglecki
greglecki / Resource.swift
Last active June 1, 2016 08:00
Generic way of loading data from the network.
struct Resource<A> {
let pathComponent: String
let parse: AnyObject -> A?
}
extension Resource {
func loadAsynchronous(callback: A? -> ()) {
let session = NSURLSession.sharedSession()
let resourceURL = webserviceURL.URLByAppendingPathComponent(pathComponent)
session.dataTaskWithURL(resourceURL) {
@greglecki
greglecki / Access root view controller
Created April 20, 2016 07:48
RootViewController.swift
UIApplication.sharedApplication().windows[0].rootViewController!.presentViewController(alert, animated: true, completion: nil)
@greglecki
greglecki / Profiling.cs
Last active April 22, 2016 09:29
Check execution time.
using System.Diagnostics;
private readonly Stopwatch stopwatch;
// Initialize should be done in class constructor
stopwatch = new Stopwatch();
// Usage
stopwatch.Start();
// Metod/algoritm to execute
stopwatch.Stop();
@greglecki
greglecki / ColorIncrease.swift
Created May 18, 2016 08:11
Create color which increase gradually
for index in 0..<array.count {
let red = CGFloat(Float(index) / Float(sortedResults.count))
let green = CGFloat(1.0 - (Float(index) / Float(sortedResults.count)))
let color = NSColor(calibratedRed: red, green: green, blue: 0.0, alpha: 1.0)
}
@greglecki
greglecki / GetImageWebAPI.cs
Created June 23, 2016 09:22
Returns image as response message via Web API
public HttpResponseMessage GetImage()
{
byte[] bytes = System.IO.File
.ReadAllBytes
(
HttpContext.Current.Server
.MapPath("~/Images/default.jpg")
);
var result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new ByteArrayContent(bytes);