これは、シングルタッチとマルチタッチの両方のユース ケースで機能します。oldlib
以下の obj-c ファイルで使用しているスタブ c++ libを作成しました。
必要なものは何でも として渡すことができますbutton
。私のコードはタッチ ID を渡します。ライブラリがそこで何を期待しているかを確認し、それに応じて変更する必要があると思います。
これが私のコードです:
oldlib.h
#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
oldlib.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 <-これをC++ヘッダーと一緒にコンパイルできるようにするMM拡張に注意してください
#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