我是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)]];
}
但图片未更新其位置
//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]];
}
}
最佳答案
您实际上需要为视图设置新框架。您只是更改了视图原始框架的副本,该副本已不再与视图建立连接。