gpt4 book ai didi

ios - 绘制圆网格

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:00:35 27 4
gpt4 key购买 nike

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

最佳答案

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

- (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);
}

enter image description here

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com