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:方 ...
随机推荐
- GIT使用—创建一个版本库
一.GIT命令行 [root@localhost ~]# git usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path] ...
- 理解 JavaScript call()/apply()/bind()
理解 JavaScript this 文章中已经比较全面的分析了 this 在 JavaScript 中的指向问题,用一句话来总结就是:this 的指向一定是在执行时决定的,指向被调用函数的对象.当然 ...
- computeIfAbsent
// java8之前,若从map中根据key获取value,如果key不存在,则添加,这一系列操作可以是下面的操作 Object key = map.get("key"); if ...
- kali 源设置sources.list
由于阿里源有些问题,可能我设置的问题,所以就去掉了,163的很快 # deb cdrom:[Debian GNU/Linux 2016.1 _Kali-rolling_ - Official Snap ...
- J2EE--Struts2基础开发笔记
内容中包含 base64string 图片造成字符过多,拒绝显示
- Linux系统下chkconfig命令使用详解
chkconfig命令可以用来检查.设置系统的各种服务 使用语法:chkconfig [--add][--del][--list][系统服务] 或 chkconfig [--level <等级代 ...
- Dev控件-gridview的属性说明
说明 Options OptionsBehavior 视图的行为选项 AllowIncrementalSearch 允许用户通过输入想得到的列值来定位行 AllowPartialRedrawOnScr ...
- 二路归并排序,利用递归,时间复杂度o(nlgn)
public class MergeSort { public void mergeSort(int[]data, int left, int right) { if(left >= right ...
- ExtJS Ext.Ajax.request最好设为同步
ExtJS 中Ext.Ajax.request最好设为同步,即async: false,因为如果Ajax后面需要用到Ajax更新的数据的话,设置同步,后面才能用到最新的数据. function Get ...
- ZC_02_获取Constructor
1. package reflectionZ; import java.lang.reflect.Constructor; import java.lang.reflect.Type; public ...