2012-07-07 55 views
0

在我正在構建的應用程序中,導入了方形圖像,它被分成16個圖塊,並且從頂部開始將這些圖塊放置在屏幕上的正方形中。但是,我在瓦片頂部的頂部添加了一個額外的信息欄,因此我需要將這組瓦片進一步向下移動到屏幕上。我已經玩過代碼所做的for循環和座標,但沒有給我所期望的尊重。定位多個UIImage - 目標C

-(void) initPuzzle:(NSString *) imagePath{ 
UIImage *orgImage = [UIImage imageNamed:imagePath]; 

if(orgImage == nil){ 
    return; 
} 

[self.tiles removeAllObjects]; 

tileWidth = orgImage.size.width/NUM_HORIZONTAL_PIECES; 
tileHeight = orgImage.size.height/NUM_VERTICAL_PIECES; 

blankPosition = CGPointMake(NUM_HORIZONTAL_PIECES-1, NUM_VERTICAL_PIECES-1); 

for(int x=0; x<NUM_HORIZONTAL_PIECES; x++){ 
    for(int y=0; y<NUM_VERTICAL_PIECES; y++){ 

     // *** The coordinates need to be altered - ? 
     CGPoint orgPosition = CGPointMake(x,y); 

     if(blankPosition.x == orgPosition.x && blankPosition.y == orgPosition.y){ 
      continue; 
     } 

     CGRect frame = CGRectMake(tileWidth*x, tileHeight*y, 
            tileWidth, tileHeight); 
     CGImageRef tileImageRef = CGImageCreateWithImageInRect(orgImage.CGImage, frame); 
     UIImage *tileImage = [UIImage imageWithCGImage:tileImageRef]; 

     CGRect tileFrame = CGRectMake((tileWidth+TILE_SPACING)*x, (tileHeight+TILE_SPACING)*y, 
             tileWidth, tileHeight); 

     tileImageView = [[Tile alloc] initWithImage:tileImage]; 
     tileImageView.frame = tileFrame; 
     tileImageView.originalPosition = orgPosition; 
     tileImageView.currentPosition = orgPosition; 


     CGImageRelease(tileImageRef); 

     [tiles addObject:tileImageView]; 

     // now add to view 
     [self.view insertSubview:tileImageView atIndex:0]; 
    } 
} 

[self shuffle]; 

}

+0

你的問題是什麼?我看到了信息,但並不是說你在問什麼。 – WendiKidd 2012-07-07 00:16:25

+0

我怎樣才能將這組拼貼放置在屏幕的下方,比如100像素左右。 – Alex 2012-07-07 00:17:18

回答

0
int topBarWidth = 100;  //100 based on the example in your comment 

然後在你的代碼中你設置每個分片的框架來看,加topBarWidth到任何的y值您之前設置。

+0

topBarHeight將是一個更好的名稱,你會像這樣使用它: CGRect frame = CGRectMake(tileWidth * x,(tileHeight * y)+ topBarHeight,tileWidth,tileHeight); – brindy 2012-07-07 00:30:49

0

另一種選擇是爲您的瓷磚創建子視圖,然後重新定位。如果你想動畫所有的瓷磚上下移動,你只需要動畫瓷磚視圖,而不是重新計算所有圖像的位置。