Skip to content

Instantly share code, notes, and snippets.

View nielsbot's full-sized avatar

nielsbot nielsbot

View GitHub Profile
@nielsbot
nielsbot / gist:ba06510907886bae5ae7
Created May 31, 2015 20:15
simple Core Motion sample
import CoreMotion
public class Main
{
var motionManager:CMMotionActivityManager!
let motionHandler:CMMotionActivityHandler! = { (activity:CMMotionActivity?) -> Void in
let desc = activity?.debugDescription ?? "no activity"
println("activity is \(desc)")
}
@nielsbot
nielsbot / profiler.h
Created February 25, 2012 02:30 — forked from PlacePop/profiler.h
Quick and Dirty Profiler for iPhone
//
// profiler.h
// Quick and dirty profiler
//
// Created by Niels Gabel on 9/1/08.
//
// Copyright 2008-2011 Niels Gabel
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@nielsbot
nielsbot / README
Created February 25, 2012 02:31 — forked from PlacePop/README
Adds support for loading UIImage's from PDF files. I think it's a great way to add resolution-independent graphics to your apps, and also remove the "export to bitmap" step from your workflow. Just save directly from Illustrator!
Adds support for loading UIImage's from PDF files. I think it's a great way to add resolution-independent graphics to your apps, and also remove the "export to bitmap" step from your workflow. Just save directly from Illustrator!
What it does:
- You can pass the name of a PDF file (with or without the '.pdf') to +[UIImage imageNamed:]
- You can use the name of a PDF file inside Interface Builder, in your UIImageView's
- Turn a multi-page PDF into an array of UIImage's... good for animation.
Feedback to gists @nielsbot.com. Thanks!
@nielsbot
nielsbot / SetViewCenter.m
Created March 15, 2012 00:33
Pixel align your UIViews even when using the center property
@implementation UIView (PositionAtCenter)
-(void)centerPixelAligned:(CGPoint)p
{
CGSize size = self.bounds.size ;
p.x = floorf( p.x ) + 0.5 * fmodf( size.width, 2.0f ) ;
p.y = floorf( p.y ) + 0.5 * fmodf( size.height, 2.0f ) ;
self.center = p;
@nielsbot
nielsbot / PropertyContainer.h
Created April 12, 2012 20:32
PropertyContainer
//
// PropertyContainer.h
//
// Created by Niels Gabel on 12/14/11.
// Copyright (c) 2011 DoubleDutch Inc. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface PropertyContainer : NSObject
@nielsbot
nielsbot / AppDelegate.h
Created July 18, 2012 22:05
How to overlay a transparent window on the system keyboard
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) IBOutlet UIWindow *window;
@property ( strong ) IBOutlet UITextField * textField ;
@end
@nielsbot
nielsbot / gist:4669657
Last active December 11, 2015 22:28
Tired of dealing with NSNull in your KVO observers? I use this.
@implementation NSNull (IfNullThenNil)
-(id)ifNullThenNil { return nil ; }
@end
@implementation NSObject (IfNullThenNil)
-(id)ifNullThenNil { return self ; }
@end
@nielsbot
nielsbot / gist:4678311
Last active December 11, 2015 23:39
Create CGColor from HTML/CSS/Hex color string
static CGColorRef CGColorCreateWithCSSColor( NSString * string )
{
union {
struct {
#if LITTLE_ENDIAN
uint8 alpha ;
uint8 blue ;
uint8 green ;
uint8 red ;
#else
@nielsbot
nielsbot / gist:5294660
Last active December 15, 2015 17:09
Make sure your layer is pixel-aligned when setting it's position. (Avoids things looking blurry)
// when setting the position of a layer, use this:
// layer.position = [ layer pixelAlignedPostionForPoint:<originalPoint> ] ;
@implementation CALayer (SetPositionPixelAligned)
-(CGPoint)pixelAlignedPositionForPoint:(CGPoint)p
{
CGSize size = self.bounds.size ;
CGPoint anchorPoint = self.anchorPoint ;
@nielsbot
nielsbot / gist:6873377
Last active December 24, 2015 22:29
A helper to make KVO observing easier. You can use a block as your observer, and it automatically deregisters itself when the observed object is being deallocated. (I think tracking all KVO observers so you can unregister them is a major weakness of the KVO API)I'm sure there are bugs... Please credit me if you use this code.
// NWGKeyValueObserving.h
// by nielsbot ( nielsbot@nielsbot.com)
typedef void (^KeyValueObserverBlock)( NSDictionary * change ) ;
@interface NWGKeyValueObserver : NSObject
@property ( nonatomic, readonly, copy ) NSString * keyPath ;
@property ( nonatomic, readonly ) id target ;
@property ( nonatomic, readonly ) NSKeyValueObservingOptions options ;