CGRectInset和CGRectOffset 意思 和 區別

1、CGRectInset

CGRect CGRectInset (
   CGRect rect,
   CGFloat dx,
   CGFloat dy
);
該結構體的應用是以原rect爲中心,再參考dx,dy,進行縮放或者放大。


圖中的每一個矩形都是以上一個矩形作爲參考矩形。所以下一矩形(比如黃色矩形對綠色矩形來說是下一個矩形)都比上一個矩形要小。

具體小多少都是要參照dx和dy來判定的。
CGContextAddRect(context, currentRect);

            CGContextDrawPath(context, kCGPathFillStroke);

            CGRect original = CGRectMake(100, 100, 200, 200);

            CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);

            CGContextAddRect(context, original);

            CGContextDrawPath(context, kCGPathFillStroke);


            CGRect firstRect = CGRectInset(original, 10, 10);

            CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);

            CGContextAddRect(context, firstRect);

            CGContextDrawPath(context, kCGPathFillStroke);

            
            CGRect secondRect = CGRectInset(firstRect, 10, 10);

            CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);

            CGContextAddRect(context, secondRect);

            CGContextDrawPath(context, kCGPathFillStroke);

            
            CGRect thirdRect = CGRectInset(secondRect, 10, 10);

            CGContextSetFillColorWithColor(context, [UIColor yellowColor].CGColor);

            CGContextAddRect(context, thirdRect);

            CGContextDrawPath(context, kCGPathFillStroke);

            

            CGRect fourRect = CGRectInset(thirdRect, 10, 10);

            CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor);

            CGContextAddRect(context, fourRect);


2、CGRectOffset

CGRect CGRectOffset(
        CGRect rect,
         CGFloat dx,
         CGFloat dy
);
    

相對於源矩形原點rect(左上角的點)沿x軸和y軸偏移, 再rect基礎上沿x軸和y軸偏移

    float offset = 125.0;
    CGRect r1 = CGRectMake(100, 100, 5, 5);
    CGRect r2 = CGRectOffset(r1, offset, offset);

3、frame和dounds

frame和bounds是UIView中的兩個屬性(property)。

-(CGRect)frame{
    return CGRectMake(self.frame.origin.x,self.frame.origin.y,self.frame.size.width,self.frame.size.height);
}

-(CGRect)bounds{
    return CGRectMake(0,0,self.frame.size.width,self.frame.size.height);
}

frame指的是:該view在父view座標系統中的位置和大小。(參照點是父親的座標系統)

bounds指的是:該view在本身座標系統中 的位置和大小。(參照點是本身座標系統)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章