Skip to content

Instantly share code, notes, and snippets.

@usagimaru
Last active June 13, 2016 04:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save usagimaru/31ae16e943995a8d3abd to your computer and use it in GitHub Desktop.
Save usagimaru/31ae16e943995a8d3abd to your computer and use it in GitHub Desktop.
A pretty useful Objective-C category of UIView that accessing easily to UIView's frame.
@interface UIView (UIViewFrame)
// frame.origin を取得
- (CGPoint)origin;
- (CGFloat)x;
- (CGFloat)y;
// frame.size を取得
- (CGSize)size;
- (CGFloat)width;
- (CGFloat)height;
// frame.origin を設定
- (void)setOrigin:(CGPoint)p;
- (void)setX:(CGFloat)x;
- (void)setY:(CGFloat)y;
// frame.size を設定
- (void)setSize:(CGSize)s;
- (void)setWidth:(CGFloat)w;
- (void)setHeight:(CGFloat)h;
// frame.origin を相対的に設定
- (void)setOriginRelative:(CGPoint)p;
- (void)setXRelative:(CGFloat)x;
- (void)setYRelative:(CGFloat)y;
// frame.size を相対的に設定
- (void)setSizeRelative:(CGSize)s;
- (void)setWidthRelative:(CGFloat)w;
- (void)setHeightRelative:(CGFloat)h;
@end
@interface UIView (UIViewFrame)
// frame.origin を取得
- (CGPoint)origin;
- (CGFloat)x;
- (CGFloat)y;
// frame.size を取得
- (CGSize)size;
- (CGFloat)width;
- (CGFloat)height;
// frame.origin を設定
- (void)setOrigin:(CGPoint)p;
- (void)setX:(CGFloat)x;
- (void)setY:(CGFloat)y;
// frame.size を設定
- (void)setSize:(CGSize)s;
- (void)setWidth:(CGFloat)w;
- (void)setHeight:(CGFloat)h;
// frame.origin を相対的に設定
- (void)setOriginRelative:(CGPoint)p;
- (void)setXRelative:(CGFloat)x;
- (void)setYRelative:(CGFloat)y;
// frame.size を相対的に設定
- (void)setSizeRelative:(CGSize)s;
- (void)setWidthRelative:(CGFloat)w;
- (void)setHeightRelative:(CGFloat)h;
@end
@implementation UIView (ViewFrame)
- (CGPoint)origin
{
return self.frame.origin;
}
- (CGFloat)x
{
return self.origin.x;
}
- (CGFloat)y
{
return self.origin.y;
}
- (CGSize)size
{
return self.frame.size;
}
- (CGFloat)width
{
return self.size.width;
}
- (CGFloat)height
{
return self.size.height;
}
// frame.origin を設定
- (void)setOrigin:(CGPoint)p
{
CGRect r = self.frame;
r.origin = p;
self.frame = r;
}
- (void)setX:(CGFloat)x
{
CGRect r = self.frame;
r.origin.x = x;
self.frame = r;
}
- (void)setY:(CGFloat)y
{
CGRect r = self.frame;
r.origin.y = y;
self.frame = r;
}
// frame.size を設定
- (void)setSize:(CGSize)s
{
CGRect r = self.frame;
r.size = s;
self.frame = r;
}
- (void)setWidth:(CGFloat)w
{
CGRect r = self.frame;
r.size.width = w;
self.frame = r;
}
- (void)setHeight:(CGFloat)h
{
CGRect r = self.frame;
r.size.height = h;
self.frame = r;
}
// frame.origin を相対的に設定
- (void)setOriginRelative:(CGPoint)p
{
CGRect r = self.frame;
CGPoint point = r.origin;
point.x += p.x;
point.y += p.y;
r.origin = point;
self.frame = r;
}
- (void)setXRelative:(CGFloat)x
{
CGRect r = self.frame;
CGPoint point = r.origin;
point.x += x;
r.origin = point;
self.frame = r;
}
- (void)setYRelative:(CGFloat)y
{
CGRect r = self.frame;
CGPoint point = r.origin;
point.y += y;
r.origin = point;
self.frame = r;
}
// frame.size を相対的に設定
- (void)setSizeRelative:(CGSize)s
{
CGRect r = self.frame;
r.size.width += s.width;
r.size.height += s.height;
self.frame = r;
}
- (void)setWidthRelative:(CGFloat)w
{
CGRect r = self.frame;
r.size.width += w;
self.frame = r;
}
- (void)setHeightRelative:(CGFloat)h
{
CGRect r = self.frame;
r.size.height += h;
self.frame = r;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment