2012-09-24 30 views
1

我正在尋找一些示例代碼來跟蹤CFDictionary的UITouches。不幸的是,蘋果文檔沒有一個完整的例子。使用CFDictionary跟蹤UITouches

http://developer.apple.com/library/IOs/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/MultitouchEvents/MultitouchEvents.html#//apple_ref/doc/uid/TP40009541-CH3-SW7

我目前使用NSMutable數組,但我想保存完整的UITouch對象,因此要獲得時間戳爲好。

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);  
    } 
} 
+0

從文檔:「你應該使用CFDictionaryRef類型,而不是一個NSDictionary對象;後者複製其鍵,但UITouch類不採用NSCopying協議,這是需要的對象拷貝。」 – RParadox

回答

1

UITouch的指針在觸摸的生命週期內不會改變。你可以從指針創建一個NSValue。

// NSMutableDictionary * touchLookup; set up somewhere else... 
NSValue * key = [NSValue valueWithPointer:touch]; 
[touchLookup setObject:touch forKey:key];