定位多个UIImage - 目标C.

时间:2012-07-07 00:15:30

标签: objective-c ios xcode uiimage

在我正在构建的应用程序中,导入方形图像,将其分成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];

}

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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