UI原点尺寸获取的简化方法
2014-05-23 11:22
267 查看
在开发中,经常会用到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);
我们可以扩展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);
相关文章推荐
- Javascript 获取浏览器窗口中文档(视口)可用尺寸的方法
- ios5 中文键盘高度变高覆盖现有ui问题的解决方案(获取键盘高度的方法)
- 苹果官方给出的代替[UIDevice uniqueIdentifier]获取UDID的方法
- Android使用什么的方法来获取屏幕尺寸
- 用JavaScript获取DOM元素位置和尺寸大小的方法
- Javascript 获取浏览器窗口中文档(视口)可用尺寸的方法
- 获取设备的屏幕尺寸与密度的方法
- 天天记录 - Android获取屏幕尺寸,分辨率3种方法
- UI中 加个timer 写个while true的方法 不断获取run的对象
- 获取屏幕尺寸与密度的方法,还是比较准确的
- ios5 中文键盘高度变高覆盖现有ui问题的解决方案(获取键盘高度的方法)
- ios5 中文键盘高度变高覆盖现有ui问题的解决方案(获取键盘高度的方法)
- ios5 中文键盘高度变高覆盖现有ui问题的解决方案(获取键盘高度的方法)
- 获取UIDevice的uniqueIdentifier替代方法
- .NET一个线程更新另一个线程的UI(两种实现方法及若干简化)
- android 获取屏幕尺寸方法
- ios5 中文键盘高度变高覆盖现有ui问题的解决方案(获取键盘高度的方法)
- ios5 中文键盘高度变高覆盖现有ui问题的解决方案(获取键盘高度的方法)
- ios5 中文键盘高度变高覆盖现有ui问题的解决方案(获取键盘高度的方法)
- ios5 中文键盘高度变高覆盖现有ui问题的解决方案(获取键盘高度的方法)