zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    软件设计师2006年11月下午试题6(C++ 状态模式)
    Delphi中使用RegExpr单元进行匹配与替换操作
    正则表达式中贪婪与懒惰匹配
    C++类属性算法equal和mismatch
    lazarus下使用正则表达式
    正则表达式在每行开头插入行号
    STL向量构造函数
    软件设计师2004年5月下午试题6(C++ 数组下标检测)
    演示STL双端队列的push_back和push_front函数
    用正则表达式改小写为大写
  • 原文地址:https://www.cnblogs.com/pengyunjing/p/6036230.html
Copyright © 2011-2022 走看看