You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

271 lines
5.3 KiB

//
// Distribution
//
// Created by YJYH on 16/7/1.
// Copyright © 2016年 YJYH. All rights reserved.
//
#import "UIViewExt.h"
CGPoint CGRectGetCenter(CGRect rect)
{
CGPoint pt;
pt.x = CGRectGetMidX(rect);
pt.y = CGRectGetMidY(rect);
return pt;
}
CGRect CGRectMoveToCenter(CGRect rect, CGPoint center)
{
CGRect newrect = CGRectZero;
newrect.origin.x = center.x-CGRectGetMidX(rect);
newrect.origin.y = center.y-CGRectGetMidY(rect);
newrect.size = rect.size;
return newrect;
}
@implementation UIView (ViewGeometry)
// Retrieve and set the origin
- (CGPoint) origin
{
return self.frame.origin;
}
- (void) setOrigin: (CGPoint) aPoint
{
CGRect newframe = self.frame;
newframe.origin = aPoint;
self.frame = newframe;
}
// Retrieve and set the size
- (CGSize) size
{
return self.frame.size;
}
- (void) setSize: (CGSize) aSize
{
CGRect newframe = self.frame;
newframe.size = aSize;
self.frame = newframe;
}
// Query other frame locations
- (CGPoint) bottomRight
{
CGFloat x = self.frame.origin.x + self.frame.size.width;
CGFloat y = self.frame.origin.y + self.frame.size.height;
return CGPointMake(x, y);
}
- (CGPoint) bottomLeft
{
CGFloat x = self.frame.origin.x;
CGFloat y = self.frame.origin.y + self.frame.size.height;
return CGPointMake(x, y);
}
- (CGPoint) topRight
{
CGFloat x = self.frame.origin.x + self.frame.size.width;
CGFloat y = self.frame.origin.y;
return CGPointMake(x, y);
}
// Retrieve and set height, width, top, bottom, left, right
- (CGFloat) height
{
return self.frame.size.height;
}
- (void) setHeight: (CGFloat) newheight
{
CGRect newframe = self.frame;
newframe.size.height = newheight;
self.frame = newframe;
}
- (CGFloat) width
{
return self.frame.size.width;
}
- (void) setWidth: (CGFloat) newwidth
{
CGRect newframe = self.frame;
newframe.size.width = newwidth;
self.frame = newframe;
}
- (CGFloat) top
{
return self.frame.origin.y;
}
- (void) setTop: (CGFloat) newtop
{
CGRect newframe = self.frame;
newframe.origin.y = newtop;
self.frame = newframe;
}
- (CGFloat) left
{
return self.frame.origin.x;
}
- (void) setLeft: (CGFloat) newleft
{
CGRect newframe = self.frame;
newframe.origin.x = newleft;
self.frame = newframe;
}
- (CGFloat) bottom
{
return self.frame.origin.y + self.frame.size.height;
}
- (void) setBottom: (CGFloat) newbottom
{
CGRect newframe = self.frame;
newframe.origin.y = newbottom - self.frame.size.height;
self.frame = newframe;
}
- (CGFloat) right
{
return self.frame.origin.x + self.frame.size.width;
}
- (void) setRight: (CGFloat) newright
{
CGFloat delta = newright - (self.frame.origin.x + self.frame.size.width);
CGRect newframe = self.frame;
newframe.origin.x += delta ;
self.frame = newframe;
}
// Move via offset
- (void) moveBy: (CGPoint) delta
{
CGPoint newcenter = self.center;
newcenter.x += delta.x;
newcenter.y += delta.y;
self.center = newcenter;
}
// Scaling
- (void) scaleBy: (CGFloat) scaleFactor
{
CGRect newframe = self.frame;
newframe.size.width *= scaleFactor;
newframe.size.height *= scaleFactor;
self.frame = newframe;
}
// Ensure that both dimensions fit within the given size by scaling down
- (void) fitInSize: (CGSize) aSize
{
CGFloat scale;
CGRect newframe = self.frame;
if (newframe.size.height && (newframe.size.height > aSize.height))
{
scale = aSize.height / newframe.size.height;
newframe.size.width *= scale;
newframe.size.height *= scale;
}
if (newframe.size.width && (newframe.size.width >= aSize.width))
{
scale = aSize.width / newframe.size.width;
newframe.size.width *= scale;
newframe.size.height *= scale;
}
self.frame = newframe;
}
// 设置UIView的X
- (void)setViewX:(CGFloat)newX
{
CGRect viewFrame = [self frame];
viewFrame.origin.x = newX;
[self setFrame:viewFrame];
}
// 设置UIView的Y
- (void)setViewY:(CGFloat)newY
{
CGRect viewFrame = [self frame];
viewFrame.origin.y = newY;
[self setFrame:viewFrame];
}
- (CGFloat)centerX
{
return self.center.x;
}
- (void)setCenterX:(CGFloat)centerX
{
self.center = CGPointMake(centerX, self.center.y);
}
- (CGFloat)centerY
{
return self.center.y;
}
- (void)setCenterY:(CGFloat)centerY
{
self.center = CGPointMake(self.center.x,centerY);
}
// 设置UIView的Origin
- (void)setViewOrigin:(CGPoint)newOrigin
{
CGRect viewFrame = [self frame];
viewFrame.origin = newOrigin;
[self setFrame:viewFrame];
}
// 设置UIView的width
- (void)setViewWidth:(CGFloat)newWidth
{
CGRect viewFrame = [self frame];
viewFrame.size.width = newWidth;
[self setFrame:viewFrame];
}
// 设置UIView的height
- (void)setViewHeight:(CGFloat)newHeight
{
CGRect viewFrame = [self frame];
viewFrame.size.height = newHeight;
[self setFrame:viewFrame];
}
// 设置UIView的Size
- (void)setViewSize:(CGSize)newSize
{
CGRect viewFrame = [self frame];
viewFrame.size = newSize;
[self setFrame:viewFrame];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end