zoukankan      html  css  js  c++  java
  • 常用框架-UIViewExt

    .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文件

    /*
    
     Erica Sadun, http://ericasadun.com
    
     iPhone Developer's Cookbook, 3.0 Edition
    
     BSD License, Use at your own risk
    
     */
    
     
    
    #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
  • 相关阅读:
    转战物联网·基础篇06-深入理解MQTT协议之基本术语
    转战物联网·基础篇05-通俗理解MQTT协议的实现原理和异步方式
    转战物联网&#183;基础篇04-不可不知的进制关系与数据传输的本质
    转战物联网·基础篇03-从JSON数据到短指令谈思维的转变
    转战物联网·基础篇02-物联网中的角儿
    转战物联网·基础篇01-物联网之我见
    nRF24L01+组网方式及防撞(防冲突)机制的实战分享
    ElementUi中el-table分页效果
    原生无缝Banner轮播图
    详解立即执行函数(function(){}()),(function(){})()
  • 原文地址:https://www.cnblogs.com/hushuai-ios/p/3679735.html
Copyright © 2011-2022 走看看