Iphone CGRectMake的问题
我一直在尝试在iPhone上创建一个16x16图像的网格Iphone CGRectMake的问题,iphone,objective-c,cocoa-touch,Iphone,Objective C,Cocoa Touch,我一直在尝试在iPhone上创建一个16x16图像的网格 - (void)viewDidLoad { [super viewDidLoad]; int xLocation = 0; for (int i=0; i <= 619; i++) { if (((i % 30) == 0) && i != 0) { //if at end of column (30th row), moves x over 16 px to next
- (void)viewDidLoad {
[super viewDidLoad];
int xLocation = 0;
for (int i=0; i <= 619; i++) {
if (((i % 30) == 0) && i != 0) { //if at end of column (30th row), moves x over 16 px to next column
xLocation += 16;
}
else {;}
CGRect imageRect = CGRectMake( xLocation, (16*i), 16, 16);
UIImageView *image = [[UIImageView alloc] initWithFrame:imageRect];
[image setImage:[UIImage imageNamed:@"Untitled-1.png"]];
[image setOpaque:YES];
NSLog(@"%d", xLocation);
[self.view addSubview:image];
[image release];
}
-(void)viewDidLoad{
[超级视图下载];
int xLocation=0;
对于(int i=0;i%d不能正确显示浮点值。请使用%f或将xLocation转换为int:
NSLog(@"%d", (int)xLocation);
您还可以通过每次计算来简化xLocation计算的可读性,例如
16 * (i / 30)
在现代处理器上,开销最小。%d不能正确显示浮点值。请使用%f或将xLocation转换为int:
NSLog(@"%d", (int)xLocation);
您还可以通过每次计算来简化xLocation计算的可读性,例如
16 * (i / 30)
在现代处理器上,开销是最小的。第一列的xLocation为0,每列的xLocation都在增加,但每一列的y坐标都没有重置为0。它只是在i的基础上不断增加。因此,从第二列开始的列就在屏幕右侧xLocation处,但y值很高
尝试将imageRect计算更改为:
CGRect imageRect = CGRectMake( xLocation, (16*(i % 30)), 16, 16);
第一列的xLocation为0,并且每列的xLocation都会增加,但是每列的y坐标都不会重置为0。它只是基于i不断增加。因此,从第二列开始,右侧xLocation的列只是在屏幕外,但y值较高
尝试将imageRect计算更改为:
CGRect imageRect = CGRectMake( xLocation, (16*(i % 30)), 16, 16);
在NSLog中将xLocation转换为int肯定会起作用,但您也可以使用:
NSLog(@"imageRect %@", NSStringFromCGRect(imageRect));
有一系列的NSStringFromXXX帮助函数随时都会派上用场。在NSLog中将XLLocation转换为int肯定会起作用,但您也可以使用:
NSLog(@"imageRect %@", NSStringFromCGRect(imageRect));
有一系列的NSStringFromXXX帮助函数不时派上用场。xLocation
类型为int
xLocation
类型为int
。非常感谢,我一直专注于xLocation,其实这并不是问题。非常感谢,我一直专注于xLocation,而不是问题所在实际上,这是个问题。