2

何かをタップすると、タップした場所に小さな50X53の画像が表示されるゲームを作ろうとしています。まず、50x53の画像をボタンにして、ボタンをタッチするとボタンの背景が赤に変わります(画像がそこに追加されたことをシミュレートします)ここで、viewControllerに約48個のボタン(6X8)があり、これらのボタンの1つを押すと、背景が赤に変わります。 UIは完全に空白になり(ボタンは表示されません)、特定の領域を押すと、押した小さなスペースに画像がポップアップ表示されます。私はする代わりに考えました

-(IBAction)imagePressed
{
sender.background=[UIColor redColor];
}


-(IBAction)imagePressedAgain
{
sender.background=[UIColor whiteColor];
}

私は次のようなことをするつもりでした

-(IBAction)imagePressed
{
sender.image=@"whateverImage.png";
//I know this isn't the right syntax to change a buttons image (and i don't even know if i can change a buttons image) but anyways i was going to try to here
}
-(IBAction)imagePressedAgain
{
sender.image=@"imageThatIsJustAWhiteBackground";
}

だから私はこれをやってみて、それから自分自身に思いました、うーん、多分私はそれをこのようにするべきではないでしょう、私はたくさんの小さなUIImageViewsを使ってそれらすべてをinterfaceBuilderに置くべきですか?画面全体に48個のボタンを配置する代わりに、48個のUIImageViewを配置しますか?次に、それぞれにタップジェスチャレコグナイザーを追加すると思いますか?または、古い計画に固執してボタンの画像を変更する必要がありますか?

(o、そしてところで、ユーザーが画面上の指を触れた場所に正確に比例して画像を表示することはできません。整理されたグリッドに配置する必要があるため、48個の画像を表示する必要があります。)

4

3 に答える 3

5

説明したようにグリッドのような配置(6x8)を維持したい場合は、次のようなことを行うことができます。

 NSMutableArray *buttons = [[NSMutableArray alloc] init];

// Loop through and create 48 buttons at 50 x 53
for(int i = 0; i < 48; i++)
{
    UIButton *pressMe = [UIButton buttonWithType:UIButtonTypeCustom];

    [buttons addObject:pressMe];
    pressMe.frame = CGRectMake(0, 0, 50, 53);
    // Set background image for when button is selected
    [pressMe setBackgroundImage:[UIImage imageNamed:@"selectedImage.png"] forState:UIControlStateSelected];
    // Add target to toggle selected state of button
    [pressMe addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
}

int col = 0;
int row = 0;
int maxPerRow = 6;
float spacingHorizontal = 50;
float spacingVertical = 53;

for(UIButton *view in buttons)
{
    // position buttons in 6 x 8 grid
    view.frame = CGRectMake(col * spacingHorizontal, row * spacingVertical, view.frame.size.width, view.frame.size.height);
    [self.view addSubview:view];

    if(col % maxPerRow == maxPerRow-1)
    {
        col = 0;
        row++;
    }
    else
    {
        col++;
    }
}

重要なのは、ボタンに選択した画像を設定することです。これにより、ボタンをタップしたときにカスタム画像を表示できます。これが私のテストのスクリーンキャップです。必要に応じて、プロジェクトファイルを提供できます。

ここに画像の説明を入力してください

于 2012-07-11T18:34:12.597 に答える
1

編集:元の質問に次のものが含まれていることは明らかにわかりませんでした。

(o、そしてところで、ユーザーが画面上の指を触れた場所に正確に比例して画像を表示することはできません。整理されたグリッドに配置する必要があるため、48個の画像を表示する必要があります。)

そのため、私の答えは、実際には質問者が探していたものではありません。ezekielDFMはそれに非常によく答えました。とにかく投稿しますが、誰かがユーザーの任意のタッチの場所に画像を表示しようとしている場合に備えて。

元の回答

rdelmarが言ったように、これを設定する最良の方法はを使用することUITapGestureRecognizerです。ジェスチャレコグナイザは、ビュー上のタッチイベントをインターセプトし、それらに関する有用な情報を提供します。

UITapGestureRecognizerビューコントローラの実装では、最初に適切なものをビューにアタッチする必要があります。

- (id)init 
{
    UITapGestureRecognizer *tapRecognizer = 
    [[UITapGestureRecognizer alloc] initWithTarget:self 
                                            action:@selector(handleSingleTap:)];

    // The gesture recognizer requires only one finger to fire
    [tapRecognizer setNumberOfTouchesRequired:1];

    // Make recognizer cancel any touches that it recognizes, so they don't
    // "fall through" to the actual view
    [tapRecognizer setCancelsTouchesInView:YES];
    [[self view] addGestureRecognizer:tapRecognizer];
}

次に、handleSingleTap:同じ実装のどこかにそのセレクターを実装します。次のようになります。

- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
    // Get the location of the touch
    CGPoint touchPoint = [recognizer locationOfTouch:0 inView:[self view]];

    // Init a new image, or get an image in some other way
    UIImage *myImage = [UIImage imageNamed:@"test_image.png"];

    // Create a new imageView for that image... 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
    // ... and center it at the touch location
    [imageView setCenter:touchPoint];

    // Add the new UIImageView to your view's subviews
    [[self view] addSubview:imageView];
}

それはあなたが始めるはずです。より具体的なサポートが必要な場合はお知らせください。

于 2012-07-11T18:40:05.327 に答える
0

私は現在コンピューターの前にいないので、これをテストすることはできませんが、これを行う最も簡単な方法は、ビューにジェスチャ認識機能を追加することです。画像ビューやボタンは必要ありません。事前定義。ユーザーがビューをタップすると、イベントから場所を取得し、その情報を使用して、グリッド上にあるタッチポイントに最も近い場所に画像ビューを配置します。

于 2012-07-11T18:11:52.853 に答える