Saturday, 2 June 2012

CGRect with origin selection


 enum originSelection Discription

 1 2 3
 4 5 6
 7 8 9

 1 : kOriginSelectionDefault, kOriginSelectionTopLeft


 2 : kOriginSelectionTopMid



 3 : kOriginSelectionTopRight

 4 : kOriginSelectionMidLeft

 5 : kOriginSelectionMidMid, kOriginSelectionCenter, kOriginSelectionCentre

 6 : kOriginSelectionMidRight

 7 : kOriginSelectionBottomLeft

 8 : kOriginSelectionBottomMid

 9 : kOriginSelectionBottomRight




 Default selection is 1 point calculation



---- MyCGRect.h

//
//  MyCGRect.h
//  WhatzzApp
//
//  Created by Gaurav D. Sharma on 02/06/12.
//  Copyright (c) 2012 KIPL. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface MyCGRect : NSObject


/**

 enum originSelection Discription

 1 2 3
 4 5 6
 7 8 9

 1 : kOriginSelectionDefault, kOriginSelectionTopLeft

 2 : kOriginSelectionTopMid

 3 : kOriginSelectionTopRight

 4 : kOriginSelectionMidLeft

 5 : kOriginSelectionMidMid, kOriginSelectionCenter, kOriginSelectionCentre

 6 : kOriginSelectionMidRight

 7 : kOriginSelectionBottomLeft

 8 : kOriginSelectionBottomMid

 9 : kOriginSelectionBottomRight

 Default selection is 1 point calculation

 */

enum originSelection{
    kOriginSelectionDefault,
    kOriginSelectionTopLeft,
    kOriginSelectionTopMid,
    kOriginSelectionTopRight,
    kOriginSelectionMidLeft,
    kOriginSelectionMidMid,
    kOriginSelectionCenter,
    kOriginSelectionCentre,
    kOriginSelectionMidRight,
    kOriginSelectionBottomLeft,
    kOriginSelectionBottomMid,
    kOriginSelectionBottomRight
};

+ (CGRect)CGRectMake:(CGRect)rect
 withOriginSelection:(enum originSelection)originSelected;

CGRect CGRectMakeWithOriginSelection(CGRect,enum originSelection);


+ (CGRect)CGRectMakeWithPoint:(CGPoint)point
                     withSize:(CGSize)size
          withOriginSelection:(enum originSelection)originSelected;

CGRect CGRectMakeWithPoint_Size(CGPoint,CGSize,enum originSelection);


+ (CGRect)CGRectMakeWithX:(CGFloat)x
                    withY:(CGFloat)y
                withWidth:(CGFloat)width
               withHeight:(CGFloat)height
      withOriginSelection:(enum originSelection)originSelected;

CGRect CGRectMakeWithX_Y_Width_Height(CGFloat,CGFloat,CGFloat,CGFloat,enum originSelection);

@end

------ MyCGRect.m
//
//  MyCGRect.m
//
//  Created by Gaurav D. Sharma on 02/06/12.
//  Copyright (c) 2012 KIPL. All rights reserved.
//

#import "MyCGRect.h"

@implementation MyCGRect

+ (CGRect)CGRectMake:(CGRect)rect
 withOriginSelection:(enum originSelection)originSelected{
    return CGRectMakeWithX_Y_Width_Height(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height, originSelected);
}

CGRect CGRectMakeWithOriginSelection(CGRect rect,enum originSelection originSelected){
    return CGRectMakeWithX_Y_Width_Height(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height, originSelected);
}


+ (CGRect)CGRectMakeWithPoint:(CGPoint)point
                     withSize:(CGSize)size
          withOriginSelection:(enum originSelection)originSelected{
    return CGRectMakeWithX_Y_Width_Height(point.x, point.y, size.width, size.height, originSelected);
}

CGRect CGRectMakeWithPoint_Size(CGPoint point,CGSize size,enum originSelection originSelected){
    return CGRectMakeWithX_Y_Width_Height(point.x, point.y, size.width, size.height, originSelected);
}

+ (CGRect)CGRectMakeWithX:(CGFloat)x
                    withY:(CGFloat)y
                withWidth:(CGFloat)width
               withHeight:(CGFloat)height
      withOriginSelection:(enum originSelection)originSelected{
    return CGRectMakeWithX_Y_Width_Height(x, y, width, height, originSelected);   
}

CGRect CGRectMakeWithX_Y_Width_Height(CGFloat x,CGFloat y,CGFloat width,CGFloat height,enum originSelection originSelected){
   
    switch (originSelected) {
        case kOriginSelectionDefault:
        case kOriginSelectionTopLeft:
        default:
            return CGRectMake(x, y, width, height);
            break;
    }      
   
    CGFloat orignalX;
    CGFloat orignalY;
   
    switch (originSelected) {
        case kOriginSelectionTopMid:
            orignalX = x - (width / 2);
            orignalY = y;
            break;
           
        case kOriginSelectionTopRight:
            orignalX = x - width;
            orignalY = y;
            break;
           
        case kOriginSelectionMidLeft:
            orignalX = x;
            orignalY = y - (height / 2);
            break;
           
        case kOriginSelectionMidMid:
        case kOriginSelectionCenter:
        case kOriginSelectionCentre:
            orignalX = x - (width / 2);
            orignalY = y - (height / 2);
            break;
           
        case kOriginSelectionMidRight:
            orignalX = x - width;
            orignalY = y - (height / 2);
            break;
           
        case kOriginSelectionBottomLeft:
            orignalX = x;
            orignalY = y - height;
            break;
           
        case kOriginSelectionBottomMid:
            orignalX = x - (width / 2);
            orignalY = y - height;
            break;
           
        case kOriginSelectionBottomRight:
            orignalX = x - width;
            orignalY = y - height;
            break;
           
        default:
            orignalX = x;
            orignalY = y;
            break;
    }
   
    return CGRectMake(orignalX, orignalY, width, height);
}

@end


----   Prefix.pch

#import "MyCGRect.h"


=====================
Enjoy & thank you for using it.

No comments:

Post a Comment