zoukankan      html  css  js  c++  java
  • iOS:frame访问、设置简化

    看到一些程序都有这种写法,也不知道原创者是谁了。先在博客保存下。

    在.m文件

    #import "UIView+MyFrameCategory.h"
    
    @implementation UIView (MyFrameCategory)
    
    //=================================
    - (void)setX:(CGFloat)x;
    {
        CGRect  tempFrame = self.frame;
        tempFrame.origin.x = x;
        self.frame = tempFrame;
    }
    
    - (CGFloat)x;
    {
        return self.frame.origin.x;
    }
    //=================================
    - (void)setY:(CGFloat)y;
    {
        CGRect  tempFrame = self.frame;
        tempFrame.origin.y = y;
        self.frame = tempFrame;
    }
    
    - (CGFloat)y;
    {
        return self.frame.origin.y;
    }
    //=================================
    - (void)setWidth:(CGFloat)width;
    {
        CGRect  tempFrame = self.frame;
        tempFrame.size.width = width;
        self.frame = tempFrame;
    }
    
    - (CGFloat)width;
    {
        return self.frame.size.width;
    }
    //=================================
    - (void)setHeight:(CGFloat)height;
    {
        CGRect  tempFrame = self.frame;
        tempFrame.size.height = height;
        self.frame = tempFrame;
    }
    
    - (CGFloat)height;
    {
        return self.frame.size.height;
    }
    //=================================
    - (void)setSize:(CGSize)size;
    {
        CGRect  tempFrame = self.frame;
        tempFrame.size = size;
        self.frame = tempFrame;
    }
    
    - (CGSize)size;
    {
        return self.frame.size;
    }
    //=================================
    - (void)setCenterX:(CGFloat)x;
    {
        CGPoint tempPoint = self.center;
        tempPoint.x = x;
        self.center = tempPoint;
    }
    
    - (CGFloat)centerX;
    {
        return self.center.x;
    }
    //=================================
    - (void)setCenterY:(CGFloat)y;
    {
        CGPoint tempPoint = self.center;
        tempPoint.y = y;
        self.center = tempPoint;
    }
    
    - (CGFloat)centerY;
    {
        return self.center.y;
    }
    //=================================
    
    @end
    

    在.h文件

    #import <UIKit/UIKit.h>
    
    @interface UIView (MyFrameCategory)
    
    @property(nonatomic,assign) CGFloat x;
    @property(nonatomic,assign) CGFloat y;
    @property(nonatomic,assign) CGFloat width;
    @property(nonatomic,assign) CGFloat height;
    @property(nonatomic,assign) CGFloat centerX;
    @property(nonatomic,assign) CGFloat centerY;
    @property(nonatomic,assign) CGSize size;
    
    @end
    
  • 相关阅读:
    1295. 统计位数为偶数的数字『简单』
    1281. 整数的各位积和之差『简单』
    697. 数组的度『简单』
    748. 最短完整词『简单』
    832. 翻转图像『简单』
    1446. 连续字符『简单』
    1455. 检查单词是否为句中其他单词的前缀『简单』
    1160. 拼写单词『简单』
    1304. 和为零的N个唯一整数『简单』
    1103. 分糖果 II『简单』
  • 原文地址:https://www.cnblogs.com/leonlincq/p/6122070.html
Copyright © 2011-2022 走看看