1

画面にプログラムで描かれたいくつかの円があります。次に、指のクリックのx座標とy座標の間の距離を、円のx座標とy座標のそれぞれで計算します。

円の半径のいずれよりも小さい距離は、クリックされた円です。とてもシンプルです。しかし、私はiveが多くの繰り返しコードを取得していることを発見し、コードをクリーンアップできると感じていますが、現時点でそれを行うための最良の方法がわかりません。

どんな助けでも大歓迎です。

float diffx = touch.x - bass.pos.x;
float diffy = touch.y - bass.pos.y;
float dist = sqrt(diffx*diffx + diffy*diffy);
if(dist < bass.radius){

    if(recordingInfo.isRecording){
        //do some stuff related to this button unique

    }
    //play some sound
}

diffx = touch.x - treble.pos.x;
diffy = touch.y - treble.pos.y;
dist = sqrt(diffx*diffx + diffy*diffy);

if(dist < treble.radius){
    if(recordingInfo.isRecording){
        //do something related to this button
    }
    //play some sound
}

diffx = touch.x - hihat.pos.x;
diffy = touch.y - hihat.pos.y;
dist = sqrt(diffx*diffx + diffy*diffy);

if(dist < hihat.radius){
    if(recordingInfo.isRecording){

        //do shayt related to this button

    }
    //play this sound
}

diffx = touch.x - bassTwo.pos.x;
diffy = touch.y - bassTwo.pos.y;
dist = sqrt(diffx*diffx + diffy*diffy);

if(dist < bassTwo.radius){
    if(recordingInfo.isRecording){
        //do some crap regarding this indivudal button

    }
    //play another sound
}

diffx = touch.x - kick.pos.x;
diffy = touch.y - kick.pos.y;
dist = sqrt(diffx*diffx + diffy*diffy);

if(dist < kick.radius){
    if(recordingInfo.isRecording){
      //do some funky stuff related to this button
    }
    //play some sound
}

diffx = touch.x - snare.pos.x;
diffy = touch.y - snare.pos.y;
dist = sqrt(diffx*diffx + diffy*diffy);

if(dist < snare.radius){
    if(recordingInfo.isRecording){
        //
    }
    //play some sound
}

diffx = touch.x - recordButton.pos.x;
diffy = touch.y - recordButton.pos.y;
dist = sqrt(diffx*diffx + diffy*diffy);

if(dist < recordButton.radius){
    //and do some funky stuff audio visual styff gere
}    

diffx = touch.x - play.pos.x;
diffy = touch.y - play.pos.y;
dist = sqrt(diffx*diffx + diffy*diffy);
     //code execution if this circle button is hit
}    

またはこれは大丈夫ですか?このコードをすべてtouchDownメソッドに配置します

4

2 に答える 2

1

繰り返されるコードは次のとおりです。

diffx = touch.x - recordButton.pos.x;
diffy = touch.y - recordButton.pos.y;
dist = sqrt(diffx*diffx + diffy*diffy);

コードが複数回表示されるときはいつでも、それを関数に入れることを検討する必要があります。

float distance(vec touch, vec button_center) {
    float diffx = touch.x - bass.pos.x;
    float diffy = touch.y - bass.pos.y;
    float dist = sqrt(diffx*diffx + diffy*diffy);
    return dist;
}

if(distance(touch,bass.pos) < bass.radius){
   ...
}
if(distance(touch, treble.pos) < treble.radius){
   ...
}
if(distance(touch,hihat.pos) < hihat.radius){
   ...
}

もちろん、ボタンが押されたかどうかを確認するために、そのチェックも繰り返します。

bool is_hit(Button b,vec touch) {
    return distance(b.pos,touch) < b.radius;
}

if(is_hit(bass,touch)) {}
if(is_hit(treble,touch)) {}
...

これはイベントを処理する非常に単純な方法であり、プログラムのアーキテクチャを変更せずに繰り返しをさらに減らすことは困難です。もう少し柔軟なものが必要な場合は、GUIフレームワークがイベントを処理する方法を調べてください。イベントに関するCocoaのドキュメントは、見るのに良い例かもしれません:Cocoaイベント処理ガイド

于 2012-04-23T03:00:31.947 に答える
1

まず、いくつかの簡単なユーティリティメソッドを追加します。

- (float) distanceBetweenTouch:(UITouch*)touch andPoint:(CGPoint)point {
    float diffx = touch.x - point.x;
    float diffy = touch.y - point.y;
    return sqrt(diffx*diffx + diffy*diffy);
}

- (NSArray*) tappedButtons:(NSArray*)buttons forTouch:(UITouch*)touch {
    NSMutableArray* result = [NSMutableArray array];

    for (SomeCustomButtonType* button in buttons) {
        if ([self distanceBetweenTouch:touch andPoint:button.pos] < button.radius) {
            [result addObject:button];
        }
    }

    return result;
}

次に、コードを修正して次のようにします。

NSArray* allButtons = [NSArray* arrayWithObjects:bass, treble, hihat, ..., nil];
NSArray* tappedButtons = [self tappedButtons:allButtons forTouch:touch];

for (SomeCustomButtonType* button in tappedButtons) {
    if (button == bass) {
        //handle tap on the 'bass' button
    }
    else if (button == treble) {
        //handle tap on the 'treble' button
    }
    //... (handlers for other buttons)
}

...またはさらに良いことに、コードを少し動かして、各ボタンがタップされたときに何をすべきかを認識できるようにすると、おそらくそのif/elseブロックとforループを次のように書き直すことができます。

[tappedButtons makeObjectsPerformSelector:@selector(handleTap)];

ただし、ボタンが部分的に重なっていない限り、1回のタップが複数のボタンとどのように交差するかはわかりません。

于 2012-04-23T03:00:58.870 に答える