大刘的 iOS 自学笔记

CGRectDivide

2022-06-25  本文已影响0人  大刘

Created by 大刘 liuxing8807@126.com

CGRect代表一个矩形区域, origin是左上角的点, size是宽高

/* Rectangles. */

struct CGRect {
    CGPoint origin;
    CGSize size;
};
typedef struct CG_BOXABLE CGRect CGRect;

### 矩形切割 CGRectDivide

```Objective-C
- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGRect rect = CGRectMake(60, 60, 240, 140);
    CGRect slice;
    CGRect remainder;
    CGRectDivide(rect, &slice, &remainder, 40, CGRectMaxXEdge);
    NSLog(@"slice: %@",NSStringFromCGRect(slice));
    NSLog(@"remainder: %@",NSStringFromCGRect(remainder));
    
    // typedef CF_CLOSED_ENUM(uint32_t, CGRectEdge) {
    //     CGRectMinXEdge, CGRectMinYEdge, CGRectMaxXEdge, CGRectMaxYEdge
    // };
}
cgrect_1.png

注意CGRectDivide方法的最后一个参数, 它和切割方向有关, 如果上面代码改为 CGRectMinXEdge, 则会从左边开始往右推40处切, 结果就变成了

slice: {{60, 60}, {40, 140}}
remainder: {{100, 60}, {200, 140}}

相同的Swift代码:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let rect = CGRect(x: 60, y: 60, width: 240, height: 140)
        let tuple: (slice: CGRect, remainder: CGRect) = rect.divided(atDistance: 40, from: CGRectEdge.maxXEdge)
        print("slice: ", NSCoder.string(for: tuple.slice)) // slice: {{260, 60}, {40, 140}}
        print("remainder: ", NSCoder.string(for: tuple.remainder)) // remainder: {{60, 60}, {200, 140}}
    }
}

看一个矩形切割的示例:

把一个视图平均切为4等份

cgrect_2.png
int columnCount = 4;

int column;
CGRect* columnRects = (CGRect*)calloc(columnCount, sizeof(*columnRects));
// Set the first column to cover the entire view.
columnRects[0] = self.view.bounds;

// Divide the columns equally across the frame's width.
CGFloat columnWidth = CGRectGetWidth(self.view.bounds) / columnCount;
for (column = 0; column  < columnCount - 1; column++) {
    CGRectDivide(columnRects[column], &columnRects[column], &columnRects[column + 1], columnWidth, CGRectMinXEdge);
}

for (int i = 0; i < columnCount; i++) {
    UIView *v = [[UIView alloc] initWithFrame:columnRects[i]];
    v.backgroundColor = i % 2 == 0 ? UIColor.greenColor : UIColor.purpleColor;
    [self.view addSubview:v];
}

Swift:

override func viewDidLoad() {
    super.viewDidLoad()
 
    let columnCount = 4
    var columnRects: [CGRect] = [CGRect](repeating: CGRect.zero, count: 4)
    let columnWidth = self.view.bounds.width / CGFloat(columnCount)
    columnRects[0] = self.view.bounds
    for i in 0..<columnCount-1 {
        let tuple: (slice: CGRect, remainder: CGRect) = columnRects[i].divided(atDistance: columnWidth, from: CGRectEdge.minXEdge)
        columnRects[i] = tuple.slice
        columnRects[i+1] = tuple.remainder
    }
    
    for i in 0..<columnCount {
        let rect: CGRect = columnRects[i]
        let v: UIView = UIView(frame: rect)
        v.backgroundColor = i % 2 == 0 ? UIColor.green : UIColor.purple
        self.view.addSubview(v)
        print(NSCoder.string(for: columnRects[i]))
        // {{0, 0}, {80, 568}}
        // {{80, 0}, {80, 568}}
        // {{160, 0}, {80, 568}}
        // {{240, 0}, {80, 568}}
    }
}
上一篇下一篇

猜你喜欢

热点阅读