我们在project在,改变或多或少控件的坐标-宽度-高度,然后,经常看到你的self.view.frame.origin.x,self.view.frame.size.width.........相当的麻烦,在这里向大家推荐一个比較好的工具类,是UIView的类目,它里面对于求坐标,求高度什么的做了封装,非常方便大家调用.

@下载链接:点击这里

@.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> @interface UIView (TTCategory) @property(nonatomic) CGFloat left;
@property(nonatomic) CGFloat top;
@property(nonatomic) CGFloat right;
@property(nonatomic) CGFloat bottom; @property(nonatomic) CGFloat width;
@property(nonatomic) CGFloat height; @property(nonatomic) CGFloat centerX;
@property(nonatomic) CGFloat centerY; @property(nonatomic,readonly) CGFloat screenX;
@property(nonatomic,readonly) CGFloat screenY;
@property(nonatomic,readonly) CGFloat screenViewX;
@property(nonatomic,readonly) CGFloat screenViewY;
@property(nonatomic,readonly) CGRect screenFrame; @property(nonatomic) CGPoint origin;
@property(nonatomic) CGSize size; @property(nonatomic) BOOL visible; /**
* Finds the first descendant view (including this view) that is a member of a particular class.
*/
- (UIView*)descendantOrSelfWithClass:(Class)cls; /**
* Finds the first ancestor view (including this view) that is a member of a particular class.
*/
- (UIView*)ancestorOrSelfWithClass:(Class)cls; /**
* Removes all subviews.
*/
- (void)removeAllSubviews; /**
* Calculates the offset of this view from another view in screen coordinates.
*/
- (CGPoint)offsetFromView:(UIView*)otherView; /**
* The view controller whose view contains this view.
*/
- (UIViewController*)viewController; - (void)addSubviews:(NSArray *)views; @end

@.m

#import "UIViewAdditions.h"

@implementation UIView (TTCategory)

