天天看點

iOS 開發 封裝(X-Y-W-H)工具類

iOS 開發 封裝(X-Y-W-H)工具類

在代碼中常常定義一個或者多個控件的高寬和xy坐标,例如:當控制單個控件的x時,自動布局略顯備援,而單個frame需要繁瑣的來回定義,在這裡封裝一個控制xy和高寬的工具類,能顯著的提高開發效率.

#import <UIKit/UIKit.h>
@interface UIView (Amals_Ex)
@property (nonatomic, assign) CGFloat x;
@property (nonatomic, assign) CGFloat y;
@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) CGFloat height;
@end
           
#import "UIView+Amals_Ex.h"

@implementation UIView (Amals_Ex)
- (void)setY:(CGFloat)y {   
    CGRect rect = self.frame;
    rect.origin.y = y;    
    self.frame = rect;    
}
- (CGFloat)y {
    return self.frame.origin.y;
}

- (void)setX:(CGFloat)x {   
    CGRect frame = self.frame;    
    frame.origin.x = x;   
    self.frame = frame;   
}
- (CGFloat)x {   
    return self.frame.origin.x;
}

- (void)setWidth:(CGFloat)width {    
    CGRect frame = self.frame;   
    frame.size.width = width; 
    self.frame = frame;  
}
- (CGFloat)width {
    return self.frame.size.width;
}

- (void)setHeight:(CGFloat)height { 
    CGRect frame = self.frame;  
    frame.size.height = height;  
    self.frame = frame;  
}
- (CGFloat)height {
    return self.frame.size.height;
}
@end