iOS - CGRect

在开发的时候,给控件和界面布局的一直使用CGRectMake,关于CGRect其他的方法比较少用,今天认真的去看了一下CGRect的其他方法,才发现还有很多方法对纯代码布局是很有用的。特别是下面这个几个,对于方法的作用,在代码块中又注释。在swift里,这几个方法变成了属性,同学们在调用的时候注意一点。

CGRectGetMinX,CGRectGetMaxX,CGRectGetMinY,CGRectGetMaxY,CGRectGetMidX,CGRectGetMidY

关于OC:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    view.backgroundColor = [UIColor greenColor];
    [self.view addSubview:view];
    
    //获取view的最小X值,也就是这个view的X值
    CGFloat minX = CGRectGetMinX(view.frame);
    NSLog(@"minx is %f",minX);
    
    //获取view的最大的X值,也就是这个view的X值+view的宽
    CGFloat maxX = CGRectGetMaxX(view.frame);
    NSLog(@"maxX is %f",maxX);
    
    //获取view的最小Y值,也就是这个view的Y值
    CGFloat minY = CGRectGetMinY(view.frame);
    NSLog(@"minY is %f",minY);
    
    //获取view的最大Y值,也就是这个view的Y值+view的高度
    CGFloat maxY = CGRectGetMaxY(view.frame);
    NSLog(@"maxY is %f",maxY);
    
    //获取view的中点X
    CGFloat midX = CGRectGetMidX(view.frame);
    NSLog(@"midX is %f",midX);
    
    //获取view的中点Y
    CGFloat midY = CGRectGetMidY(view.frame);
    NSLog(@"midY is %f",midY);
    
    //获取view的高
    CGFloat getWidth = CGRectGetWidth(view.frame);
    NSLog(@"getWidth is %f",getWidth);
    
    //获取view的宽
    CGFloat getHeight = CGRectGetHeight(view.frame);
    NSLog(@"getHeight is %f",getHeight);
    
    //判断两个view大小是否一样
    BOOL flag = CGRectEqualToRect(view.frame, self.view.frame);
    NSLog(@"flag is %d",flag);
    
    //
    CGRect rect = CGRectStandardize(view.frame);
    NSLog(@"rect x = %f, y = %f, width = %f, height = %f",rect.origin.x,rect.origin.y,rect.size.width,rect.size.height);
    
    //判断view的rect是否为空
    BOOL flag1 = CGRectIsNull(view.frame);
    NSLog(@"flag is %d",flag1);
    //断view的rect是否为空
    BOOL flag2 = CGRectIsEmpty(view.frame);
    NSLog(@"flag2 is %d",flag2);
    
    //基于某个view,传入X和Y,获取一个新的rect。
    //官方文档给出的解释是另个view是否相交
    CGRect smallRect = CGRectInset(view.frame, -10, 10);
    NSLog(@"%f,%f,%f,%f",smallRect.origin.x,smallRect.origin.y,smallRect.size.width,smallRect.size.height);
    
    UIView *view1 = [[UIView alloc]initWithFrame:smallRect];
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];  
}

图片是CGRectInset这个方法的效果


iOS - CGRect_第1张图片
Simulator Screen Shot 2016年9月24日 18.30.59.png

关于Swift:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.white
        
        let greenView = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
        greenView.backgroundColor = UIColor.green
        view.addSubview(greenView)
        
        let minX: CGFloat = greenView.frame.minX
        print("minX is \(minX)")
        
        let maxX: CGFloat = greenView.frame.maxX
        print("maxX is \(maxX)")
        
        let minY: CGFloat = greenView.frame.minY
        print("minY is \(minY)")
        
        let maxY: CGFloat = greenView.frame.maxY
        print("maxY is \(maxY)")
        
        let midX: CGFloat = greenView.frame.midX
        print("midX is \(midX)")
        
        let midY: CGFloat = greenView.frame.midY
        print("midY is \(midY)")
        
        let width: CGFloat = greenView.frame.width
        print("width is \(width)")
        
        let height: CGFloat = greenView.frame.height
        print("height is \(height)")
        
        let rect: CGRect = greenView.frame.insetBy(dx: 10, dy: 10)
        
        let redView = UIView(frame: rect)
        redView.backgroundColor = UIColor.red
        view.addSubview(redView)
    }
iOS - CGRect_第2张图片
Simulator Screen Shot 2016年9月24日 18.27.35.png

啊啊啊,感觉自己坑了自己一大波,走了巨大段的弯路,还是怪自己看官方文档看的少啊。

你可能感兴趣的:(iOS - CGRect)