我是iOS新手。我想在特定的位置画图像。我正在使用NSMutableArray来存储它的位置。
// I am calling addCutter method in init method of ViewController
- (void) addCutter
{
//gameModel.cutters is NSMutable Array for position
[gameModel.cutters addObject:[NSValue valueWithCGRect:gameModel.cutterRect]];
cutter = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cutter.png"]];
//cutter is UIImageView
[cutter setFrame:[[gameModel.cutters objectAtIndex:[gameModel.cutters count]-1] CGRectValue]];
cutter.tag = [cutters count] + 100;
[cutters addObject:cutter];
[self.view addSubview:[cutters objectAtIndex:([cutters count]-1)]];
}
但是Image没有更新它的位置
//code for update the position
-(void) moveCutter
{
for(int i=0;i<[gameModel.cutters count]-1;i++)
{
CGRect newCutterRect = [[gameModel.cuttersobjectAtIndex:i] CGRectValue];
newCutterRect.origin.x += [[disX objectAtIndex:i]floatValue];
newCutterRect.origin.y += [[disY objectAtIndex:i]floatValue];
[gameModel.cutters replaceObjectAtIndex:i withObject:[NSValue valueWithCGRect:newCutterRect]];
}
}
答案 0 :(得分:1)
您需要为视图实际设置新帧。您刚刚更改了视图原始帧的副本,该帧与视图无关。
答案 1 :(得分:0)
您还需要更新框架:
-(void) moveCutter
{
for (cutter in cutters) {
[cutter removeFromSuperview];
}
for(int i=0;i<[gameModel.cutters count]-1;i++)
{
CGRect newCutterRect = [[gameModel.cutters objectAtIndex:i] CGRectValue];
newCutterRect.origin.x += [[disX objectAtIndex:i]floatValue];
newCutterRect.origin.y += [[disY objectAtIndex:i]floatValue];
cutter = [cutters objectAtIndex:i];//Get your object here as well
cutter.frame = newCutterRect; // Update frame here
[self.view addSubview:cutter];
[gameModel.cutters replaceObjectAtIndex:i withObject:[NSValue valueWithCGRect:newCutterRect]];
}
}