Skip to content

Instantly share code, notes, and snippets.

View jeksys's full-sized avatar

Eugene Yagrushkin jeksys

View GitHub Profile
@jeksys
jeksys / gist:1081144
Created July 13, 2011 19:32
dispatch_async Useful pattern to avoid blocking the main thread
Nested invocations provide asynchronous callbacks
• Communication between subsystems
-(IBAction)onClick:(NSButton *)sender
{
dispatch_async(account->queue,
^{
NSImageRep *image = renderAccountStatement(account);
dispatch_async(dispatch_get_main_queue(),^{
@jeksys
jeksys / gist:1077945
Created July 12, 2011 13:11
Singleton Objects - Lazy ini
Singleton Objects
Lazy initialization is common
@implementation FooController
// #1
+ (FooController *)sharedInstance
{
static dispatch_once_t once;
static FooController *instance;
dispatch_once(&once, ^{
@jeksys
jeksys / gist:1072578
Created July 8, 2011 19:15
scaleImage
- (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)newSize
{
UIGraphicsBeginImageContextWithOptions(newSize, YES, 0.0);
CGRect imageRect = {{0.0, 0.0}, newSize};
[image drawInRect:imageRect];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
@jeksys
jeksys / UISearchBar.m
Created July 7, 2011 19:53
Detecting when clear is clicked in UISearchBar (X button)
- (void)viewDidLoad {
//find the UITextField view within searchBar (outlet to UISearchBar)
//and assign self as delegate
for (UIView *view in searchBar.subviews){
if ([view isKindOfClass: [UITextField class]]) {
UITextField *tf = (UITextField *)view;
tf.delegate = self;
break;
}
}
@jeksys
jeksys / gist:1065330
Created July 5, 2011 17:22
Vibration
AudioToolbox.framework
#import <AudioToolbox/AudioServices.h>
Now use this line of code everytime you want to make the phone vibrate:
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
@jeksys
jeksys / gist:1049759
Created June 27, 2011 20:33
drawRect
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext(); // get current context
// CGFloat object_size = sizeStatus/2;
// CGFloat sx, sy, fsize = object_size;
// sx = rect.size.width - fsize;
// sy = fsize;
// CGContextMoveToPoint(context, sx, sy);
@jeksys
jeksys / gist:1049376
Created June 27, 2011 17:57
Full screen
[self setWantsFullScreenLayout:YES];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
[[UIApplication sharedApplication] setStatusBarHidden:ControlsHidden];
@jeksys
jeksys / gist:964506
Created May 10, 2011 13:50
List Of Fonts in the system
NSArray *names = [UIFont familyNames];
for (NSString *fontName in names) {
NSLog(@"%@:", fontName);
NSArray *fontnames = [UIFont fontNamesForFamilyName:fontName];
for (NSString *fontNameinFamily in fontnames) {
NSLog(@"-->%@", fontNameinFamily);
}
}
@jeksys
jeksys / gist:963263
Created May 9, 2011 19:58
Delegate template
@protocol SomeClassDelegate <NSObject>
- (void) SomeClassSelector:(ParamType*)param;
@end
@interface SomeClass : UIViewController {
id <SomeClassDelegate> delegate;
@jeksys
jeksys / gist:957049
Created May 5, 2011 13:43
imageScaledToSize
@interface UIImage (TPAdditions)
- (UIImage*)imageScaledToSize:(CGSize)size;
@end
@implementation UIImage (TPAdditions)
- (UIImage*)imageScaledToSize:(CGSize)size {
UIGraphicsBeginImageContext(size);
[self drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();