使用CFDictionary跟踪UITouches

时间:2012-09-24 18:45:39

标签: ios

我正在寻找一些示例代码来跟踪带有CFDictionary的UITouches。不幸的是,apple-docs没有完整的例子。

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

1 个答案:

答案 0 :(得分:1)

UITouch的指针在触摸的生命周期内不会改变。您可以从指针创建NSValue。

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