/******************************view是否可见*******************************/
- (BOOL)visible{
return !self.hidden;
} - (void)setVisible:(BOOL)visible{
self.hidden = !visible;
} /******************************设置View左边x坐标*******************************/
- (CGFloat)left {
return self.frame.origin.x;
} - (void)setLeft:(CGFloat)x {
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
} /****************************设置View顶部y坐标*********************************/
- (CGFloat)top {
return self.frame.origin.y;
} - (void)setTop:(CGFloat)y {
CGRect frame = self.frame;
frame.origin.y = y;
self.frame = frame;
} /****************************设置View右边x坐标*********************************/
- (CGFloat)right {
return self.frame.origin.x + self.frame.size.width;
} - (void)setRight:(CGFloat)right {
CGRect frame = self.frame;
frame.origin.x = right - frame.size.width;
self.frame = frame;
} /****************************设置View底部y坐标*************************************/
- (CGFloat)bottom {
return self.frame.origin.y + self.frame.size.height;
} - (void)setBottom:(CGFloat)bottom {
CGRect frame = self.frame;
frame.origin.y = bottom - frame.size.height;
self.frame = frame;
} /*****************************设置View的中心坐标********************************/
- (CGFloat)centerX {
return self.center.x;
} - (void)setCenterX:(CGFloat)centerX {
self.center = CGPointMake(centerX, self.center.y);
} - (CGFloat)centerY {
return self.center.y;
} - (void)setCenterY:(CGFloat)centerY {
self.center = CGPointMake(self.center.x, centerY);
} /**************************设置View的宽度***********************************/
- (CGFloat)width {
return self.frame.size.width;
} - (void)setWidth:(CGFloat)width {
CGRect frame = self.frame;
frame.size.width = width;
self.frame = frame;
} /*****************************设置View的高度********************************/
- (CGFloat)height {
return self.frame.size.height;
} - (void)setHeight:(CGFloat)height {
CGRect frame = self.frame;
frame.size.height = height;
self.frame = frame;
} - (CGFloat)screenX {
CGFloat x = 0;
for (UIView* view = self; view; view = view.superview) {
x += view.left;
}
return x;
} - (CGFloat)screenY {
CGFloat y = 0;
for (UIView* view = self; view; view = view.superview) {
y += view.top;
}
return y;
} - (CGFloat)screenViewX {
CGFloat x = 0;
for (UIView* view = self; view; view = view.superview) {
x += view.left; if ([view isKindOfClass:[UIScrollView class]]) {
UIScrollView* scrollView = (UIScrollView*)view;
x -= scrollView.contentOffset.x;
}
} return x;
} - (CGFloat)screenViewY {
CGFloat y = 0;
for (UIView* view = self; view; view = view.superview) {
y += view.top; if ([view isKindOfClass:[UIScrollView class]]) {
UIScrollView* scrollView = (UIScrollView*)view;
y -= scrollView.contentOffset.y;
}
}
return y;
} - (CGRect)screenFrame {
return CGRectMake(self.screenViewX, self.screenViewY, self.width, self.height);
} - (CGPoint)origin {
return self.frame.origin;
} - (void)setOrigin:(CGPoint)origin {
CGRect frame = self.frame;
frame.origin = origin;
self.frame = frame;
} - (CGSize)size {
return self.frame.size;
} - (void)setSize:(CGSize)size {
CGRect frame = self.frame;
frame.size = size;
self.frame = frame;
} - (CGPoint)offsetFromView:(UIView*)otherView {
CGFloat x = 0, y = 0;
for (UIView* view = self; view && view != otherView; view = view.superview) {
x += view.left;
y += view.top;
}
return CGPointMake(x, y);
}
/*
- (CGFloat)orientationWidth {
return UIInterfaceOrientationIsLandscape(TTInterfaceOrientation())
? self.height : self.width;
} - (CGFloat)orientationHeight {
return UIInterfaceOrientationIsLandscape(TTInterfaceOrientation())
? self.width : self.height;
}
*/
- (UIView*)descendantOrSelfWithClass:(Class)cls {
if ([self isKindOfClass:cls])
return self; for (UIView* child in self.subviews) {
UIView* it = [child descendantOrSelfWithClass:cls];
if (it)
return it;
} return nil;
} - (UIView*)ancestorOrSelfWithClass:(Class)cls {
if ([self isKindOfClass:cls]) {
return self;
} else if (self.superview) {
return [self.superview ancestorOrSelfWithClass:cls];
} else {
return nil;
}
} - (void)removeAllSubviews {
while (self.subviews.count) {
UIView* child = self.subviews.lastObject;
[child removeFromSuperview];
}
} - (UIViewController*)viewController {
for (UIView* next = [self superview]; next; next = next.superview) {
UIResponder* nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]]) {
return (UIViewController*)nextResponder;
}
}
return nil;
} - (void)addSubviews:(NSArray *)views
{
for (UIView* v in views) {
[self addSubview:v];
}
}
@end @interface NSString (ParseCategory)
- (NSMutableDictionary *)explodeToDictionaryInnerGlue:(NSString *)innerGlue
outterGlue:(NSString *)outterGlue;
@end @implementation NSString (ParseCategory) - (NSMutableDictionary *)explodeToDictionaryInnerGlue:(NSString *)innerGlue
outterGlue:(NSString *)outterGlue
{
NSArray *firstExplode = [self componentsSeparatedByString:outterGlue];
NSArray *secondExplode; NSInteger count = [firstExplode count];
NSMutableDictionary* returnDictionary = [NSMutableDictionary dictionaryWithCapacity:count]; for (NSInteger i = 0; i < count; i++)
{
secondExplode =
[(NSString*)[firstExplode objectAtIndex:i] componentsSeparatedByString:innerGlue];
if ([secondExplode count] == 2)
{
[returnDictionary setObject:[secondExplode objectAtIndex:1]
forKey:[secondExplode objectAtIndex:0]];
}
}
return returnDictionary;
} @end

版权声明:本文博客原创文章,博客,未经同意,不得转载。

