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];
}
@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 / 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 = {
@cobysy
cobysy / How to patch a library imported with Cocoapods
Created March 26, 2015 15:28
How to patch a library imported with Cocoapods
How to patch a library imported with Cocoapods
Forking the library, applying your patch, and pointing to your fork in the Podfile would be your best option.
If the library contains the podspec:
```
pod '<library>', :git => 'https://github.com/yourname/<library>.git'
```
If the library does not contain the podspec, you have to copy the podspec to a local path and adjust it:
let fontFamilyNames = UIFont.familyNames()
for familyName in fontFamilyNames {
println("------------------------------")
println("Font Family Name = [\(familyName)]")
let names = UIFont.fontNamesForFamilyName(familyName as! String)
println("Font Names = [\(names)]")
}
@cobysy
cobysy / recursiveFlatMap.swift
Created April 15, 2015 11:55
recursiveFlatMap.swift
// Copyright (c) 2015 bysy.io. All rights reserved.
func recursiveFlatMap<TResult>(#root: AnyObject,
@noescape children: (AnyObject) -> [AnyObject]) -> [TResult]
{
var result = [TResult]()
if let value = root as? TResult {
result.append(value)
}
result += children(root).flatMap( { recursiveFlatMap(root: $0, children: children) as [TResult] } )