Saturday, August 24, 2019

Snippet Code: NSNotification Register and Post


// Register
[[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(paintColorSelected:)
                                                 name:@"PaintColorSelected"
                                               object:nil];



// Post notification (normally in another ViewController
[[NSNotificationCenter defaultCenter] postNotificationName:@"PaintColorSelected" object:paintSelected.paintName];


Wednesday, September 14, 2016

Saturday, July 16, 2016

Code Snippet: UIView Checkered Background Pattern

    float boxes = self.view.frame.size.width/10.0;
    CGRect rect = CGRectMake(0, 0, 2*boxes, 2*boxes);
    UIGraphicsBeginImageContext(rect.size); CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGRect rect1 = CGRectMake(0, 0, 2*boxes, 2*boxes);
    CGRect rect2 = CGRectMake(0, 0, boxes, boxes);
    CGRect rect3 = CGRectMake(boxes, boxes, boxes, boxes);
    CGContextSetRGBFillColor(ctx, 0.5, 0.5, 0.5, 1.0);
    CGContextFillRect(ctx, rect1);
    CGContextSetRGBFillColor(ctx, 0.4, 0.4, 0.4, 1.0);
    CGContextFillRect(ctx, rect2);
    CGContextFillRect(ctx, rect3);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    self.view.backgroundColor = [UIColor colorWithPatternImage:img];

Wednesday, March 23, 2016

Code Snippet: SpriteKit Spring / Bounce (Simple)

A very simple code to produce an almost spring effect to a node. Play around with dropHeight and bounceFactor to get desired effect.

SKAction* bounce = [SKAction sequence:@[
[SKAction moveByX:0 y:dropHeight*bounceFactor duration:0.1f],
[SKAction moveByX:0 y:-dropHeight*bounceFactor duration:0.1f],
[SKAction moveByX:0 y:dropHeight*bounceFactor/2 duration:0.1f],
[SKAction moveByX:0 y:-dropHeight*bounceFactor/2 duration:0.1f]]];

Wednesday, March 16, 2016

Code Snippet: Distance Between 2-Points

- (CGFloat)distanceBetweenTwoPoints:(CGPoint)fromPoint toPoint:(CGPoint)toPoint{
    
    float x = toPoint.x - fromPoint.x;
    float y = toPoint.y - fromPoint.y;
    return sqrt(x * x + y * y);
    

}

Wednesday, March 2, 2016

Code Snippet: AppDelegate Constant

Useful to have to simplify your code if you use many appdelegates references. Of course, you need to import AppDelegate header in each of the main file you want to use this constant.


#import "AppDelegate.h"

#define MY_APPDELEGATE ((AppDelegate*)[UIApplication sharedApplication].delegate)