Iphone 检测多个按钮被单击的最佳方法是什么

Iphone 检测多个按钮被单击的最佳方法是什么,iphone,c++,openframeworks,Iphone,C++,Openframeworks,我在屏幕上以编程方式画了几个圆。然后我计算出手指点击的x和y坐标与每个圆的x和y坐标之间的距离 任何距离小于任何圆半径的都是单击的圆。真的很简单。然而,我发现我有很多重复的代码,我觉得我可以清理代码,但我不确定什么是目前最好的方法 感谢您的帮助 float diffx = touch.x - bass.pos.x; float diffy = touch.y - bass.pos.y; float dist = sqrt(diffx*diffx + diffy*diffy); if(dist &

我在屏幕上以编程方式画了几个圆。然后我计算出手指点击的x和y坐标与每个圆的x和y坐标之间的距离

任何距离小于任何圆半径的都是单击的圆。真的很简单。然而,我发现我有很多重复的代码,我觉得我可以清理代码,但我不确定什么是目前最好的方法

感谢您的帮助

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
}    
float diffx=touch.x-bass.pos.x;
float diffy=touch.y-bass.pos.y;
浮动距离=sqrt(diffx*diffx+diffy*diffy);
中频(距离<低音半径){
if(记录信息记录){
//做一些与这个按钮相关的事情
}
//播放一些声音
}
diffx=触摸x-高音位置x;
diffy=触摸y-三倍位置y;
dist=sqrt(diffx*diffx+diffy*diffy);
if(距离<三倍半径){
if(记录信息记录){
//执行与此按钮相关的操作
}
//播放一些声音
}
diffx=touch.x-hihat.pos.x;
diffy=touch.y-hihat.pos.y;
dist=sqrt(diffx*diffx+diffy*diffy);
if(距离<半径){
if(记录信息记录){
//shayt和这个按钮有关吗
}
//播放这个声音
}
diffx=touch.x-bass2.pos.x;
diffy=touch.y-bass2.pos.y;
dist=sqrt(diffx*diffx+diffy*diffy);
if(距离小于2.半径){
if(记录信息记录){
//对这个独立按钮做些废话
}
//播放另一个声音
}
diffx=touch.x-kick.pos.x;
diffy=触碰y-踢位y;
dist=sqrt(diffx*diffx+diffy*diffy);
if(距离<井涌半径){
if(记录信息记录){
//做一些与这个按钮有关的有趣的事情
}
//播放一些声音
}
diffx=touch.x-陷阱位置x;
diffy=touch.y-陷阱位置y;
dist=sqrt(diffx*diffx+diffy*diffy);
if(距离<陷阱半径){
if(记录信息记录){
//
}
//播放一些声音
}
diffx=touch.x-recordButton.pos.x;
diffy=touch.y-recordButton.pos.y;
dist=sqrt(diffx*diffx+diffy*diffy);
if(距离<记录按钮半径){
//做一些有意思的东西,视听的
}    
diffx=touch.x-play.pos.x;
diffy=touch.y-play.pos.y;
dist=sqrt(diffx*diffx+diffy*diffy);
//如果按下此圆圈按钮,则执行代码
}    
还是这样好?我将所有这些代码放在着陆方法中

重复的代码是:

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){
   ...
}
浮动距离(vec触摸,vec按钮\中心){
float diffx=touch.x-bass.pos.x;
float diffy=touch.y-bass.pos.y;
浮动距离=sqrt(diffx*diffx+diffy*diffy);
返回距离;
}
中频(距离(触摸,低音位置)<低音半径){
...
}
if(距离(触摸,高音位置)<高音半径){
...
}
if(距离(触摸,hihat.pos)
当然,您也在重复该检查,以查看是否按下了按钮:

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

if(is_hit(bass,touch)) {}
if(is_hit(treble,touch)) {}
...
bool被点击(按钮b,vec触摸){
返回距离(b位置,触摸)

这是一种非常简单的事件处理方法,如果不改变程序的体系结构,很难进一步减少重复。如果您想要更灵活一点的东西,您可能需要研究GUI框架如何处理事件。有关事件的Cocoa文档可能是一个很好的示例:

首先,添加一些简单的实用程序方法:

- (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;
}
…或者更好的是,如果您将代码移动一点,以便每个按钮在点击时都知道它应该做什么,您可能可以重写
if/else
块和
for
循环,如:

[tappedButtons makeObjectsPerformSelector:@selector(handleTap)];

尽管如此,我不确定一次点击将如何与多个按钮相交,除非你的按钮部分重叠。

或者?请将“按钮”放入一个NSArray,iterate Itrol中间黑客我只是奇怪地看到了你的另一个答案!这是当前的C++。
[tappedButtons makeObjectsPerformSelector:@selector(handleTap)];