Ios“巷自己的定义View和Android类别似

在.h文件设置了他的一些财产。方法

在.m文件中实现

.h文件

#import <UIKit/UIKit.h>

CGPoint CGRectGetCenter(CGRect rect);

CGRect  CGRectMoveToCenter(CGRect rect,
CGPoint center);

@interface UIView (ViewFrameGeometry)

@property
CGPoint origin;

@property CGSize size;

@property (readonly)
CGPoint bottomLeft;

@property (readonly)
CGPoint bottomRight;

@property (readonly)
CGPoint topRight;

@property
CGFloat height;

@property CGFloat width;

@property CGFloat top;

@property CGFloat left;

@property
CGFloat bottom;

@property CGFloat right;

- (void) moveBy: (CGPoint) delta;

- (void) scaleBy: (CGFloat) scaleFactor;

- (void) fitInSize: (CGSize) aSize;

@end

.m文件

#import "UIViewExt.h"

CGPoint CGRectGetCenter(CGRect rect)

{

CGPoint pt;

pt.x =
CGRectGetMidX(rect);

pt.y =
CGRectGetMidY(rect);

return pt;

}

CGRect CGRectMoveToCenter(CGRect rect,
CGPoint center)

{

CGRect newrect = CGRectZero;

newrect.origin.x = center.x-CGRectGetMidX(rect);

newrect.origin.y = center.y-CGRectGetMidY(rect);

newrect.size = rect.size;

return newrect;

}

@implementation UIView (ViewGeometry)

// Retrieve and set the origin

- (CGPoint) origin

{

return
self.frame.origin;

}

- (void) setOrigin: (CGPoint) aPoint

{

CGRect newframe =
self.frame;

newframe.origin = aPoint;

self.frame = newframe;

}

// Retrieve and set the size

- (CGSize) size

{

return
self.frame.size;

}

- (void) setSize: (CGSize) aSize

{

CGRect newframe =
self.frame;

newframe.size = aSize;

self.frame = newframe;

}

// Query other frame locations

- (CGPoint) bottomRight

{

CGFloat x =
self.frame.origin.x +
self.frame.size.width;

CGFloat y =
self.frame.origin.y +
self.frame.size.height;

return
CGPointMake(x, y);

}

- (CGPoint) bottomLeft

{

CGFloat x =
self.frame.origin.x;

CGFloat y =
self.frame.origin.y +
self.frame.size.height;

return
CGPointMake(x, y);

}

- (CGPoint) topRight

{

CGFloat x =
self.frame.origin.x +
self.frame.size.width;

CGFloat y =
self.frame.origin.y;

return
CGPointMake(x, y);

}

// Retrieve and set height, width, top, bottom, left, right

- (CGFloat) height

{

return
self.frame.size.height;

}

- (void) setHeight: (CGFloat) newheight

{

CGRect newframe =
self.frame;

newframe.size.height = newheight;

self.frame = newframe;

}

- (CGFloat) width

{

return
self.frame.size.width;

}

- (void) setWidth: (CGFloat) newwidth

{

CGRect newframe =
self.frame;

newframe.size.width = newwidth;

self.frame = newframe;

}

- (CGFloat) top

{

return
self.frame.origin.y;

}

- (void) setTop: (CGFloat) newtop

{

CGRect newframe =
self.frame;

newframe.origin.y = newtop;

self.frame = newframe;

}

- (CGFloat) left

{

return
self.frame.origin.x;

}

- (void) setLeft: (CGFloat) newleft

{

CGRect newframe =
self.frame;

newframe.origin.x = newleft;

self.frame = newframe;

}

- (CGFloat) bottom

{

return
self.frame.origin.y +
self.frame.size.height;

}

- (void) setBottom: (CGFloat) newbottom

{

CGRect newframe =
self.frame;

newframe.origin.y = newbottom -
self.frame.size.height;

self.frame = newframe;

}

- (CGFloat) right

{

return
self.frame.origin.x +
self.frame.size.width;

}

- (void) setRight: (CGFloat) newright

{

CGFloat delta = newright - (self.frame.origin.x +
self.frame.size.width);

CGRect newframe =
self.frame;

newframe.origin.x += delta ;

self.frame = newframe;

}

// Move via offset

- (void) moveBy: (CGPoint) delta

{

CGPoint newcenter =
self.center;

newcenter.x += delta.x;

newcenter.y += delta.y;

self.center = newcenter;

}

// Scaling

- (void) scaleBy: (CGFloat) scaleFactor

{

CGRect newframe =
self.frame;

newframe.size.width *= scaleFactor;

newframe.size.height *= scaleFactor;

self.frame = newframe;

}

// Ensure that both dimensions fit within the given size by scaling down

- (void) fitInSize: (CGSize) aSize

{

CGFloat scale;

CGRect newframe =
self.frame;

if (newframe.size.height && (newframe.size.height > aSize.height))

{

scale = aSize.height / newframe.size.height;

newframe.size.width *= scale;

newframe.size.height *= scale;

}

if (newframe.size.width && (newframe.size.width >= aSize.width))

{

scale = aSize.width / newframe.size.width;

newframe.size.width *= scale;

newframe.size.height *= scale;

}

self.frame = newframe;

}

@end

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

它们的定义View的更多相关文章

  1. Android 它们的定义View (一)

    转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/24252901 非常Android入门程序员AndroidView.可能都是比較恐 ...

  2. Android 它们的定义View它BounceProgressBar

    转载请注明出处:http://blog.csdn.net/bbld_/article/details/41246247 [Rocko's blog] 之前几天下载了非常久没用了的桌面版酷狗来用用的时候 ...

  3. Android 自己定义View (二) 进阶

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24300125 继续自己定义View之旅.前面已经介绍过一个自己定义View的基础 ...

  4. 【android自己定义控件】自己定义View属性

    1.自己定义View的属性 2.在View的构造方法中获得我们自己定义的属性 3.重写onMesure 4.重写onDraw 3这个步骤不是必须,当然了大部分情况下还是须要重写的. 1.自己定义Vie ...

  5. Qt--自定义View

    这是上一篇文章的续篇,关于自定义View. 多个View内部可以映射到同一个数据模型,也可以映射不同的数据结构:可以使用所有数据,也可以只使用部分数据.因为视图层与数据层的分离,操作相对比较灵活. 1 ...

  6. 自己定义View Controller转换动画

    原文链接 : Introduction to Custom View Controller Transitions and Animations 原文作者 : joyce echessa 译文出自 : ...

  7. Android-自定义View前传-View的三大流程-Layout

    Android-自定义View前传-View的三大流程-Layout 参考 <Android开发艺术探索> https://github.com/hongyangAndroid/FlowL ...

  8. 图文剖析自己定义View的绘制(以自己定义滑动button为例)

    自己定义View一直是横在Android开发人员面前的一道坎. 一.View和ViewGroup的关系 从View和ViewGroup的关系来看.ViewGroup继承View. View的子类.多是 ...

  9. 自己定义View Layout过程 - 最易懂的自己定义View原理系列(3)

    前言 自己定义View是Android开发人员必须了解的基础 网上有大量关于自己定义View原理的文章.但存在一些问题:内容不全.思路不清晰.无源代码分析.简单问题复杂化等等 今天,我将全面总结自己定 ...

随机推荐

  1. javascript单元测试(转)

    1.      什么是单元测试 在计算机编程中,单元测试(又称为模块测试)是针对程序模块(软件设计的最小单位)来进行正确性检验的测试工作.程序单元是应用的最小可测试部件.在过程化编程中,一个单元就是单 ...

  2. Qt 技巧:设置在 debug 路径下直接运行可执行文件

    Qt 编译的时候默认会使用影子构建,这时在工作路径下会生成一个目录:xxx-build-desktop-xxx. 如果编译通过,在该目录下会生成一个可执行文件,双击之,但不能运行,提示是缺少某个动态库 ...

  3. dubbo 服务化

    当网站变大后,不可避免的需要拆分应用进行服务化,以提高开发效率,调优性能,节省关键竞争资源等. 当服务越来越多时,服务的URL地址信息就会爆炸式增长,配置管理变得非常困难,F5硬件负载均衡器的单点压力 ...

  4. IT第十一天、第十二天、第十三天 - 数组的应用、飞行棋游戏的编写和总结

    NIIT第十一天 上午 多维数组 1.数组是引用数据类型 排序 1.冒泡排序法 2.类冒泡排序法 下午 飞行棋游戏 1.项目策划 2.项目规则确认 3.项目模块确认 晚上 1.飞行棋游戏,项目框架的编 ...

  5. BZOJ 3196

    program bzoj3196; ; maxn=; maxm=; var n,m,time,temp:longint; root,a:..maxn] of longint; left,right,r ...

  6. X-UA-Compatible IE 浏览器默认文档模式设置

    制作网页的时候,IE8浏览器浏览页面的时候,有时候文档模式默认是IE7,导致IE8兼容性不是非常好.出现IE7应该出现的模式. 解决的方法例如以下: 在X-UA-Compatible中可用的方法有: ...

  7. Swift编程语言学习2.1——基础运营商(在)

    操作员正在检查,更改.归并值特殊符号或短语.例如,加+这两个数字相加(例如let i = 1 + 2). 算如更复杂的逻辑和操作的实施&&(例如if enteredDoorCode & ...

  8. ThinkPHP - 加载第三方类库

    目录结构: 将核心的第三方目录放置在Apps下的Core目录中. 这样其他控制器便可以轻松访问. *为什么不直接放在ThinkPHP框架既有的第三方文件夹中,答案是便于升级,升级TP版本时,可直接替换 ...

  9. [Swust OJ 567]--老虎在不在笼子里(凸包问题)

    题目链接:http://acm.swust.edu.cn/problem/567/ Time limit(ms): 1000 Memory limit(kb): 65535   一只老虎自从看了< ...

  10. BZOJ 1216: [HNOI2003]操作系统( 优先队列 )

    按题意用priority_queue模拟即可 ---------------------------------------------------------------------- #inclu ...