2013-11-26 38 views
0

我是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]]; 
    } 
} 

回答

1

实际上你需要设置视图的新框架。您只是更改了视图原始框架的副本,该框架与视图无关。

+0

如何为没有,它仍然没有改变 – user2546410

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]]; 
    } 
    } 
+0

设置新的框架? – user2546410

+0

@ user2546410这有什么问题,代码你所得到的视图 –

+0

我已将上述代码放入我的项目中,但图像位置不变 – user2546410