Skip to content

Instantly share code, notes, and snippets.

@mindbrix
Created June 3, 2013 16:24
Show Gist options
  • Save mindbrix/5699333 to your computer and use it in GitHub Desktop.
Save mindbrix/5699333 to your computer and use it in GitHub Desktop.
UIView+Geometry
//
// UIView+Geometry.h
// Swipe To Scrub
//
// Created by Nigel Barber on 29/10/2012.
// Copyright (c) 2012 Mindbrix. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIView (Geometry)
+(void)logPoint:(CGPoint)point :(NSString *)name;
+(void)logRect:(CGRect)rect :(NSString *)name;
+(void)logSize:(CGSize)size :(NSString *)name;
+(CGRect)normalizeRect:(CGRect)rect;
+(CGRect)scaleRect:(CGRect)rect toScale:(float)scale;
-(void)centre;
-(void)centreHorizontally;
-(void)centreVertically;
-(CGPoint)centreFromRect:(CGRect)rect;
-(void)logCenter;
-(void)logCenter:(NSString *)name;
-(void)logBounds;
-(void)logBounds:(NSString *)name;
-(void)logFrame;
-(void)logFrame:(NSString *)name;
@property( nonatomic, readonly ) float aspectRatio;
@property( nonatomic, assign ) float centreX;
@property( nonatomic, assign ) float centreY;
@property( nonatomic, readonly ) CGPoint frameCentre;
@property( nonatomic, readonly ) BOOL isHorizontal;
@property( nonatomic, readonly ) float maximumX;
@property( nonatomic, readonly ) float maximumY;
@property( nonatomic, assign ) CGPoint origin;
@property( nonatomic, assign ) float originX;
@property( nonatomic, assign ) float originY;
@property( nonatomic, assign ) CGSize frameSize;
@property( nonatomic, assign ) float sizeWidth;
@property( nonatomic, assign ) float sizeHeight;
@end
//
// UIView+Geometry.m
// Swipe To Scrub
//
// Created by Nigel Barber on 29/10/2012.
// Copyright (c) 2012 Mindbrix. All rights reserved.
//
#import "UIView+Geometry.h"
@implementation UIView (Geometry)
@dynamic aspectRatio;
@dynamic centreX;
@dynamic centreY;
@dynamic frameCentre;
@dynamic isHorizontal;
@dynamic maximumX;
@dynamic maximumY;
@dynamic origin;
@dynamic originX;
@dynamic originY;
#pragma mark - Logging
+(void)logPoint:(CGPoint)point :(NSString *)name
{
NSLog( @"%@ = %@", name, NSStringFromCGPoint( point ));
}
+(void)logRect:(CGRect)rect :(NSString *)name
{
NSLog( @"%@ = %@", name, NSStringFromCGRect( rect ));
}
+(void)logSize:(CGSize)size :(NSString *)name
{
NSLog( @"%@ = %@", name, NSStringFromCGSize( size ));
}
-(void)logCenter
{
[ self logCenter:@"Center" ];
}
-(void)logCenter:(NSString *)name
{
[[ self class ] logPoint:self.center :name ];
}
-(void)logBounds
{
[ self logBounds:@"Bounds" ];
}
-(void)logBounds:(NSString *)name
{
[[ self class ] logRect:self.bounds :name ];
}
-(void)logFrame
{
[ self logFrame:@"Frame" ];
}
-(void)logFrame:(NSString *)name
{
[[ self class ] logRect:self.frame :name ];
}
#pragma mark - Scaling
+(CGRect)normalizeRect:(CGRect)rect
{
return [ UIView scaleRect:rect toScale:1.0f / rect.size.height ];
}
+(CGRect)scaleRect:(CGRect)rect toScale:(float)scale
{
return CGRectMake( rect.origin.x * scale, rect.origin.y * scale, rect.size.width * scale, rect.size.height * scale );
}
#pragma mark - Layout
-(void)centre
{
self.origin = CGPointMake(( self.superview.bounds.size.width - self.bounds.size.width ) / 2.0f, ( self.superview.bounds.size.height - self.bounds.size.height ) / 2.0f );
}
-(void)centreHorizontally
{
self.originX = ceilf(( self.superview.bounds.size.width - self.bounds.size.width ) / 2.0f );
}
-(void)centreVertically
{
self.originY = ceilf(( self.superview.bounds.size.height - self.bounds.size.height ) / 2.0f );
}
-(CGPoint)centreFromRect:(CGRect)rect
{
return CGPointMake( rect.origin.x + rect.size.width / 2.0f, rect.origin.y + rect.size.height / 2.0f );
}
#pragma mark - Properties
-(float)aspectRatio
{
return self.bounds.size.width / self.bounds.size.height;
}
-(float)centreX
{
return self.center.x;
}
-(void)setCentreX:(float)centreX
{
self.center = CGPointMake( centreX, self.center.y );
}
-(float)centreY
{
return self.center.y;
}
-(void)setCentreY:(float)centreY
{
self.center = CGPointMake( self.center.x, centreY );
}
-(CGPoint)frameCentre
{
return [ self centreFromRect:self.frame ];
}
-(BOOL)isHorizontal
{
return ( self.aspectRatio > 1.0f );
}
-(float)maximumX
{
return self.frame.origin.x + self.frame.size.width;
}
-(float)maximumY
{
return self.frame.origin.y + self.frame.size.height;
}
-(CGPoint)origin
{
return self.frame.origin;
}
-(void)setOrigin:(CGPoint)origin
{
CGRect frame = self.frame;
frame.origin = origin;
self.frame = frame;
}
-(float)originX
{
return self.frame.origin.x;
}
-(void)setOriginX:(float)originX
{
CGRect frame = self.frame;
frame.origin.x = originX;
self.frame = frame;
}
-(float)originY
{
return self.frame.origin.y;
}
-(void)setOriginY:(float)originY
{
CGRect frame = self.frame;
frame.origin.y = originY;
self.frame = frame;
}
-(CGSize)frameSize
{
return self.frame.size;
}
-(void)setFrameSize:(CGSize)size
{
CGRect frame = self.frame;
frame.size = size;
self.frame = frame;
}
-(float)sizeWidth
{
return self.frame.size.width;
}
-(void)setSizeWidth:(float)sizeWidth
{
CGRect frame = self.frame;
frame.size.width = sizeWidth;
self.frame = frame;
}
-(float)sizeHeight
{
return self.frame.size.height;
}
-(void)setSizeHeight:(float)sizeHeight
{
CGRect frame = self.frame;
frame.size.height = sizeHeight;
self.frame = frame;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment