当前位置: 首页 > news >正文

iOS开发-UIView扩展CGRect

关于UIView的位置都会遇到,一般需要改变UIView的位置,需要先获取原有的frame位置,然后在frame上面修改,有的时候如果只是改变了一下垂直方向的位置,宽度和高度的一种,这种写法很麻烦。下面两种写法第二种明显更简单,如果需要实现第二种方法就需要扩展UIView。

    //1
    CGRect frame=self.testView.frame;
    frame.size.width=120;
    self.testView.frame=frame;
    [self printFrame];
    //2
    self.testView.width=120;
    [self printFrame];

扩展定义:

@interface UIView (ReSize)

@property (nonatomic, assign) CGSize size;

@property (nonatomic,assign)  CGFloat x;

@property  (nonatomic,assign) CGFloat y;

@property (nonatomic, assign) CGFloat top;

@property (nonatomic, assign) CGFloat bottom;

@property (nonatomic, assign) CGFloat left;

@property (nonatomic, assign) CGFloat right;

@property (nonatomic, assign) CGFloat centerX;

@property (nonatomic, assign) CGFloat centerY;

@property (nonatomic, assign) CGFloat width;

@property (nonatomic, assign) CGFloat height;

@end

 扩展实现:

@implementation UIView (ReSize)

- (CGSize)size;
{
    return [self frame].size;
}

- (void)setSize:(CGSize)size;
{
    CGPoint origin = [self frame].origin;
    [self setFrame:CGRectMake(origin.x, origin.y, size.width, size.height)];
}

-(CGFloat)x{
    CGRect frame=[self frame];
    return frame.origin.x;
}

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

-(CGFloat)y{
    CGRect frame=[self frame];
    return frame.origin.y;
}

-(void)setY:(CGFloat)y{
    CGRect frame=[self frame];
    frame.origin.y=y;
    return [self setFrame:frame];
}

- (CGFloat)left;
{
    return CGRectGetMinX([self frame]);
}

- (void)setLeft:(CGFloat)x;
{
    CGRect frame = [self frame];
    frame.origin.x = x;
    [self setFrame:frame];
}

- (CGFloat)top;
{
    return CGRectGetMinY([self frame]);
}

- (void)setTop:(CGFloat)y;
{
    CGRect frame = [self frame];
    frame.origin.y = y;
    [self setFrame:frame];
}

- (CGFloat)right;
{
    return CGRectGetMaxX([self frame]);
}

- (void)setRight:(CGFloat)right;
{
    CGRect frame = [self frame];
    frame.origin.x = right - frame.size.width;
    
    [self setFrame:frame];
}

- (CGFloat)bottom;
{
    return CGRectGetMaxY([self frame]);
}

- (void)setBottom:(CGFloat)bottom;
{
    CGRect frame = [self frame];
    frame.origin.y = bottom - frame.size.height;
    [self setFrame:frame];
}

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

- (void)setCenterX:(CGFloat)centerX;
{
    [self setCenter:CGPointMake(centerX, self.center.y)];
}

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

- (void)setCenterY:(CGFloat)centerY;
{
    [self setCenter:CGPointMake(self.center.x, centerY)];
}

- (CGFloat)width;
{
    return CGRectGetWidth([self frame]);
}

- (void)setWidth:(CGFloat)width;
{
    CGRect frame = [self frame];
    frame.size.width = width;
    [self setFrame:CGRectStandardize(frame)];
}

- (CGFloat)height;
{
    return CGRectGetHeight([self frame]);
}

- (void)setHeight:(CGFloat)height;
{
    CGRect frame=[self frame];
    frame.size.height = height;
    [self setFrame:CGRectStandardize(frame)];
}

@end

项目源代码地址:https://github.com/SmallElephant/iOS-FEViewReSize

转载于:https://www.cnblogs.com/xiaofeixiang/p/5119677.html

相关文章:

  • 如何使用 SPICE client (virt-viewer) 来连接远程虚拟机桌面?
  • openssh 加固
  • Spring MVC 拦截器 interceptors
  • Javascript继承机制的实现
  • AndroidTestCase简单使用
  • Linux开源文本编辑器培训教材(二)
  • 微信公众平台开发(111) 现金红包、裂变红包、企业付款
  • ListView和SimPleteAdapter 把新闻数据绑定到ListView
  • I.MX6 Linux udev porting
  • Nginx搭建反向代理服务器过程详解(转)
  • 【PHP】PHP7的异常处理详解
  • IOS开发知识(六)
  • linux基础网络设置
  • 深入浅出Node.js (6) - 理解Buffer
  • Javascript 正确用法 二
  • canvas绘制圆角头像
  • Cumulo 的 ClojureScript 模块已经成型
  • HashMap ConcurrentHashMap
  • JSDuck 与 AngularJS 融合技巧
  • Linux中的硬链接与软链接
  • Lucene解析 - 基本概念
  • node学习系列之简单文件上传
  • spring cloud gateway 源码解析(4)跨域问题处理
  • 编写符合Python风格的对象
  • 从setTimeout-setInterval看JS线程
  • 讲清楚之javascript作用域
  • 你不可错过的前端面试题(一)
  • 如何设计一个微型分布式架构?
  • 如何胜任知名企业的商业数据分析师?
  • 使用API自动生成工具优化前端工作流
  • 推荐一个React的管理后台框架
  • 微信小程序实战练习(仿五洲到家微信版)
  • 用Python写一份独特的元宵节祝福
  • const的用法,特别是用在函数前面与后面的区别
  • ​【原创】基于SSM的酒店预约管理系统(酒店管理系统毕业设计)
  • ​TypeScript都不会用,也敢说会前端?
  • ​猴子吃桃问题:每天都吃了前一天剩下的一半多一个。
  • ​七周四次课(5月9日)iptables filter表案例、iptables nat表应用
  • #define用法
  • #NOIP 2014# day.1 T3 飞扬的小鸟 bird
  • #我与Java虚拟机的故事#连载17:我的Java技术水平有了一个本质的提升
  • (17)Hive ——MR任务的map与reduce个数由什么决定?
  • (2015)JS ES6 必知的十个 特性
  • (2022版)一套教程搞定k8s安装到实战 | RBAC
  • (9)STL算法之逆转旋转
  • (echarts)echarts使用时重新加载数据之前的数据存留在图上的问题
  • (分类)KNN算法- 参数调优
  • (附源码)spring boot球鞋文化交流论坛 毕业设计 141436
  • (附源码)ssm基于微信小程序的疫苗管理系统 毕业设计 092354
  • (附源码)基于ssm的模具配件账单管理系统 毕业设计 081848
  • (经验分享)作为一名普通本科计算机专业学生,我大学四年到底走了多少弯路
  • (考研湖科大教书匠计算机网络)第一章概述-第五节1:计算机网络体系结构之分层思想和举例
  • (理论篇)httpmoudle和httphandler一览
  • (一)appium-desktop定位元素原理
  • (一)使用Mybatis实现在student数据库中插入一个学生信息