Skip to content

Commit

Permalink
UIView+Positioning分类加前缀
Browse files Browse the repository at this point in the history
  • Loading branch information
yixiang committed Oct 22, 2018
1 parent f0190b3 commit f2e64f4
Show file tree
Hide file tree
Showing 47 changed files with 231 additions and 336 deletions.
4 changes: 2 additions & 2 deletions DoraemonKit/Src/Core/Base/DoraemonBaseSwitchViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ - (void)initUI{
self.title = @"";

UISwitch *switchView = [[UISwitch alloc] init];
switchView.origin = CGPointMake(DoraemonScreenWidth/2-switchView.width/2, DoraemonScreenHeight/2-switchView.height/2);
switchView.doraemon_origin = CGPointMake(DoraemonScreenWidth/2-switchView.doraemon_width/2, DoraemonScreenHeight/2-switchView.doraemon_height/2);
[self.view addSubview:switchView];
[switchView addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
switchView.on = [self switchViewOn];
Expand All @@ -35,7 +35,7 @@ - (void)initUI{
tipLabel.text = @"点我右侧😘: ";
[self.view addSubview:tipLabel];
[tipLabel sizeToFit];
tipLabel.origin = CGPointMake(switchView.left-10-tipLabel.width, DoraemonScreenHeight/2-tipLabel.height/2);
tipLabel.doraemon_origin = CGPointMake(switchView.doraemon_left-10-tipLabel.doraemon_width, DoraemonScreenHeight/2-tipLabel.doraemon_height/2);
}

- (BOOL)switchViewOn{
Expand Down
46 changes: 12 additions & 34 deletions DoraemonKit/Src/Core/Category/UIView+Positioning.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,62 +14,40 @@
@interface UIView (Positioning)

/** View's X Position */
@property (nonatomic, assign) CGFloat x;
@property (nonatomic, assign) CGFloat doraemon_x;

/** View's Y Position */
@property (nonatomic, assign) CGFloat y;
@property (nonatomic, assign) CGFloat doraemon_y;

/** View's width */
@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) CGFloat doraemon_width;

/** View's height */
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, assign) CGFloat doraemon_height;

/** View's origin - Sets X and Y Positions */
@property (nonatomic, assign) CGPoint origin;
@property (nonatomic, assign) CGPoint doraemon_origin;

/** View's size - Sets Width and Height */
@property (nonatomic, assign) CGSize size;
@property (nonatomic, assign) CGSize doraemon_size;

/** Y value representing the bottom of the view **/
@property (nonatomic, assign) CGFloat bottom;
@property (nonatomic, assign) CGFloat doraemon_bottom;

/** X Value representing the right side of the view **/
@property (nonatomic, assign) CGFloat right;
@property (nonatomic, assign) CGFloat doraemon_right;

/** X Value representing the top of the view (alias of x) **/
@property (nonatomic, assign) CGFloat left;
@property (nonatomic, assign) CGFloat doraemon_left;

/** Y Value representing the top of the view (alias of y) **/
@property (nonatomic, assign) CGFloat top;
@property (nonatomic, assign) CGFloat doraemon_top;

/** X value of the object's center **/
@property (nonatomic, assign) CGFloat centerX;
@property (nonatomic, assign) CGFloat doraemon_centerX;

/** Y value of the object's center **/
@property (nonatomic, assign) CGFloat centerY;
@property (nonatomic, assign) CGFloat doraemon_centerY;

/** Returns the Subview with the heighest X value **/
@property (nonatomic, strong, readonly) UIView *lastSubviewOnX;

/** Returns the Subview with the heighest Y value **/
@property (nonatomic, strong, readonly) UIView *lastSubviewOnY;

/** View's bounds X value **/
@property (nonatomic, assign) CGFloat boundsX;

/** View's bounds Y value **/
@property (nonatomic, assign) CGFloat boundsY;

/** View's bounds width **/
@property (nonatomic, assign) CGFloat boundsWidth;

/** View's bounds height **/
@property (nonatomic, assign) CGFloat boundsHeight;

/**
Centers the view to its parent view (if exists)
*/
-(void) centerToParent;

@end
165 changes: 41 additions & 124 deletions DoraemonKit/Src/Core/Category/UIView+Positioning.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,189 +11,106 @@
#define PIXEL_INTEGRAL(pointValue) (round(pointValue * SCREEN_SCALE) / SCREEN_SCALE)

@implementation UIView (Positioning)
@dynamic x, y, width, height, origin, size;
@dynamic boundsWidth, boundsHeight, boundsX, boundsY;
@dynamic doraemon_x, doraemon_y, doraemon_width, doraemon_height, doraemon_origin, doraemon_size;

// Setters
-(void)setX:(CGFloat)x{
self.frame = CGRectMake(PIXEL_INTEGRAL(x), self.y, self.width, self.height);
-(void)setDoraemon_x:(CGFloat)x{
self.frame = CGRectMake(PIXEL_INTEGRAL(x), self.doraemon_y, self.doraemon_width, self.doraemon_height);
}

-(void)setY:(CGFloat)y{
self.frame = CGRectMake(self.x, PIXEL_INTEGRAL(y), self.width, self.height);
-(void)setDoraemon_y:(CGFloat)y{
self.frame = CGRectMake(self.doraemon_x, PIXEL_INTEGRAL(y), self.doraemon_width, self.doraemon_height);
}

-(void)setWidth:(CGFloat)width{
self.frame = CGRectMake(self.x, self.y, PIXEL_INTEGRAL(width), self.height);
-(void)setDoraemon_width:(CGFloat)width{
self.frame = CGRectMake(self.doraemon_x, self.doraemon_y, PIXEL_INTEGRAL(width), self.doraemon_height);
}

-(void)setHeight:(CGFloat)height{
self.frame = CGRectMake(self.x, self.y, self.width, PIXEL_INTEGRAL(height));
-(void)setDoraemon_height:(CGFloat)height{
self.frame = CGRectMake(self.doraemon_x, self.doraemon_y, self.doraemon_width, PIXEL_INTEGRAL(height));
}

-(void)setOrigin:(CGPoint)origin{
self.x = origin.x;
self.y = origin.y;
-(void)setDoraemon_origin:(CGPoint)origin{
self.doraemon_x = origin.x;
self.doraemon_y = origin.y;
}

-(void)setSize:(CGSize)size{
self.width = size.width;
self.height = size.height;
-(void)setDoraemon_size:(CGSize)size{
self.doraemon_width = size.width;
self.doraemon_height = size.height;
}

-(void)setRight:(CGFloat)right {
self.x = right - self.width;
-(void)setDoraemon_right:(CGFloat)right {
self.doraemon_x = right - self.doraemon_width;
}

-(void)setBottom:(CGFloat)bottom {
self.y = bottom - self.height;
-(void)setDoraemon_bottom:(CGFloat)bottom {
self.doraemon_y = bottom - self.doraemon_height;
}

-(void)setLeft:(CGFloat)left{
self.x = left;
-(void)setDoraemon_left:(CGFloat)left{
self.doraemon_x = left;
}

-(void)setTop:(CGFloat)top{
self.y = top;
-(void)setDoraemon_top:(CGFloat)top{
self.doraemon_y = top;
}

-(void)setCenterX:(CGFloat)centerX {
-(void)setDoraemon_centerX:(CGFloat)centerX {
self.center = CGPointMake(PIXEL_INTEGRAL(centerX), self.center.y);
}

-(void)setCenterY:(CGFloat)centerY {
-(void)setDoraemon_centerY:(CGFloat)centerY {
self.center = CGPointMake(self.center.x, PIXEL_INTEGRAL(centerY));
}

-(void)setBoundsX:(CGFloat)boundsX{
self.bounds = CGRectMake(PIXEL_INTEGRAL(boundsX), self.boundsY, self.boundsWidth, self.boundsHeight);
}

-(void)setBoundsY:(CGFloat)boundsY{
self.bounds = CGRectMake(self.boundsX, PIXEL_INTEGRAL(boundsY), self.boundsWidth, self.boundsHeight);
}

-(void)setBoundsWidth:(CGFloat)boundsWidth{
self.bounds = CGRectMake(self.boundsX, self.boundsY, PIXEL_INTEGRAL(boundsWidth), self.boundsHeight);
}

-(void)setBoundsHeight:(CGFloat)boundsHeight{
self.bounds = CGRectMake(self.boundsX, self.boundsY, self.boundsWidth, PIXEL_INTEGRAL(boundsHeight));
}

// Getters
-(CGFloat)x{
-(CGFloat)doraemon_x{
return self.frame.origin.x;
}

-(CGFloat)y{
-(CGFloat)doraemon_y{
return self.frame.origin.y;
}

-(CGFloat)width{
-(CGFloat)doraemon_width{
return self.frame.size.width;
}

-(CGFloat)height{
-(CGFloat)doraemon_height{
return self.frame.size.height;
}

-(CGPoint)origin{
return CGPointMake(self.x, self.y);
-(CGPoint)doraemon_origin{
return CGPointMake(self.doraemon_x, self.doraemon_y);
}

-(CGSize)size{
return CGSizeMake(self.width, self.height);
-(CGSize)doraemon_size{
return CGSizeMake(self.doraemon_width, self.doraemon_height);
}

-(CGFloat)right {
-(CGFloat)doraemon_right {
return self.frame.origin.x + self.frame.size.width;
}

-(CGFloat)bottom {
-(CGFloat)doraemon_bottom {
return self.frame.origin.y + self.frame.size.height;
}

-(CGFloat)left{
return self.x;
-(CGFloat)doraemon_left{
return self.doraemon_x;
}

-(CGFloat)top{
return self.y;
-(CGFloat)doraemon_top{
return self.doraemon_y;
}

-(CGFloat)centerX {
-(CGFloat)doraemon_centerX {
return self.center.x;
}

-(CGFloat)centerY {
-(CGFloat)doraemon_centerY {
return self.center.y;
}

-(UIView *)lastSubviewOnX{
if(self.subviews.count > 0){
UIView *outView = self.subviews[0];

for(UIView *v in self.subviews)
if(v.x > outView.x)
outView = v;

return outView;
}

return nil;
}

-(UIView *)lastSubviewOnY{
if(self.subviews.count > 0){
UIView *outView = self.subviews[0];

for(UIView *v in self.subviews)
if(v.y > outView.y)
outView = v;

return outView;
}

return nil;
}

-(CGFloat)boundsX{
return self.bounds.origin.x;
}

-(CGFloat)boundsY{
return self.bounds.origin.y;
}

-(CGFloat)boundsWidth{
return self.bounds.size.width;
}

-(CGFloat)boundsHeight{
return self.bounds.size.height;
}

// Methods
-(void)centerToParent{
if(self.superview){
switch ([UIApplication sharedApplication].statusBarOrientation){
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationLandscapeRight:{
self.origin = CGPointMake((self.superview.height / 2.0) - (self.width / 2.0),
(self.superview.width / 2.0) - (self.height / 2.0));
break;
}
case UIInterfaceOrientationPortrait:
case UIInterfaceOrientationPortraitUpsideDown:{
self.origin = CGPointMake((self.superview.width / 2.0) - (self.width / 2.0),
(self.superview.height / 2.0) - (self.height / 2.0));
break;
}
case UIInterfaceOrientationUnknown:
return;
}
}
}

@end
6 changes: 3 additions & 3 deletions DoraemonKit/Src/Core/CommonUI/CellBtn/DoraemonCellButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ - (instancetype)initWithFrame:(CGRect)frame{
- (void)renderUIWithTitle:(NSString *)title{
_titleLabel.text = title;
[_titleLabel sizeToFit];
_titleLabel.frame = CGRectMake(20, self.height/2-_titleLabel.height/2, _titleLabel.width, _titleLabel.height);
_titleLabel.frame = CGRectMake(20, self.doraemon_height/2-_titleLabel.doraemon_height/2, _titleLabel.doraemon_width, _titleLabel.doraemon_height);
}

- (void)needTopLine{
_topLine.hidden = NO;
_topLine.frame = CGRectMake(0, 0, self.width, 0.5);
_topLine.frame = CGRectMake(0, 0, self.doraemon_width, 0.5);
}

- (void)needDownLine{
_downLine.hidden = NO;
_downLine.frame = CGRectMake(0, self.height-0.5, self.width, 0.5);
_downLine.frame = CGRectMake(0, self.doraemon_height-0.5, self.doraemon_width, 0.5);
}

- (void)tap{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ - (instancetype)initWithFrame:(CGRect)frame{
_pointList = [NSMutableArray array];
_pointLayerList = [NSMutableArray array];

_bottomLine = [[UIView alloc] initWithFrame:CGRectMake(kStartX, self.height-1, self.width, 1)];
_bottomLine = [[UIView alloc] initWithFrame:CGRectMake(kStartX, self.doraemon_height-1, self.doraemon_width, 1)];
_bottomLine.backgroundColor = [UIColor whiteColor];
[self addSubview:_bottomLine];

_lowValueLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.height-10, kStartX, 10)];
_lowValueLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.doraemon_height-10, kStartX, 10)];
_lowValueLabel.text = @"0";
_lowValueLabel.textColor = [UIColor whiteColor];
//_lowValueLabel.textAlignment = NSTextAlignmentRight;
Expand Down Expand Up @@ -76,8 +76,8 @@ - (void)setHightValue:(NSString *)value{
}

- (void)addHeightValue:(CGFloat)showHeight andTipValue:(NSString *)tipValue{
CGFloat width = self.width;
CGFloat height = self.height;
CGFloat width = self.doraemon_width;
CGFloat height = self.doraemon_height;
CGFloat step = width / _numberOfPoints;
if (_pointList.count == 0) {
_x = kStartX;
Expand Down Expand Up @@ -126,13 +126,13 @@ - (void)drawLine{
UIBezierPath *path = [UIBezierPath bezierPath];

DoraemonPoint *point = self.pointList[0];
CGPoint p1 = CGPointMake(point.x, self.height - point.y);
CGPoint p1 = CGPointMake(point.x, self.doraemon_height - point.y);
[path moveToPoint:p1];
[self addPointLayer:p1];

for (int i=1; i<self.pointList.count; i++) {
point = self.pointList[i];
CGPoint p2 = CGPointMake(point.x, self.height - point.y);
CGPoint p2 = CGPointMake(point.x, self.doraemon_height - point.y);
[path addLineToPoint:p2];

[self addPointLayer:p2];
Expand Down Expand Up @@ -169,7 +169,7 @@ - (void)drawTipViewWithValue:(NSString *)tip{
DoraemonPoint *point = self.pointList[self.pointList.count-1];
_tipLabel.text = tip;
[_tipLabel sizeToFit];
_tipLabel.frame = CGRectMake(point.x, (self.height - point.y)-2-1-_tipLabel.height, _tipLabel.width, _tipLabel.height);
_tipLabel.frame = CGRectMake(point.x, (self.doraemon_height - point.y)-2-1-_tipLabel.doraemon_height, _tipLabel.doraemon_width, _tipLabel.doraemon_height);
}

- (void)clear{
Expand Down
Loading

0 comments on commit f2e64f4

Please sign in to comment.