我需要支持四个手指同时平移而不会使用Objective-C相互干扰.
我已经搜索了一段时间的洗液,但他们显示的答案对我不起作用.任何帮助,将不胜感激.
解决方法 我在相当长的一段时间内遇到了同样的问题并最终解决了它.以下是我的DrawVIEw.m中的代码,它是UIVIEw的子类,能够使用drawRect:支持绘图.#import "DrawVIEw.h"#define MAX_touches 4@interface DrawVIEw() { bool touchInRect[MAX_touches]; CGRect rects[MAX_touches]; UItouch *savedtouches[MAX_touches];}@end@implementation DrawVIEw-(ID)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if (self) { // Initialization code self.multipletouchEnabled = YES; for (int i=0; i<MAX_touches; i++) { rects[i] = CGRectMake(200,200,50,50); savedtouches[i] = NulL; touchInRect[i] = false; } } return self;}- (voID)drawRect:(CGRect)rect { // Drawing code [[UIcolor bluecolor] set]; CGContextRef context = UIGraphicsGetCurrentContext(); for (int i=0; i<MAX_touches; i++) { CGContextFillRect(context,rects[i]); CGContextstrokePath(context); }}#pragma mark - Handle touches- (voID)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSArray *alltouches = [touches allObjects]; for (int i=0; i<[alltouches count]; i++) { UItouch *touch = alltouches[i]; CGPoint newPoint = [touch locationInVIEw:self]; for (int j=0; j<MAX_touches; j++) { if (CGRectContainsPoint(rects[j],newPoint) && !touchInRect[j]) { touchInRect[j] = true; savedtouches[j] = touch; break; } } }}- (voID)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { NSArray *alltouches = [touches allObjects]; for (int i=0; i<[alltouches count]; i++) { UItouch *touch = alltouches[i]; CGPoint newPoint = [touch locationInVIEw:self]; for (int j=0; j<MAX_touches; j++) { if (touch == savedtouches[j]) { rects[j] = [self rectWithSize:rects[j].size andCenter:newPoint]; [self setNeedsdisplay]; break; } } }}- (voID)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { NSArray *alltouches = [touches allObjects]; for (int i=0; i<[alltouches count]; i++) { UItouch *touch = alltouches[i]; for (int j=0; j<MAX_touches; j++) { if (touch == savedtouches[j]) { touchInRect[j] = false; savedtouches[j] = NulL; break; } } }}- (CGRect)rectWithSize:(CGSize)size andCenter:(CGPoint)point { return CGRectMake(point.x - size.wIDth/2,point.y - size.height/2,size.wIDth,size.height);}@end
我将MAX_touches设置为4,因此屏幕上会有四个对象.这个的基本概念是在调用touchesBegan ::时将每个UItouch ID存储在savedtouches数组中,然后在touchesMoved :: called时将每个ID与屏幕上的触摸进行比较.
只需将代码粘贴到.m文件中即可.示例结果如下所示:
希望这可以帮助 :)
总结以上是内存溢出为你收集整理的ios – 如何在Xcode中跟踪多个触摸全部内容,希望文章能够帮你解决ios – 如何在Xcode中跟踪多个触摸所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)