我们在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. Android中目的地Intent的使用

    一.什么是Intent? Intent的中文意思是目的.在Android中也是“目的”的意思.就是我们要去哪里,从这个activity要前往另一个Activity就需要用到Intent. 示例代码一: ...

  2. Cocos2d-x 3.2 Lua演示样例 ClickAndMoveTest(点击移动測试)

    Cocos2d-x 3.2 Lua演示样例 ClickAndMoveTest(点击移动測试)  本篇博客介绍Cocos2d-x 3.2Lua演示样例中点击移动的样例,在这个样例你能够得到怎样创建单点触 ...

  3. hibernate操作数据库总结

    这篇文章用于总结hibernate操作数据库的各种方法 一.query方式 1.hibernate使用原生态的sql语句执行数据库查询 有些时候有些开发人员总觉得用hql语句不踏实,程序出现了错误,就 ...

  4. poj2348(博弈)

    poj2348 给定两个数a,b,大的数能减少小的数的倍数,不能是的数小于0,谁先使得数等于0,谁就赢了 有三种情况 ① a % b ==0  这个状态是必胜的 ② a - b < b  这个状 ...

  5. 状态压缩dp(hdu2167,poj2411)

    hdu2167 http://acm.hdu.edu.cn/showproblem.php?pid=2167 给定一个N*N的板子,里面有N*N个数字,选中一些数字,使得和最大 要求任意两个选中的数字 ...

  6. ACM:回溯,八皇后问题,素数环

    (一)八皇后问题 (1)回溯 #include <iostream> #include <string> #define MAXN 100 using namespace st ...

  7. 【夸QT十一】外来物品:通用脚本帮助Web运行基础Linux命令

    需求分析: 需要注意的是在这里第一次,这个人是不是QT系列文章,它是关于Web的,之所以写这篇文章.这是因为碍着Web相关开发时间,而且往往涉及linux与底层指令处理.例如,创建一个文件夹,删除一个 ...

  8. Word001

    C# Word 类库 2009-08-06 22:10 13470人阅读 评论(10) 收藏 举报 c#objectstring文档microsoftexcel using System;using ...

  9. C#(SuperWebSocket)与websocket通信

    原文:C#(SuperWebSocket)与websocket通信 客户端代码 点击可以查看一些关于websocket的介绍 <!DOCTYPE html> <html> &l ...

  10. Android自己主动化測试——CTS測试

    一.为什么须要兼容性測试(下面称CTS)? 1.1.让APP提供更好的用户体验.用户能够选择很多其它的适合自己设备的APP.让APP更稳定. 1.2.让开发人员设计更高质量的APP. 1.3.通过CT ...