0

在我正在构建的应用程序中,导入了一个方形图像,它被分成 16 个图块,这些图块被放置在屏幕上的一个正方形中,从顶部开始。但是,我在磁贴上方的顶部添加了一个额外的信息栏,因此我需要将这组磁贴移到屏幕下方。我玩过 for 循环和代码生成的坐标,但没有什么能给我想要的尊重。

-(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];

}

4

2 回答 2

0

另一种选择是为您的图块创建一个子视图,然后重新定位它。如果您想为所有上下移动的图块设置动画,您只需为图块视图设置动画,而不是重新计算所有图像的位置。

于 2012-07-07T00:33:31.050 回答
0
int topBarWidth = 100;     //100 based on the example in your comment

然后在代码中设置每个图块的框架的位置,添加topBarWidth到您之前设置的任何 y 值。

于 2012-07-07T00:18:41.550 回答