1

私はゲームに取り組んでいますが、ボタンの色によって境界線の太さが変わるのです。この問題は、アプリに背景画像を配置するまで発生しなかったため、原因がわかりません。私が何を意味するかを示すために、いくつかのスクリーンショットを撮りました。何らかの理由で、境界線が表示されているボタンが青色の場合、境界線の太さが1pxではなく2pxに変更されます。

さらに、コントロールボタンの境界線にいくつかの問題があります。コントロールボタンとは、赤、青、茶色、緑、紫、黄色の順で上にある6つのボタンを意味します。

スクリーンショットは次のとおりです。

スクリーンショット#1スクリーンショット#2

x-1、y-1から始まり、幅+2、高さ+2のボタンを背景に配置して境界線を作成します。これがあなたにアイデアを与えるためのコードの一部です:

 UIButton *borderButton = [UIButton buttonWithType:UIButtonTypeCustom];
 borderButton.frame = CGRectMake((controlX-1), (controlY-1), 42, 32);
 borderButton.backgroundColor = [UIColor blackColor];
 [self.view addSubview:borderButton];

 UIButton *controlButton = [UIButton buttonWithType:UIButtonTypeCustom];
 [controlButton addTarget:self
                      action:@selector(doMove:)
            forControlEvents:UIControlEventTouchDown];
 controlButton.frame = CGRectMake(controlX, controlY, 40, 30);
 controlButton.tag = 500 + i;
 tmpColor = [self.colors objectAtIndex:i];
 controlButton.backgroundColor = tmpColor;
 [self.view addSubview:controlButton];

このコードはループから取得され、controlXとcontrolYは、コントロールボタンを開始する場所のx/y値を示します。

誰かがこれを修正する方法を知っているなら、私はそれを大いに感謝します。境界線を間違った方法で処理していて、同じ外観を実現するためのより簡単で問題の少ない方法がある場合は、変更するコードがあまりないので、それも喜んで行います。この苛立たしいUIの不具合を解決するのを手伝ってくれる人に事前に感謝します。

4

1 に答える 1

1

境界線付きのカスタムボタンを実装する最も簡単な方法は、ボタンのレイヤーを使用することです。

#import <QuartzCore/QuartzCore.h>
...
UIButton *controlButton = [UIButton buttonWithType:UIButtonTypeCustom];
 [controlButton addTarget:self
                      action:@selector(doMove:)
            forControlEvents:UIControlEventTouchDown];
 controlButton.frame = CGRectMake(controlX, controlY, 40, 30);
 controlButton.tag = 500 + i;

 // Set layer properties
 controlButton.layer.borderWidth = 1.0;
 controlButton.layer.borderColor = [UIColor blackColor].CGColor;
 controlButton.layer.cornerRadius = 5.0; // if you want rounded corners...

 tmpColor = [self.colors objectAtIndex:i];
 controlButton.backgroundColor = tmpColor;
 [self.view addSubview:controlButton];

また、画像がぼやけないように、ボタンやその他の要素(ラベルなど)が整数座標に配置されていることを確認する必要がある場合があります。座標を修正するには、CGRectIntegralを使用します

PS複雑なアニメーション/ゲームロジックの場合は、Cocos2Dなどのフレームワークを使用することをお勧めします

于 2012-12-10T07:56:49.020 に答える