iOS开发-UIView扩展CGRect
关于UIView的位置都会遇到,一般需要改变UIView的位置,需要先获取原有的frame位置,然后在frame上面修改,有的时候如果只是改变了一下垂直方向的位置,宽度和高度的一种,这种写法很麻烦。下面两种写法第二种明显更简单,如果需要实现第二种方法就需要扩展UIView。
1 2 3 4 5 6 7 8 | //1 CGRect frame=self.testView.frame; frame.size.width=120; self.testView.frame=frame; [self printFrame]; //2 self.testView.width=120; [self printFrame]; |
扩展定义:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | @ 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 |
扩展实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | @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
本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/5119677.html,如需转载请自行联系原作者

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Android数据层架构的实现 下篇
接上篇:Android数据层架构的实现 上篇 4.外观模式实现数据处理引擎框架暴露出来的api 我们在使用各种开源框架的时候,大多数时候都不会对框架内部的实现进行细究,所以一个好的框架需要一个简单的入口来对整个框架进行使用,我接下来就是要讲解本框架的入口类。 public class DataEngine { public static String TAG="DataEngine"; public static Context nowContext; public static ObservableBoolean nowIsShowProgressBar; public static final List<Interceptor> mInterceptors=new ArrayList<>(); public static final DataEngine sInstance; static { synchronized (DataEngine.class){ sInstance=new DataEngine(); } } private final Serv...
- 下一篇
在安卓上运行TensorFlow:让深度学习进入移动端
如果你关注我的前一篇帖子,并按照其中的内容实践,你可能已经学会了如何在Linux上安装一个GPU加速的TensorFlow,并构建了你自己的图像分类器。老实讲,在笔记本上对图片进行分类是很花时间的:需要下载分类用的图片,并在终端里输入很多行命令来运行分类。 不过,尽管没有很多的公开资料,好消息是你也可以在有摄像头的手机上运行TensofrFlow的Inception分类器,甚至是你自定义的分类器。然后你只要把摄像头对准你希望做分类的东西,TensorFlow就会告诉你它认为这是什么东西。TensorFlow是可以在iOS和树莓派上面运行,不过在这个教程里,我会介绍如何在安卓设备上运行TensorFlow。 我会一步一步地介绍如何在安卓设备上运行定制化的图片分类器。实现这个功能需要很多步骤,而且其他地方没有这样的介绍,只能通过反复地查看
相关文章
文章评论
共有0条评论来说两句吧...