Skip to content

Instantly share code, notes, and snippets.

@cobysy
cobysy / noDelayOnContentTouchesInTableViewCell.m
Last active August 29, 2015 14:02
Allow controls in a UITableViewCell to immediately react to touches in iOS 7 (credit: http://stackoverflow.com/questions/19256996/uibutton-not-showing-highlight-on-tap-in-ios7)
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
for (id obj in cell.subviews) {
if ([NSStringFromClass([obj class]) isEqualToString:@"UITableViewCellScrollView"]) {
UIScrollView* scroll = (UIScrollView*)obj;
scroll.delaysContentTouches = NO;
break;
}
@cobysy
cobysy / resumeRotationOnEnterForeground.m
Created June 10, 2014 07:04
Resume animation rotation on leaving the background state to becoming the active app.
- (void)registerForAppStateNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
}
- (void)applicationDidEnterBackground
{
self.suspendedRotation = [self.imageView.layer.presentationLayer valueForKeyPath:@"transform.rotation.z"];
}
@cobysy
cobysy / snapTableViewToTopCell.m
Created June 10, 2014 08:03
Snap table to top most cell when dragging
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
// if decelerating, let scrollViewDidEndDecelerating: handle it
if (decelerate == NO)
[self snapTable];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[self snapTable];
}
export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced
export PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
alias ls="ls -lA"
@cobysy
cobysy / drawNSImage.m
Created June 24, 2014 16:51
Draw NSImage (off-screen)
NSBitmapImageRep* offscreenRep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:NULL
pixelsWide:size.width
pixelsHigh:size.height
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSCalibratedRGBColorSpace
bytesPerRow:0
@cobysy
cobysy / distanceToNSColor.m
Created June 27, 2014 12:13
Calculate color similarity/distance in RGBA color space
@implementation NSColor (DDExtensions)
- (float)distanceTo:(NSColor*)color
{
// Calculate color similarity/distance in RGBA color space
// http://stackoverflow.com/questions/4754506/color-similarity-distance-in-rgba-color-space
// max((r₁-r₂)², (r₁-r₂ - a₁+a₂)²) +
// max((g₁-g₂)², (g₁-g₂ - a₁+a₂)²) +
// max((b₁-b₂)², (b₁-b₂ - a₁+a₂)²)
@cobysy
cobysy / deviceSize.m
Created July 3, 2014 22:50
Get device size on iOS device
- (CGSize)deviceSize
{
CGSize size = [UIScreen mainScreen].bounds.size;
if ([UIScreen instancesRespondToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] > 1) {
size = CGSizeMake(size.width * 2, size.height * 2); // Retina screenshot size support
}
return size;
}
@cobysy
cobysy / build-libpng-xcode.sh
Last active April 11, 2019 19:07
Build libpng with Xcode
# install command line developer tools
xcode-select --install
# update macports
sudo port selfupdate; sudo port upgrade outdated
# install cmake
sudo port install cmake
# download and extract libpng
@cobysy
cobysy / AFNetworkingGDataXMLHTMLResponseSerializer.m
Last active August 29, 2015 14:04
An XML and HTML response serializer for AFNetworking 2.0, using GDataXML-HTML
//
// AFNetworkingGDataXMLHTMLResponseSerializer.m
//
// Created by _ on 17/07/14.
// Copyright (c) 2014 cobysy. All rights reserved.
//
#import "AFNetworkingGDataXMLHTMLResponseSerializer.h"
#import <GDataXML-HTML/GDataXMLNode.h>
// memoized Levenshtein Distance
// description given here: http://programmingpraxis.com/2014/09/12/levenshtein-distance/
import Foundation
// memoize for a two parameter recursive function
func memoize<T1: Hashable, T2: Hashable, U>(body: ((T1, T2) -> U, T1, T2) -> U) -> ((T1, T2) -> U) {
var memo = [T1: [T2: U]]()
var result: ((T1, T2) -> U)!
result = {