UI原点尺寸获取的简化方法

在开发中,经常会用到UI的坐标原点,或尺寸大小,通常使用的方法是,获取x坐标:self.frame.origin.x;获取y坐标:self.frame.origin.y;获取宽:self.frame.size.width;获取高:self.frame.size.height;获取宽:self.bounds.size.width;获取高:self.bounds.size.height,但有时候用多了就会觉得烦,其实我们可以简化这些信息的获取方法。

我们可以扩展UIView类,并定义新的属性,以简化其坐标原点,尺寸的获取方法。详细步骤如下:

步骤1 创建UIView类的扩展类

Xcode——菜单栏——File——New——File——iOS——Cocoa Touch——Objective-C category——Category为扩展名称(如UIFrameMethod),Category on为被扩展的某个类(如UIView)(创建后的文件名,即为UIView+UIFrameMethod)。


步骤2 .h文件中定义相关属性

/****************************************************************/


///设置视图宽度属性,相对于父视图

@property (nonatomic, assign) CGFloat frameWidth;


///设置视图高度属性,相对于父视图

@property (nonatomic, assign) CGFloat frameHeight;


///设置视图宽度属性,相对于自身

@property (nonatomic, assign) CGFloat boundsWidth;


///设置视图高度属性,相对于自身

@property (nonatomic, assign) CGFloat boundsHeight;


///设置视图x坐标属性

@property (nonatomic, assign) CGFloat frameOriginX;


///设置视图y坐标属性

@property (nonatomic, assign) CGFloat frameOriginY;


/****************************************************************/


步骤3 .m实现文件中重写setter,getter方法

/****************************************************************/


- (CGFloat)frameWidth

{

    return self.frame.size.width;

}


- (void)setFrameWidth:(CGFloat)newwidth

{

    CGRect newRect = self.frame;

    newRect.size.width = newwidth;

    self.frame = newRect;

}


- (CGFloat)frameHeight

{

    return self.frame.size.height;

}


- (void)setFrameHeight:(CGFloat)newheight

{

    CGRect newRect = self.frame;

    newRect.size.width = newheight;

    self.frame = newRect;

}


- (CGFloat)boundsWidth

{

    return self.bounds.size.width;

}


- (void)setBoundsWidth:(CGFloat)newboundswidth

{

    CGRect newBounds = self.bounds;

    newBounds.size.width = newboundswidth;

    self.frame = newBounds;

}


- (CGFloat)boundsHeight

{

    return self.bounds.size.height;

}


- (void)setBoundsHeight:(CGFloat)newboundsheight

{

    CGRect newBounds = self.bounds;

    newBounds.size.height = newboundsheight;

    self.frame = newBounds;

}


- (CGFloat)frameOriginX

{

    return self.frame.origin.x;

}


- (void)setFrameOriginX:(CGFloat)newOriginX

{

    CGRect newRect = self.frame;

    newRect.origin.x = newOriginX;

    self.frame = newRect;

}


- (CGFloat)frameOriginY

{

    return self.frame.origin.y;

}


- (void)setFrameOriginY:(CGFloat)newOriginY

{

    CGRect newRect = self.frame;

    newRect.origin.y = newOriginY;

    self.frame = newRect;

}


/****************************************************************/


步骤4 在使用时先导入该扩展类的头文件(UIView+UIFrameMethod

引入位置,一般设置在“DemoUIInit-Prefix.pch”文件中,即

#ifdef __OBJC__

    #import <UIKit/UIKit.h>

    #import <Foundation/Foundation.h>

    #import "UIView+UIFrameMethod.h"  // 扩展类

#endif


步骤5 使用时,即获取相关属性时代码如下

NSLog(@"currentview %@, x = %f y = %f w = %f h = %f", currentview, currentview.frameOriginX, currentview.frameOriginY, currentview.frameWidth, currentview.frameHeight);



转载于:https://my.oschina.net/potato512/blog/647847

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值