zoukankan      html  css  js  c++  java
  • iOS开发-UIView扩展CGRect

    关于UIView的位置都会遇到,一般需要改变UIView的位置,需要先获取原有的frame位置,然后在frame上面修改,有的时候如果只是改变了一下垂直方向的位置,宽度和高度的一种,这种写法很麻烦。下面两种写法第二种明显更简单,如果需要实现第二种方法就需要扩展UIView。

        //1
        CGRect frame=self.testView.frame;
        frame.size.width=120;
        self.testView.frame=frame;
        [self printFrame];
        //2
        self.testView.width=120;
        [self printFrame];
    

    扩展定义:

    @interface UIView (ReSize)
    
    @property (nonatomic, assign) CGSize size;
    
    @property (nonatomic,assign)  CGFloat x;
    
    @property  (nonatomic,assign) CGFloat y;
    
    @property (nonatomic, assign) CGFloat top;
    
    @property (nonatomic, assign) CGFloat bottom;
    
    @property (nonatomic, assign) CGFloat left;
    
    @property (nonatomic, assign) CGFloat right;
    
    @property (nonatomic, assign) CGFloat centerX;
    
    @property (nonatomic, assign) CGFloat centerY;
    
    @property (nonatomic, assign) CGFloat width;
    
    @property (nonatomic, assign) CGFloat height;
    
    @end
    

     扩展实现:

    @implementation UIView (ReSize)
    
    - (CGSize)size;
    {
        return [self frame].size;
    }
    
    - (void)setSize:(CGSize)size;
    {
        CGPoint origin = [self frame].origin;
        [self setFrame:CGRectMake(origin.x, origin.y, size.width, size.height)];
    }
    
    -(CGFloat)x{
        CGRect frame=[self frame];
        return frame.origin.x;
    }
    
    -(void)setX:(CGFloat)x{
        CGRect frame=[self frame];
        frame.origin.x=x;
        [self setFrame:frame];
    }
    
    -(CGFloat)y{
        CGRect frame=[self frame];
        return frame.origin.y;
    }
    
    -(void)setY:(CGFloat)y{
        CGRect frame=[self frame];
        frame.origin.y=y;
        return [self setFrame:frame];
    }
    
    - (CGFloat)left;
    {
        return CGRectGetMinX([self frame]);
    }
    
    - (void)setLeft:(CGFloat)x;
    {
        CGRect frame = [self frame];
        frame.origin.x = x;
        [self setFrame:frame];
    }
    
    - (CGFloat)top;
    {
        return CGRectGetMinY([self frame]);
    }
    
    - (void)setTop:(CGFloat)y;
    {
        CGRect frame = [self frame];
        frame.origin.y = y;
        [self setFrame:frame];
    }
    
    - (CGFloat)right;
    {
        return CGRectGetMaxX([self frame]);
    }
    
    - (void)setRight:(CGFloat)right;
    {
        CGRect frame = [self frame];
        frame.origin.x = right - frame.size.width;
        
        [self setFrame:frame];
    }
    
    - (CGFloat)bottom;
    {
        return CGRectGetMaxY([self frame]);
    }
    
    - (void)setBottom:(CGFloat)bottom;
    {
        CGRect frame = [self frame];
        frame.origin.y = bottom - frame.size.height;
        [self setFrame:frame];
    }
    
    - (CGFloat)centerX;
    {
        return [self center].x;
    }
    
    - (void)setCenterX:(CGFloat)centerX;
    {
        [self setCenter:CGPointMake(centerX, self.center.y)];
    }
    
    - (CGFloat)centerY;
    {
        return [self center].y;
    }
    
    - (void)setCenterY:(CGFloat)centerY;
    {
        [self setCenter:CGPointMake(self.center.x, centerY)];
    }
    
    - (CGFloat)width;
    {
        return CGRectGetWidth([self frame]);
    }
    
    - (void)setWidth:(CGFloat)width;
    {
        CGRect frame = [self frame];
        frame.size.width = width;
        [self setFrame:CGRectStandardize(frame)];
    }
    
    - (CGFloat)height;
    {
        return CGRectGetHeight([self frame]);
    }
    
    - (void)setHeight:(CGFloat)height;
    {
        CGRect frame=[self frame];
        frame.size.height = height;
        [self setFrame:CGRectStandardize(frame)];
    }
    
    @end
    

    项目源代码地址:https://github.com/SmallElephant/iOS-FEViewReSize

  • 相关阅读:
    Overloaded的方法是否可以改变返回值的类型
    parseXXX的用法
    java的类型转换问题。int a = 123456;short b = (short)a;System.out.println(b);为什么结果是-7616?
    UVA 10405 Longest Common Subsequence(简单DP)
    POJ 1001 Exponentiation(大数处理)
    POJ 2318 TOYS(计算几何)(二分)
    POJ 1265 Area (计算几何)(Pick定理)
    POJ 3371 Flesch Reading Ease (模拟题)
    POJ 3687 Labeling Balls(拓扑序列)
    POJ 1094 Sorting It All Out(拓扑序列)
  • 原文地址:https://www.cnblogs.com/xiaofeixiang/p/5119677.html
Copyright © 2011-2022 走看看