0

画像 (この場合は「goodCell.png」) を描画するたびに、ビュー コントローラーのウィンドウの特定の xy 座標で、適切なパラメーターを含む次のメソッドを呼び出します。

-(void)paintGoodCellatX:(int)xAxis andY:(int)yAxis onViewController:(UIViewController*)playingViewController
{
    int x = 32*(xAxis - 1);
    int y = 384 - (32* yAxis);

    UIImage* myImage = [UIImage imageNamed:@"goodCell.png"];
    UIImageView* myImageView = [[UIImageView alloc] initWithImage:myImage];
    myImageView.frame = CGRectMake(x,y, 32, 32);
    [playingViewController.view addSubview:myImageView];

}

コード中のある時点で、特定の x & y 座標で描画された上記の画像の 1 つを削除したい場合、どうすればよいですか?

このコードを考えると、座標を除いて、描画した ImageView を保持するものは何もありません (したがって[myImageView removeFromSuperView];、名前が何も定義していないため、次のようなものは使用できません)。myImageViewView Controllerウィンドウの特定のxy座標でUIImageViewを削除/削除する方法、またはこの問題を回避する他の方法はありますか?

ありがとうございました

4

2 に答える 2

2

ビューに TAG を追加して、意味のある識別子を提供します。myImageView.tag = XXX; を使用します。

次に、[playingViewController viewWithTag:XXX] を使用して、削除するビューの UIImageView ハンドルを取得します。

タグは整数です。これらの X/Y 位置を格納する NSArray へのオフセット、またはこれらの値を格納する NSDictionary へのキーでしょうか?

于 2012-08-23T12:26:58.457 に答える
1

すべてのビューに、ポイントによって計算されるタグを付けることができます。たとえば、タグとして 3 桁の x よりも 3 桁の y 軸を指定できます。

x=312 y=567 の場合、

myImageView.tag = 312567;

これにより、常にビューを識別できます

別のより良い可能性は、追加されたすべてを NSArray に格納することです。特定のビューを 1 つ削除する場合は、配列を反復処理して、ポイントがビューの境界内にあるかどうかを確認する必要があります。多かれ少なかれ:

創造のために;

NSMutableArray *imageViewArray = [[NSMutableArray alloc] init];
-(void)paintGoodCellatX:(int)xAxis andY:(int)yAxis onViewController:(UIViewController*)playingViewController
{
    int x = 32*(xAxis - 1);
    int y = 384 - (32* yAxis);

    UIImage* myImage = [UIImage imageNamed:@"goodCell.png"];
    UIImageView* myImageView = [[UIImageView alloc] initWithImage:myImage];
    myImageView.frame = CGRectMake(x,y, 32, 32);
    [playingViewController.view addSubview:myImageView];
    [imageViewArray add:myImageView];
}

削除の場合:

for(UIImageView *mv in imageViewArray) {
 if(CGRectContainsPoint(mv.bounds, yourCGPoint)) 
  [mv removeFromSuperview];
}
于 2012-08-23T12:30:42.990 に答える