Skip to content

Instantly share code, notes, and snippets.

View ksm's full-sized avatar
🏔️

Karol S. Mazur ksm

🏔️
View GitHub Profile
@ksm
ksm / gist:3689409
Created September 10, 2012 07:24
Enum with Fixed Underlying Type
/*
Enum with Fixed Underlying Type
New with Xcode 4.4
Via WWDC2012 Session 405 - Modern Objective-C
Results in better code completion and stronger type checking.
Use -Wconversion compiler flag to check for enum type errors.
-Wswitch for checking if switch statement is fully handled for enum.
*/
@ksm
ksm / gist:3029763
Created July 1, 2012 21:57
Batching up database changes into a single save
// Source: Stanford CS193P November 16, 2010 Lecture 16
// Use case: lots of Core Data changes. You want them to save once they settle down.
- (void)delayedSave:(NSManagedObjectContext *)ctxt
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(doSave:) object:ctxt];
[self performSelector:@selector(doSave:) withObject:ctxt afterDelay:1.0];
}
@ksm
ksm / .gitignore
Created May 16, 2012 12:01 — forked from kluivers/.gitignore
.gitignore for Xcode projects
# .gitignore in use by Joris Kluivers
#
# Latest version:
# https://gist.github.com/gists/1923197
*.DS_Store
# Xcode
*.pbxuser
*.mode1v3
@ksm
ksm / gist:2152929
Created March 21, 2012 21:18
KVO granular notifications on NSMutableArray
/*
Thanks to @_bejo
*/
- (void)_insertGlobalRoomMessage:(id)msg atIndex:(NSUInteger)index
{
NSIndexSet *changeSet = [NSIndexSet indexSetWithIndex:index];
[self willChange:NSKeyValueChangeInsertion valuesAtIndexes:changeSet forKey:@"globalRoomMessages"];
[(NSMutableArray *)self.globalRoomMessages insertObject:msg atIndex:index];
[self didChange:NSKeyValueChangeInsertion valuesAtIndexes:changeSet forKey:@"globalRoomMessages"];
@ksm
ksm / gist:1875946
Created February 21, 2012 11:20
CALayer shadowPath done the right way
/*
Source: Apple Developer - Understanding iOS View Compositing
*/
// setup the layer
CALayer *layer = view.layer;
layer.bounds = sublayer_bounds;
layer.backgroundColor = random_color();
// set the shadow properties on the layer
@ksm
ksm / gist:1869980
Created February 20, 2012 16:23
CALayer masking in drawLayer:inContext (to avoid layer.cornerRadius performance hit)
/*
Source: Apple Developer - Understanding iOS View Compositing
Note: setting view.layer.cornerRadius and .masksToBounds
sends the view for rending to an offscreen buffer.
We want to avoid unnecessary rendering passes.
Let the context do the work.
*/
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
CGRect rect = layer.bounds;
@ksm
ksm / gist:1869823
Created February 20, 2012 15:57
CALayer draw resized image (kill all misaligned images)
/*
Source: Apple Developer - Understanding iOS View Compositing
*/
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
UIImage *image = [self loadImage];
if (image != nil) {
CGSize s = image.size;
CGRect r = layer.bounds;
@ksm
ksm / debug.h
Created February 20, 2012 11:00
Xcode: how to debug the right way
// via @_bejo
// then just add a DEBUG = 1 to debug target in xcode
#ifdef DEBUG
#define DebugLog( s, ... ) NSLog( @"<%@:(%d)> %@", [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )
#else
#define DebugLog( s, ... )
@ksm
ksm / UINavigationController+Fade.h
Created February 14, 2012 22:23
UINavigationController custom pop/push transition animation
/*
Copied and pasted from David Hamrick's blog:
Source: http://www.davidhamrick.com/2011/12/31/Changing-the-UINavigationController-animation-style.html
*/
@interface UINavigationController (Fade)
- (void)pushFadeViewController:(UIViewController *)viewController;
- (void)fadePopViewController;
@ksm
ksm / DrawView.h
Created February 14, 2012 21:59
DrawView with a block for drawRect: an alternative to subclassing UIView for simple drawing operations
/*
Copied and pasted from David Hamrick's blog.
Source: http://www.davidhamrick.com/2011/08/07/using-blocks-for-drawing-to-avoid-subclasssing-in-objective-c.html
*/
typedef void(^DrawView_DrawBlock)(UIView* v,CGContextRef context);
@interface DrawView : UIView