Skip to content

Instantly share code, notes, and snippets.

View nielsbot's full-sized avatar

nielsbot nielsbot

View GitHub Profile
@nielsbot
nielsbot / tableview.swift
Last active August 20, 2022 17:54
How to handle the iOS keyboard in your UITableView
import UIKit
import CoreGraphics
// our Cell class
class Cell : UITableViewCell
{
// cell reuse identifier for table view
static let Identifier = "Cell"
// the object the cell represents/displays. Could be anything you like
@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 / 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 ;
@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:5155671
Last active April 2, 2022 08:07
A wrapper around kqueue to monitor changes to files. Works on iOS.
#import "FileChangeObserver.h"
#undef Assert
#define Assert(COND) { if (!(COND)) { raise( SIGINT ) ; } }
@interface FileChangeObserver ()
@property ( nonatomic, readonly ) int kqueue ;
@property ( nonatomic ) enum FileChangeNotificationType typeMask ;
@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: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 / MemoryMappedDataConsumer.h
Last active August 8, 2018 01:13
memory mapped core graphics data consumer for writing PDFs
#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
@interface MemoryMappedDataConsumer : NSObject
@property ( nonatomic, readonly ) size_t size ;
@property ( nonatomic, readonly ) size_t capacity ;
@property ( nonatomic, readonly ) CGDataConsumerRef CGDataConsumer ;
@property ( nonatomic, readonly, copy ) NSURL * url ;
@nielsbot
nielsbot / CGPathCreateRoundRect.m
Created August 26, 2012 23:47
Function to create a round rect CGPath
CGPathRef CGPathCreateRoundRect( const CGRect r, const CGFloat cornerRadius )
{
CGMutablePathRef p = CGPathCreateMutable() ;
CGPathMoveToPoint( p, NULL, r.origin.x + cornerRadius, r.origin.y ) ;
CGFloat maxX = CGRectGetMaxX( r ) ;
CGFloat maxY = CGRectGetMaxY( r ) ;
CGPathAddArcToPoint( p, NULL, maxX, r.origin.y, maxX, r.origin.y + cornerRadius, cornerRadius ) ;
@nielsbot
nielsbot / procinfo.c
Created August 24, 2012 20:05
Get number of threads (running) in all tasks on OS X
#import <libproc.h>
#import <stdlib.h>
#import <stdio.h>
int main( int argc, const char * argv[])
{
pid_t * pids = calloc(0x4000, 1);
int count = proc_listallpids(pids, 0x4000);
printf("count=%u\n", count) ;