我有现有的跨平台 C++ 代码来处理鼠标输入,方法如下:
onMouseDown(x,y,button)
onMouseUp(x,y,button)
onMouseMove(x,y,buttons)
我正在将功能移植到 C++ iOS 应用程序中……根据需要使用最少的 Objective-C。我想处理单点触摸手势来模拟鼠标功能,这样我就可以将参数传递给现有方法(以及添加多点触摸)。
执行此操作的代码应该是什么样的,理想情况下作为最小示例应用程序 - 主要是 OBJ-C/C++ 交互真正让我感到困惑?
最佳答案
下面是我如何将多点触控传递给 OpenGL 程序的 C++ 代码(使用 1 或 2 个手指):
// Set this in your ViewController's init code
[self setMultipleTouchEnabled:YES];
// Implement these in ViewController
int touchCount = 0;
UITouch* touch[2];
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{
NSArray* array = [touches allObjects];
UITouch* touchTemp;
int t;
int touchedPixel[2];
for(int i = 0; i < [array count]; ++i){
touchTemp = [array objectAtIndex:i];
if(touchCount >= 2)
return;
if(touch[0] == NULL)
t = 0;
else
t = 1;
touch[t] = touchTemp;
CGPoint touchLoc = [touch[t] locationInView:(EAGLView *)self.view];
++touchCount;
touchedPixel[X] = touchLoc.x;
touchedPixel[Y] = touchLoc.y;
mainScreen->push(t, touchedPixel); // mainScreen is a C++ object for GL drawing.
}
}
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event{
NSArray* array = [touches allObjects];
UITouch* touchTemp;
int t;
int touchedPixel[2];
for(int i = 0; i < [array count]; ++i){
touchTemp = [array objectAtIndex:i];
for(t = 0; t < 2; ++t){ // Find the matching touch in the array
if(touchTemp == touch[t])
break;
}
if(t == 2) // Return if touch was not found
continue;
CGPoint touchLoc = [touch[t] locationInView:(EAGLView *)self.view];
touchedPixel[X] = touchLoc.x;
touchedPixel[Y] = touchLoc.y;
mainScreen->move(t, touchedPixel);
}
}
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event{
NSArray* array = [touches allObjects];
UITouch* touchTemp;
int t;
int touchedPixel[2];
for(int i = 0; i < [array count]; ++i){
touchTemp = [array objectAtIndex:i];
for(t = 0; t < 2; ++t){ // Find the matching touch in the array
if(touchTemp == touch[t])
break;
}
if(t == 2) // Return if touch was not found
continue;
CGPoint touchLoc = [touch[t] locationInView:(EAGLView *)self.view];
touch[t] = NULL;
--touchCount;
touchedPixel[X] = touchLoc.x;
touchedPixel[Y] = touchLoc.y;
mainScreen->release(t, touchedPixel);
}
}
- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event{
printf("Touch cancelled\n");
[self touchesEnded:touches withEvent: event];
}
关于c++ - 处理 iOS 触摸以模拟 C++ 输入库的鼠标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13798944/