在iOS上实现绘制具有固定列和行的圆形网格的最佳方法是什么?我想在用户点击时识别每个圆圈。
我尝试使用集合视图,但似乎CoreGraphics适用于此类任务。

最佳答案

您可以如下动态创建按钮:

- (void)drawSheet
{
    int numberOfRow=5;
    int numberOfColumn=4;
    float x = 1,
            y = 20,
            padding = 5,
            width = (self.view.frame.size.width-(padding*(numberOfColumn-1)))/numberOfColumn,
            height = (self.view.frame.size.height-(padding*(numberOfRow-1)))/numberOfRow;
    int counter = 0;
    for (int i=0; i<numberOfRow; i++) {
        for (int j=0; j<numberOfColumn; j++) {
            UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, 74, 74)];
            btn.layer.cornerRadius = btn.frame.size.width/2;
            btn.layer.borderColor = [UIColor grayColor].CGColor;
            btn.layer.borderWidth = 2.0;
            [btn addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
            btn.clipsToBounds = YES;
            btn.tag = counter;
            [self.view addSubview:btn];
            x = x + width + padding;
            counter = counter + 1;
        }
        x = 0;
        y = y + height + padding;
    }
}

当您点击它时,您将得到它的标签:
- (IBAction)buttonPress:(UIButton *)sender{
    NSLog(@"%ld",sender.tag);
}

关于ios - 画圆网格,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31219384/

10-14 23:49