UIViewAdditions(一个非常方便的工具类用它)
我们在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(一个非常方便的工具类用它)的更多相关文章
- java使用注解和反射打造一个简单的jdbc工具类
a simple jdbc tools 如有转载和引用,请注明出处,谢谢 1. 定义我们需要的注解 要想实现对数据库的操作,我们必须知道数据表名以及表中的字段名称以及类型,正如hibernate 使用 ...
- java 写一个JSON解析的工具类
上面是一个标准的json的响应内容截图,第一个红圈”per_page”是一个json对象,我们可以根据”per_page”来找到对应值是3,而第二个红圈“data”是一个JSON数组,而不是对象,不能 ...
- 使用POI做的一个生成Excel的工具类。包含了导出Excel和解析Excel方法
PoiExcelUtils.java /** * */ package com.common.office; import java.io.File; import java.io.FileInput ...
- 翻翻git之---一个丰富的通知工具类 NotifyUtil
转载请注明出处王亟亟的大牛之路 P1(废话板块.今天还加了个小广告) 昨天出去浪,到家把麦麦当当放出来玩一会就整到了12点多..早上睡过头了. .简直心酸. ... 近期手头上有一些职位能够操作,然后 ...
- 自己写了一个mysql连接的工具类【java】
要用的话,包名自己可以改一下: package com.usa3v.dreamcenter.util; import java.sql.Connection; import java.sql.Driv ...
- 一个简单的Hibernate工具类HibernateUtil
HibernateUtil package com.wj.app.util; import org.hibernate.Session; import org.hibernate.SessionFac ...
- Android开发之Toast吐司的一个封装好的工具类。带有源代码java文件,
import android.content.Context; import android.widget.Toast; //Toast统一管理类 public class T { private T ...
- 一个md5加密的工具类,用的虚拟机的包,不需要额外导包
package com.yun.park.service.utils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import jav ...
- Java一个文件上传工具类
/** * 文件上传 * * @author cary * @since 2012-12-19 下午2:22:12 */ public class FileUploader { static fina ...
随机推荐
- 为什么不要在android或者ios上直连mysql或者sqlserver之类的数据库(跳大神)
很多同学 都有直连这些数据库的想法,假设我说了下面二个问题之后你还想直连,那我也没办法 数据库是一个服务端最重要的部分,也是最脆弱的部分,更是最敏感的部分 假设直连会造成例如以下问题 1.安全问题,你 ...
- VirtualBox安装ubuntu14.04和文件共享
因为机器的VMware使用很卡,占用更多的内存,所以我想,以取代VirtualBox.已安装ubuntu14.04使用与VMware在相同的. VirtualBox下载链接:https://www.v ...
- 编译联想A820内核源码
编译平台:Fedora 20 x64 交叉编译工具链:arm-linux-androideabi-4.6 话说这个编译工具我研究了两天,Fedora自带一个arm-none-eabi的ToolChai ...
- document.getElementById()使用方法
document.getElementById使用 语法:oElement = document .getElementById ( sID ) 參数:sID――必选项. 字符串 (String) . ...
- 使用Ratpack和Spring Boot打造高性能的JVM微服务应用
使用Ratpack和Spring Boot打造高性能的JVM微服务应用 这是我为InfoQ翻译的文章,原文地址:Build High Performance JVM Microservices wit ...
- C++习题 对象转换
[Submit][Status][Web Board] Description 定义一个Teacher(教师)类(教师号,姓名,性别,薪金)和一个Student(学生)类(学号,姓名,性别,成绩),二 ...
- Hbase结构简单、作法
Hbase架构简单介绍.实践 版权声明:本文博主原创文章,博客,未经同意不得转载.
- android Animation动画的xml使用
在Android应用程序,使用动画效果,能带给用户更好的感觉,做动画能够通过XML或Android代码来实现. Animation动画效果的实现能够通过两种方式进行实现,一种是tweened anim ...
- HTML介绍JS
首先,该脚本的链接插入HTML代码: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvU2h1aVRpYW5OYWlMdW8=/font/5a6L5L2T/f ...
- Mono for Android开发调研笔记
安装完Mono for Android(简称:MonoDroid)之后,可以用MonoDevelop或Visual Studio来开发Mono for Android应用程序:目前只能在模拟器上调试和 ...