UIViewAdditions(一个非常方便的工具类用它)的更多相关文章

  1. java使用注解和反射打造一个简单的jdbc工具类

    a simple jdbc tools 如有转载和引用,请注明出处,谢谢 1. 定义我们需要的注解 要想实现对数据库的操作,我们必须知道数据表名以及表中的字段名称以及类型,正如hibernate 使用 ...

  2. java 写一个JSON解析的工具类

    上面是一个标准的json的响应内容截图,第一个红圈”per_page”是一个json对象,我们可以根据”per_page”来找到对应值是3,而第二个红圈“data”是一个JSON数组,而不是对象,不能 ...

  3. 使用POI做的一个生成Excel的工具类。包含了导出Excel和解析Excel方法

    PoiExcelUtils.java /** * */ package com.common.office; import java.io.File; import java.io.FileInput ...

  4. 翻翻git之---一个丰富的通知工具类 NotifyUtil

    转载请注明出处王亟亟的大牛之路 P1(废话板块.今天还加了个小广告) 昨天出去浪,到家把麦麦当当放出来玩一会就整到了12点多..早上睡过头了. .简直心酸. ... 近期手头上有一些职位能够操作,然后 ...

  5. 自己写了一个mysql连接的工具类【java】

    要用的话,包名自己可以改一下: package com.usa3v.dreamcenter.util; import java.sql.Connection; import java.sql.Driv ...

  6. 一个简单的Hibernate工具类HibernateUtil

    HibernateUtil package com.wj.app.util; import org.hibernate.Session; import org.hibernate.SessionFac ...

  7. Android开发之Toast吐司的一个封装好的工具类。带有源代码java文件,

    import android.content.Context; import android.widget.Toast; //Toast统一管理类 public class T { private T ...

  8. 一个md5加密的工具类,用的虚拟机的包,不需要额外导包

    package com.yun.park.service.utils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import jav ...

  9. Java一个文件上传工具类

    /** * 文件上传 * * @author cary * @since 2012-12-19 下午2:22:12 */ public class FileUploader { static fina ...

随机推荐

  1. Spring整合JMS-基于activeMQ实现(二)

    Spring整合JMS-基于activeMQ实现(二) 1.消息监听器      在Spring整合JMS的应用中我们在定义消息监听器的时候一共能够定义三种类型的消息监听器,各自是MessageLis ...

  2. SqlServer保留几位小数的两种做法

    SqlServer保留几位小数的两种做法   数据库里的 float momey 类型,都会精确到多位小数.但有时候 我们不需要那么精确,例如,只精确到两位有效数字. 解决: 1. 使用 Round( ...

  3. 第4周 页面限制8060 bytes

    原文:第4周 页面限制8060 bytes 恭喜您!在你面前就只剩下几页了,然后你就可以完成第1个月的SQL Server性能调优培训了.今天我将讲下页的一些限制,还有为什么你会喜欢这些限制,同时也会 ...

  4. JDK源码学习系列02----AbstractStringBuilder

     JDK源码学习系列02----AbstractStringBuilder 因为看StringBuffer 和 StringBuilder 的源码时发现两者都继承了AbstractStringBuil ...

  5. Windows+Atlassian-Jira-6.0.4+MySql5.0安装破解汉化

     Windows+Atlassian-Jira-6.0.4+MySql5.0安装破解汉化 一:整理的安装程序 例如以下图: 文件太大.上传不到csdn上.有须要的联系. 新增的百度云盘下载:链接: ...

  6. Linux 0.12 内核管理存储器

    Linux 0.12 内核管理存储器 其分段,用分段的机制把进程间的虚拟地址分隔开. 每一个进程都有一张段表LDT.整个系统有一张GDT表.且整个系统仅仅有一个总页表. 其地址翻译过程为: 程序中给出 ...

  7. 玩转html5(一)-----盘点html5新增的那些酷酷的input类型和属性

    今天正式开始学习html5了,相比html以前的版本,html5新增了好多功能,属性,使我们做出来的界面更加的绚丽,而且使用起来超级简单,这篇文章先来说说html增加的那些input类型和属性. 这些 ...

  8. SQL声明大全

    1.随机选择3记录     select top 3 * from tablename newid() 2.随机选记录     select newid(). 3.删除反复记录 1) delete f ...

  9. uva live 6190 Beautiful Spacing (二分法+dp试 基于优化的独特性质)

    I - Beautiful Spacing Time Limit:8000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu ...

  10. Linux下如何查看高CPU占用率线程 LINUX CPU利用率计算(转)

    Java 系统性能分析 命令 1. cpu分析 top , pidstat(sysstat) pid -p PID -t 1 10 vmstat 1 CPU上下文切换.运行队列.利用率 ps Hh - ...