Skip to content

Instantly share code, notes, and snippets.

@mundstein
mundstein / AppDelegate.m
Created December 22, 2011 08:41
Cool objective-c code spy
// Override this method in any class to get a pretty list of
// all methods that are being called - very useful.
// Try it on the AppDelegate for example
-(BOOL)respondsToSelector:(SEL)aSelector {
printf("The spy says: %s\n", [NSStringFromSelector(aSelector) UTF8String]);
return [super respondsToSelector:(aSelector)];
}
@mundstein
mundstein / TableViewController.m
Created December 22, 2011 14:01
Horizontal Tableview e.g. Image browser with minimal code
// Make a picture browser, just like in the Photo app
// **With only six additional lines of code!**
// in viewDidLoad:
self.tableView.transform = CGAffineTransformMakeRotation(-M_PI/2); // turn the table 90° anti-clockwise
self.tableView.showsVerticalScrollIndicator = NO;
self.tableView.pagingEnabled = YES;
@mundstein
mundstein / CustomView.m
Created February 12, 2012 11:17
Log the frame changes of a custom view
// override this method to get a great log output
// to show how the view is resized when rotating,
// calling sizeToFit and other methods that affect the frame
- (void) setFrame:(CGRect)frame {
[super setFrame:frame];
NSLog(@"%@ - %@", NSStringFromSelector(_cmd), NSStringFromCGRect(frame));
}
@mundstein
mundstein / Date.m
Created March 18, 2012 14:43
Objective-C method to return last or next Monday from date.
-(NSDate *) lastMondayBeforeDate:(NSDate*)timeStamp {
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps =
[gregorian components:NSWeekdayCalendarUnit fromDate:timeStamp];
NSInteger weekday = [comps weekday];
weekday = weekday==1 ? 6 : weekday-2; // start with 0 on Monday rather than 1 on Sunday
NSTimeInterval secondsSinceMondayMidnight =
(NSUInteger) [timeStamp timeIntervalSince1970] % 60*60*24 +
weekday * 60*60*24;
return [timeStamp dateByAddingTimeInterval:-secondsSinceMondayMidnight];
@mundstein
mundstein / NSObject+Block.m
Created April 22, 2012 19:19
Extend NSObject to perform block after delay
@implementation NSObject (PerformBlockAfterDelay)
- (void)performBlock:(void (^)(void))block
afterDelay:(NSTimeInterval)delay
{
block = [[block copy] autorelease];
[self performSelector:@selector(fireBlockAfterDelay:)
withObject:block
afterDelay:delay];
}
@mundstein
mundstein / shadow.m
Created May 10, 2012 12:47
Create a fancy shadow^
/*
* Create a fancy shadow around the frontView.
*
* Note: UIBezierPath needed because shadows are evil. If you don't use the path, you might not
* not notice a difference at first, but the keen eye will (even on an iPhone 4S) observe that
* the interface rotation _WILL_ lag slightly and feel less fluid than with the path.
*/
UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:self.frontView.bounds];
self.frontView.layer.masksToBounds = NO;
self.frontView.layer.shadowColor = [UIColor blackColor].CGColor;
@mundstein
mundstein / ShadowButton.h
Created June 23, 2012 11:54
Button with drop shadow for iPhone which also animates down when pressed.
//
// ShadowButton.h
#import <Foundation/Foundation.h>
@interface ShadowButton : UIButton {
}
@end
@mundstein
mundstein / Localize.m
Created August 27, 2012 21:01
Easy way to localize independent of user setting
// The main localization class that does the hard work
-(NSString *) localized:(NSString *) key
{
// langCode should be set as a global variable somewhere
NSString *path = [[NSBundle mainBundle] pathForResource:langCode ofType:@"lproj"];
NSBundle* languageBundle = [NSBundle bundleWithPath:path];
return [languageBundle localizedStringForKey:key value:@"" table:nil];;
}
@mundstein
mundstein / UIImage+Masking.m
Created October 29, 2012 11:34
Great simple masking image routine for core graphics
- (UIImage*) maskImage:(UIImage *)im withMask:(UIImage *)maskImage {
CGImageRef maskRef = maskImage.CGImage;
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, false);
@mundstein
mundstein / quiver.m
Last active December 15, 2015 03:49
View Quiviring for iOS, like in Springboard. Ideal for collection views before deleting.
// INSERT CELL ANIMATION SNIPPET HERE
- (void)startQuivering
{
CABasicAnimation *quiverAnim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
float startAngle = (-2) * M_PI/180.0;
float stopAngle = -startAngle;
quiverAnim.fromValue = [NSNumber numberWithFloat:startAngle];
quiverAnim.toValue = [NSNumber numberWithFloat:3 * stopAngle];
quiverAnim.autoreverses = YES;
quiverAnim.duration = 0.2;