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
iOS开发-UIView扩展CGRect的更多相关文章
- iOS开发UIView.h简介
1.UICoordinateSpace不同坐标空间的坐标切换 @protocol UICoordinateSpace <NSObject> //将当前的坐标空间点转换到指定的坐标空间 - ...
- iOS 自定义方法 - UIView扩展
示例代码 //#import <UIKit/UIKit.h>@interface UIView (LPCView)/** 上 */@property CGFloat top;/** 下 * ...
- iOS开发之类扩展
在以往写代码时,我们经常是把声明写在.h文件中,把实现写在.m文件中,但是在实际开发中,如果把声明写在.h文件中会暴露程序很多属性(成员变量.成员变量的get和set方法),为了安全考虑,引入了类扩展 ...
- iOS开发系列--扩展--播放音乐库中的音乐
众所周知音乐是iOS的重要组成播放,无论是iPod.iTouch.iPhone还是iPad都可以在iTunes购买音乐或添加本地音乐到音乐 库中同步到你的iOS设备.在MediaPlayer.fram ...
- iOS开发--打印NSRange,CGRect等结构体
使用对应的转换NSStringFromCGPoint NSStringFromCGSize NSStringFromCGRect NSStringFromCGAffineTransform ...
- iOS开发——UI精选OC篇&UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍
UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍 一:UIApplication:单例(关于单例后面的文章中会详细介绍,你现在只要知道 ...
- iOS开发系列--App扩展开发
概述 从iOS 8 开始Apple引入了扩展(Extension)用于增强系统应用服务和应用之间的交互.它的出现让自定义键盘.系统分享集成等这些依靠系统服务的开发变成了可能.WWDC 2016上众多更 ...
- iOS开发UI篇—核心动画(UIView封装动画)
iOS开发UI篇—核心动画(UIView封装动画) 一.UIView动画(首尾) 1.简单说明 UIKit直接将动画集成到UIView类中,当内部的一些属性发生改变时,UIView将为这些改变提供动画 ...
- iOS开发笔记10:圆点缩放动画、强制更新、远程推送加语音提醒及UIView截屏
1.使用CAReplicatorLayer制作等待动画 CALayer+CABasicAnimation可以制作很多简单的动画效果,之前的博客中介绍的“两个动画”,一个是利用一张渐变色图片+CABas ...
随机推荐
- 两道SQL题目
1.查询省内所有城市气温都大于35度的省份(表名:Temp) SELECT province FROM Temp WHERE province NOT IN ( SELECT province FRO ...
- CNN中各种各样的卷积
https://zhuanlan.zhihu.com/p/29367273 https://zhuanlan.zhihu.com/p/28749411 以及1*1卷积核:https://www.zhi ...
- PS设计漂亮网站主页图片的实例教程
制作一个好的网页,需要花费大量的时间,包含的内容也是非常多的,其中有按钮.横幅.图标及其它素材等.制作的时候先规划好大致的框架,然后由上至下慢慢细化各部分的内容,注意好整体搭配.最终效果 一.在我们打 ...
- HDU 5988 Coding Contest(费用流+浮点数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5988 题目大意: 给定n个点,m条有向边,每个点是一个吃饭的地方,每个人一盒饭.每个点有S个人,有B盒 ...
- hdu1698
/*区间更新*/#include <cstdio> #include <algorithm> using namespace std; #define lson l , m , ...
- uva11610 树状数组+素数打表求因子,好题!
/* uva11610 树状数组+素数打表+离散化 打出的素数范围在2-1000000之间,不超过六位数,然后按照格式翻转成七位数 */ #include<bits/stdc++.h> u ...
- 更好用的cmd窗口
cmder是windows下的命令行工具,用来替代windows自带的cmd. 下载地址 下载后自建文件夹并解压,将Cmder.exe所在文件夹路径加入path, windows + r 键入cmde ...
- Chakra TypedArray代码实现笔记
ArrayBuffer.cpp阅读 对象继承关系 JavascriptArrayBuffer: ArrayBuffer: ArrayBufferBase: DynamicObject: Recycla ...
- hdu 1596 乘积的最大值
一般题是 和的最小值 这个是乘积的最大值 Sample Input31 0.5 0.50.5 1 0.40.5 0.4 131 2 //起点 终点2 31 3 Sample Output0.5000. ...
- 详解kubeadm生成的证书(转)
https://docs.lvrui.io/2018/09/28/%E8%AF%A6%E8%A7%A3kubeadm%E7%94%9F%E6%88%90%E7%9A%84%E8%AF%81%E4%B9 ...