Skip to content

Instantly share code, notes, and snippets.

@Air-Craft
Last active December 14, 2015 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Air-Craft/5153321 to your computer and use it in GitHub Desktop.
Save Air-Craft/5153321 to your computer and use it in GitHub Desktop.
A UIView category of basic operations you kinda expect to be there but aren't. Contributions welcome!
#import <UIKit/UIKit.h>
@interface UIView (Marshmallows)
@property (nonatomic) CGFloat rotation;
/** @name Positioning convenience methods */
- (void)moveByDeltaX:(CGFloat)delX deltaY:(CGFloat)delY;
- (void)moveOriginToX:(CGFloat)theX;
- (void)moveOriginToY:(CGFloat)theY;
- (void)moveOriginToX:(CGFloat)theX y:(CGFloat)theY;
- (void)moveOriginToX:(CGFloat)theX y:(CGFloat)theY;
/// *}
/** @name Resizing convenience methods */
- (void)resizeByWidth:(CGFloat)deltaW height:(CGFloat)deltaH;
- (void)resizeToWidth:(CGFloat)aWidth height:(CGFloat)aHeight;
- (void)resizeToWidth:(CGFloat)aWidth;
- (void)resizeToHeight:(CGFloat)aHeight;
- (void)resizeToSize:(CGSize)aSize;
/// *}
/// Returns the UIImage render of the current view
- (UIImage *)imageSnapshot;
/// Returns a UIImageView rendered version of the current view with the same frame
- (UIImageView *)imageViewSnapshot;
@end
/**
\addtogroup Marshmallows
\author Created by Hari Karam Singh on 06/08/2012.
\copyright Copyright (c) 2012 Club 15CC. All rights reserved.
@{
*/
#import "UIView+Marshmallows.h"
#import <objc/runtime.h>
#import <QuartzCore/QuartzCore.h>
/// Key for objc_*AssociatedObject functions
static char _rotation;
/////////////////////////////////////////////////////////////////////////
#pragma mark - UIView (Marshmallows)
/////////////////////////////////////////////////////////////////////////
@implementation UIView (Marshmallows)
- (CGFloat)rotation
{
NSNumber *n = objc_getAssociatedObject(self, &_rotation);
return n.floatValue;
}
- (void)setRotation:(CGFloat)rotation
{
// Get the delta required to align the view to the new rotation
CGFloat delta = rotation - _rotation;
// Do the rotation and update the property
self.transform = CGAffineTransformMakeRotation(delta);
// Must convert to an object for this trick to work
objc_setAssociatedObject(self, &_rotation, @(rotation), OBJC_ASSOCIATION_COPY);
}
/////////////////////////////////////////////////////////////////////////
- (void)moveByDeltaX:(CGFloat)delX deltaY:(CGFloat)delY
{
self.frame = CGRectMake(
self.frame.origin.x +delX,
self.frame.origin.y + delY,
self.frame.size.width,
self.frame.size.height
);
}
////////////////////////////////////////////////////////////////////////////
- (void)moveOriginToX:(CGFloat)theX
{
self.frame = CGRectMake(
theX,
self.frame.origin.y,
self.frame.size.width,
self.frame.size.height
);
}
/////////////////////////////////////////////////////////////////////////
- (void)moveOriginToY:(CGFloat)theY
{
self.frame = CGRectMake(
self.frame.origin.x,
theY,
self.frame.size.width,
self.frame.size.height
);
}
/////////////////////////////////////////////////////////////////////////
- (void)moveOriginToX:(CGFloat)theX y:(CGFloat)theY
{
self.frame = CGRectMake(
theX,
theY,
self.frame.size.width,
self.frame.size.height
);
}
/////////////////////////////////////////////////////////////////////////
- (void)moveOriginTo:(CGPoint)aPoint
{
CGRect f = self.frame;
f.origin = aPoint;
self.frame = f;
}
/////////////////////////////////////////////////////////////////////////
#pragma mark - Resizing Methods
/////////////////////////////////////////////////////////////////////////
- (void)resizeByWidth:(CGFloat)deltaW height:(CGFloat)deltaH
{
self.frame = CGRectMake(self.frame.origin.x,
self.frame.origin.y,
self.frame.size.width + deltaW,
self.frame.size.height + deltaH);
}
/////////////////////////////////////////////////////////////////////////
- (void)resizeToWidth:(CGFloat)aWidth height:(CGFloat)aHeight
{
self.frame = CGRectMake(self.frame.origin.x,
self.frame.origin.y,
aWidth,
aHeight);
}
/////////////////////////////////////////////////////////////////////////
- (void)resizeToWidth:(CGFloat)aWidth
{
[self resizeToWidth:aWidth height:self.frame.size.height];
}
/////////////////////////////////////////////////////////////////////////
- (void)resizeToHeight:(CGFloat)aHeight
{
[self resizeToWidth:self.frame.size.width height:aHeight];
}
/////////////////////////////////////////////////////////////////////////
- (void)resizeTo:(CGSize)aSize
{
CGRect f = self.frame;
f.size = aSize;
self.frame = f;
}
/////////////////////////////////////////////////////////////////////////
- (UIImage *)imageSnapshot
{
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;
}
/////////////////////////////////////////////////////////////////////////
- (UIImageView *)imageViewSnapshot
{
UIImageView *imgView = [[UIImageView alloc] initWithFrame:self.frame];
imgView.image = self.imageSnapshot;
return imgView;
}
/*- (void)dumpViewHierarchy
{
[self _dumpViewHierarchyForViewSelfWithIndent:@""];
}
- (void)_dumpViewHierarchyForView:(UIView *)aView withIndent:(NSString *)indent
{
NSLog(@"%@%@", indent, self); // dump this view
if (aView.subviews.count > 0) {
NSString* subIndent = [[NSString alloc] initWithFormat:@"%@%@",
indent, ([indent length]/2)%2==0 ? @"| " : @": "];
for (UIView* aSubview in aView.subviews) dumpView( aSubview, subIndent );
[subIndent release];
}
}*/
@end
/// @}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment