UIView+PYJExtension
UIView+PYJExtension.h:
//
// UIView+PYJExtension.h
// 扩展
//
// Created by 彭运京 on 16/6/21.
// Copyright © 2016年 PYJ. All rights reserved.
// #import <UIKit/UIKit.h> CGPoint CGRectGetCenter(CGRect rect);
CGRect CGRectMoveToCenter(CGRect rect, CGPoint center); @interface UIView (PYJExtension) @property CGPoint origin;
@property CGSize size; @property (readonly) CGPoint bottomLeft;
@property (readonly) CGPoint bottomRight;
@property (readonly) CGPoint topRight; /// 高
@property CGFloat height;
/// 宽
@property CGFloat width;
/// Y坐标
@property CGFloat top;
/// X坐标
@property CGFloat left;
/// 底部 Y坐标+高度
@property CGFloat bottom;
/// 右侧 X坐标+宽度
@property CGFloat right; - (void) moveBy: (CGPoint) delta;
- (void) scaleBy: (CGFloat) scaleFactor;
- (void) fitInSize: (CGSize) aSize; /**
* 判断一个控件是否真正显示在主窗口
*/
- (BOOL)isShowingOnKeyWindow; //- (CGFloat)x;
//- (void)setX:(CGFloat)x;
/** 在分类中声明@property, 只会生成方法的声明, 不会生成方法的实现和带有_下划线的成员变量*/ + (instancetype)viewFromXib; @end
UIView+PYJExtension.m:
//
// UIView+PYJExtension.m
// 扩展
//
// Created by 彭运京 on 16/6/21.
// Copyright © 2016年 PYJ. All rights reserved.
// #import "UIView+PYJExtension.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 (PYJExtension) // 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;
} - (BOOL)isShowingOnKeyWindow
{
// 主窗口
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow; // 以主窗口左上角为坐标原点, 计算self的矩形框
CGRect newFrame = [keyWindow convertRect:self.frame fromView:self.superview];
CGRect winBounds = keyWindow.bounds; // 主窗口的bounds 和 self的矩形框 是否有重叠
BOOL intersects = CGRectIntersectsRect(newFrame, winBounds); return !self.isHidden && self.alpha > 0.01 && self.window == keyWindow && intersects;
} + (instancetype)viewFromXib
{
return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject]; } @end
UIView+PYJExtension的更多相关文章
- UIView的layoutSubviews和drawRect方法何时调用
首先两个方法都是异步执行.layoutSubviews方便数据计算,drawRect方便视图重绘. layoutSubviews在以下情况下会被调用: 1.init初始化不会触发layoutSubvi ...
- iOS 自定义方法 - UIView扩展
示例代码 //#import <UIKit/UIKit.h>@interface UIView (LPCView)/** 上 */@property CGFloat top;/** 下 * ...
- UIView上的控件使用push方法跳转
有时候在项目中,为了保持前后页面的推进方式跳转方式一致,会在通过UIview上的控件跳到另一个Controller上,所以,这时候就需要用到这种方式了,当然,present方法可以实现跳转但是样式可能 ...
- IOS 杂笔-11(实现在外部无法改变UIView的size)
我想题目说的或许不是很清楚,那么现在我详细介绍一下这篇随笔内容. 在外部无法改变UIVIew控件的size. 这里说是UIView,但是事实上,是大多数控件而绝非仅UIView. 想要实现在外部无法改 ...
- iOS系列 基础篇 05 视图鼻祖 - UIView
iOS系列 基础篇 05 视图鼻祖 - UIView 目录: UIView“家族” 应用界面的构建层次 视图分类 最后 在Cocoa和Cocoa Touch框架中,“根”类时NSObject类.同样, ...
- 5. UIView
1. UIView 的初认识 官方文档 UIView class defines a rectangular area on the screen and the interfaces for man ...
- Swift - UIView,UItableView,Cell设置边框方法
// 设置边框的宽度 cell.layer.borderWidth = 1 // 设置边框的颜色 cell.layer.borderColor = UIColor.blackColor().CGCol ...
- iOS 使点击事件穿透透明的UIView
如图: 悬浮的三个按钮下方有一个可以点击的灰色区域,但是点击按钮之间的透明区域, 这三个按钮的contentView会响应这个点击事件,这时候需要让这个contentView不响应这个点击事件. 解决 ...
- iOS----自定义UIView,绘制一个UIView
绘制一个UIVIew最灵活的方式就是由它自己完成绘制.实际上你不是绘制一个UIView,你只是子类化了UIView并赋予子类绘制自己的能力.当一个UIVIew需要执行绘图操作的时,drawRect:方 ...
随机推荐
- python继承,判断类型,多态
1.python中继承 如果已经定义了Person类,需要定义新的Student和Teacher类时,可以直接从Person类继承: class Person(object): def __init_ ...
- Wyx20162314 2016-2017-2 《程序设计与数据结构》课程总结
20162314 2016-2017-2 <程序设计与数据结构>课程总结 一.每周作业.结对编程博客的链接汇总 预备作业一01 20162314:专业的期许.浅谈师生关系.对未来学习任务的 ...
- python_初步
官网地址:http://www.python.org/ Python最新源码,二进制文档,新闻资讯 Python文档下载地址:www.python.org/doc/ python教程:http://w ...
- Android Studio 入门级教程
引用原文:http://www.cnblogs.com/abao0/p/6934023.html 写博客是为了记住自己容易忘记的东西,另外也是对自己工作的总结,文章可以转载,无需版权.希望尽自己的努力 ...
- Spring AOP(2)
- sql行列互换
出现这个结果: sql如下: end) as erjidu from a GROUP BY y;
- PAT1042. Shuffling Machine (20)
#include <iostream> #include <vector> using namespace std; int n; string card[54]={" ...
- Java 自定义注解实现ORM对象关系映射
一,ORM概念 ORM即Object Relation Mapping,Object就是对象,Relation就是关系数据库,Mapping映射,就是说Java中的对象和关系数据库中的表存在一种对应关 ...
- java中HashMap、HashTable、TreeMap的区别总结【表格对比清楚明了】
底层 有序否 键值对能否为Null 遍历 线程安全 哈希Code Hashmap 数组+链表 无序 都可null iterator 不安全 内部hash方法 Hashtable 数组+链表 无序 ...
- H5滑动穿透(TODO)
https://github.com/pod4g/tool/wiki/%E7%A7%BB%E5%8A%A8%E7%AB%AF%E6%BB%9A%E5%8A%A8%E7%A9%BF%E9%80%8F%E ...