到目前为止,我在view.m文件中的drawRect方法中有一个for循环。我让for循环显示x轴上的图像。我想要做的是,不仅可以在x轴上,也可以在y轴上,制作一个图像网格。换句话说,你的典型网格。我还想让网格中的每个重复图像都成为一个对象,并附加一些属性,例如布尔值,触摸时可以通过其检索的id,以及它的坐标。在objective-c中我该如何去做呢?这是我到目前为止所拥有的,并不是很多:
- (void)drawRect:(CGRect)rect
{
int intX = 0;
int intCounter = 0;
int intY = 0;
for (intCounter = 0; intCounter < 10; intCounter++) {
UIImage* pngLeaf = [UIImage imageNamed:@"leaf2.png"];
CGRect imgRectDefault = CGRectMake(intX, 0, 34, 34);
[pngLeaf drawInRect:imgRectDefault];
intX += 32;
intY += 32;
}
}发布于 2012-04-13 10:27:10
使用UIViews会让你的工作变得更轻松。
这里有一个网格例程-它可以写得更紧凑,但它更容易理解,因为有很多显式声明的变量。将其放入您的主ViewController中并在ViewWillAppear中调用它。
- (void)makeGrid
{
int xStart = 0;
int yStart = 0;
int xCurrent = xStart;
int yCurrent = yStart;
UIImage * myImage = [UIImage imageNamed:@"juicy-tomato_small.png"];
int xStepSize = myImage.size.width;
int yStepSize = myImage.size.height;
int xCnt = 8;
int yCnt = 8;
int cellCounter = 0;
UIView * gridContainerView = [[UIView alloc] init];
[self.view addSubview:gridContainerView];
for (int y = 0; y < yCnt; y++) {
for (int x = 0; x < xCnt; x++) {
printf("xCurrent %d yCurrent %d \n", xCurrent, yCurrent);
UIImageView * myView = [[UIImageView alloc] initWithImage:myImage];
CGRect rect = myView.frame;
rect.origin.x = xCurrent;
rect.origin.y = yCurrent;
myView.frame = rect;
myView.tag = cellCounter;
[gridContainerView addSubview:myView];
// just label stuff
UILabel * myLabel = [[UILabel alloc] init];
myLabel.textColor = [UIColor blackColor];
myLabel.textAlignment = UITextAlignmentCenter;
myLabel.frame = rect;
myLabel.backgroundColor = [UIColor clearColor];
myLabel.text = [NSString stringWithFormat:@"%d",cellCounter];
[gridContainerView addSubview:myLabel];
//--------------------------------
xCurrent += xStepSize;
cellCounter++;
}
xCurrent = xStart;
yCurrent += yStepSize;
}
CGRect repositionRect = gridContainerView.frame;
repositionRect.origin.y = 100;
gridContainerView.frame = repositionRect;
}https://stackoverflow.com/questions/10133881
复制相似问题