若在真机设备构建和运行应用程序,就会发现虽然在屏幕上按下了两根手指,但第二个触点却被忽略了。这是因为,默认情况下View是忽略多触点的,必须专门为需要支持多触点的View开启此功能。若要开启多触点,需要编写代码修改根视图的multipleToucheEnabled属性。
self.view.multipleTouchEnabled = TRUE;
在触摸的生命周期里,每个UITouch对象都为同一个实例化对象,也就是说屏幕每个独立触摸全程都表示各为同一UITouch对象。在触摸方法添加代码如下。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event //首次在屏幕上检测到触摸时调用
{
NSLog(@"touchesBegan");
for (UITouch *touch in touches)
{
NSLog(@" - %p",touch);
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event //如果触摸移动到了新的位置则会调用此方法
{
NSLog(@"touchesMoved");
for (UITouch *touch in touches)
{
NSLog(@" - %p",touch);
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event//当触摸离开屏幕调用此方法
{
NSLog(@"touchesEnded");
for (UITouch *touch in touches)
{
NSLog(@" - %p",touch);
}
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event//如系统决定取消此次触摸,那可能就不调用touchesEnded方法了,在这种情况下会调用touchesCancelled方法
{
NSLog(@"touchesCancelled");
for (UITouch *touch in touches)
{
NSLog(@" - %p",touch);
}
}
增加一段代码作用是打印出包含在集合中每个UITouch对象的内存地址。下面为设备屏幕按下两根手指的输出结果
2014-01-14 23:52:56.590 bbsTouch[3671:907] touchesBegan
2014-01-14 23:52:56.606 bbsTouch[3671:907] - 0x1ddaac00
2014-01-14 23:52:56.623 bbsTouch[3671:907] touchesBegan
2014-01-14 23:52:56.628 bbsTouch[3671:907] - 0x1dd9b690
2014-01-14 23:52:56.633 bbsTouch[3671:907] touchesMoved
2014-01-14 23:52:56.637 bbsTouch[3671:907] - 0x1ddaac00
2014-01-14 23:52:56.895 bbsTouch[3671:907] touchesMoved
2014-01-14 23:52:56.902 bbsTouch[3671:907] - 0x1dd9b690
2014-01-14 23:52:56.904 bbsTouch[3671:907] - 0x1ddaac00
2014-01-14 23:52:56.958 bbsTouch[3671:907] touchesEnded
2014-01-14 23:52:56.963 bbsTouch[3671:907] - 0x1dd9b690
2014-01-14 23:52:56.967 bbsTouch[3671:907] - 0x1ddaac00
两根手指先后触摸屏幕,进入touchesBegan,内存分为0x1ddaac00,0x1dd9b690触摸过程完成后,内存地址也能被系统回收。
转载请注明原著:http://blog.csdn.net/marvindev