c++ - 处理 iOS 触摸以模拟 C++ 输入库的鼠标

标签 c++ objective-c ios

我有现有的跨平台 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/

相关文章:

c++ - 在 Flutter 应用程序中使用来自 Android NDK 的 AssetManager 类

ios - 在 AVAssetReader 中获取 Exc_Bad_access

ios - UIViewController 没有带有 UIModalPresentationFormSheet 的parentViewController?

ios - 有什么方法可以检查应用程序是否已安装或更新?

iphone - 使用 iOS SDK 和完整的 Cocoa Touch/Objective-C 代码确定用户的设备

C++ 部分模板特化,需要语句 : error: out-of-line definition of 'foo' from class Bar<T> without definition

c++ - 强制使通用模板失败,但允许特化,加上 "method exists dispatching"

c# - 为什么不直接使用类字段呢?

iOS 将数组添加到 Plist 文档根目录

objective-c - UINavigationBar 和新的 iOS 5+ 外观 API - 如何提供两个背景图像?