Ios 使用CFDictionary跟踪UITouches
我正在寻找一些示例代码,用CFDictionary跟踪UITouches。不幸的是,苹果文档没有一个完整的例子 我目前正在使用NSMutable数组,但我想存储完整的UITouch对象,以便获得时间戳Ios 使用CFDictionary跟踪UITouches,ios,Ios,我正在寻找一些示例代码,用CFDictionary跟踪UITouches。不幸的是,苹果文档没有一个完整的例子 我目前正在使用NSMutable数组,但我想存储完整的UITouch对象,以便获得时间戳 NSMutableArray *touchArray_val; -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ touchArray_val = [[NSMutableArray alloc] init
NSMutableArray *touchArray_val;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
touchArray_val = [[NSMutableArray alloc] init];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
for(UITouch *t in touches){
CGPoint currentPoint = [t locationInView:self];
[touchArray_val addObject:[NSValue valueWithCGPoint: currentPoint]];
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"%i",[touchArray count]);
for(NSValue *val in touchArray_val){
NSLog(@"%@",val);
}
}
UITouch的指针在触摸的使用寿命内不会改变。您可以从指针创建NSValue
// NSMutableDictionary * touchLookup; set up somewhere else...
NSValue * key = [NSValue valueWithPointer:touch];
[touchLookup setObject:touch forKey:key];
UITouch的指针在触摸的使用寿命内不会改变。您可以从指针创建NSValue
// NSMutableDictionary * touchLookup; set up somewhere else...
NSValue * key = [NSValue valueWithPointer:touch];
[touchLookup setObject:touch forKey:key];
文档中:“您应该使用CFDictionaryRef类型,而不是NSDictionary对象;后者复制其密钥,但UITouch类不采用对象复制所需的NSCopying协议。”文档中:“您应该使用CFDictionaryRef类型,而不是NSDictionary对象;后者复制其密钥,但UITouch类不采用对象复制所需的NSCopying协议。”