这适用于单点触控和多点触控用例。我编写了一个存根 c++ 库oldlib
,名为我在下面的 obj-c 文件中使用。
你可以通过任何你需要的作为button
. 我的代码通过了触摸 ID。我想你需要检查你的图书馆对那里的期望并相应地改变它。
这是我的代码:
老库
#ifndef Quick_oldlib_h
#define Quick_oldlib_h
void onMouseDown(int x,int y,int button);
void onMouseUp(int x,int y,int button);
void onMouseMove(int x,int y,int buttons);
#endif
旧的lib.cpp
#include "oldlib.h"
#include <stdio.h>
void onMouseDown(int x,int y,int button)
{
printf("onMouseDown:%d,%d button:%d\n", x, y, button);
}
void onMouseUp(int x,int y,int button)
{
printf("onMouseUp:%d,%d button:%d\n", x, y, button);
}
void onMouseMove(int x,int y,int button)
{
printf("onMouseMove:%d,%d button:%d\n", x, y, button);
}
QViewController.h
#import <UIKit/UIKit.h>
@interface QViewController : UIViewController
@end
QViewController.mm <- 注意 MM 扩展,它可以与 C++ 头文件一起编译
#import "QViewController.h"
#include "oldlib.h"
@interface QViewController ()
@property (nonatomic, retain) NSMutableSet* active_touches;
@end
@implementation QViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.active_touches = [[NSMutableSet alloc] init];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch* touch in touches) {
[self.active_touches addObject:touch];
onMouseDown([touch locationInView:self.view].x, [touch locationInView:self.view].y, (int)(__bridge void*)touch);
}
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch* touch in touches) {
if ([self.active_touches containsObject:touch]) {
onMouseUp([touch locationInView:self.view].x, [touch locationInView:self.view].y, (int)(__bridge void*)touch);
[self.active_touches removeObject:touch];
}
}
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch* touch in touches) {
if ([self.active_touches containsObject:touch]) {
onMouseMove([touch locationInView:self.view].x, [touch locationInView:self.view].y, (int)(__bridge void*)touch);
}
}
}
- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch* touch in touches) {
[self.active_touches removeObject:touch];
NSLog(@"cancelled: %@", touch);
}
}
@end