我们在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. Java4Android之BlockingQueue

    在研究Smack的源码的时候,我对它的连接Connection以及派生类XMPPConnection的关注是最多的,由于一个即时通信程序,它的网络模块必是它的核心. 而我非常在乎它是怎样实现的. 在收 ...

  2. Why 使用TLS记录封装IP层VPN IS A Bad Idea

    一个很自然的想法,使用TLS套餐一IP数据报实现第三层VPN.这种想法必须经过深思熟虑的,但不幸的是,.这是一个错误的想法.有文章<Why TCP Over TCP Is A Bad Idea& ...

  3. Codeforces Round#309 C Kyoya and Colored Balls

    给定一个k表示颜色的种类从1到k 然后接下来k行, 每行一个数字, 代表该颜色的球有多少个 这些球都放在一个包中,然后依次拿出.  要求颜色i的最后一个球, 必须要排在颜色i+1的最后一个球前面,   ...

  4. dij算法为什么不能处理负权,以及dij算法变种

    对于上面那张图,是可以用dij算法求解出正确答案,但那只是巧合而已. 我们再看看下面这张图. dist[4] 是不会被正确计算的. 因为dij算法认为从队列出来的点,(假设为u)肯定是已经求出最短路的 ...

  5. hdu 4885 (n^2*log(n)推断三点共线建图)+最短路

    题意:车从起点出发,每次仅仅能行驶L长度,必需加油到满,每次仅仅能去加油站或目的地方向,路过加油站就必需进去加油,问最小要路过几次加油站. 開始时候直接建图,在范围内就有边1.跑最短了,再读题后发现, ...

  6. 找出二叉树中和为n的路径

    题目描述: 输入一个整数和一棵二元树.从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径.打印出和 与输入整数相等的所有路径. 二叉树中的路径 从二叉树的根节点出发,至二叉树的叶子节点的 ...

  7. Google的Guava它Collection升华

    至于Guava这是不是在这里说.一个已被提上一个非常特殊的! 这主要是为了分享Guava对于一些升华处理组.井,不多说了,直接在代码: package com.joyce.guava.bean; /* ...

  8. AccountManager教程

    API阅读 此类提供所述用户接口到集中登记帐户. 用户只需输入一次帐号password后,您将能够访问internet资源. 不同的在线服务用不同的方式来管理用户,所以account manager ...

  9. AutoFac使用方法总结:Part III

    生命周期 AutoFac中的生命周期概念非常重要,AutoFac也提供了强大的生命周期管理的能力. AutoFac定义了三种生命周期: Per Dependency Single Instance P ...

  10. 解决apache+tomcatserver环境中文乱码的问题

    在使用apache做转发服务器时,碰到了中文乱码的问题. 说说解决思路: 1.通常乱码是由于编码不统一造成的.所以要先推断是不是由于编码问题造成的,假设是的话,那统一编码就能够去解决. 2.tomca ...