我们通过以下的代码示例来展示触摸处理程序。
1) 单击、双击处理
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//Get all the touches.
NSSet *allTouches = [event allTouches];
//Number of touches on the screen
switch ([allTouches count])
{
}
}
2) 两个指头的分开、合拢手势。
计算两个点之间的距离函数。
- (CGFloat)distanceBetweenTwoPoints
{
float x = toPoint.x - fromPoint.x;
}
记录多触点之间的初始距离。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
{
UITouch *touch2 = [[allTouches allObjects] objectAtIndex:1];
initialDistance = [self distanceBetweenTwoPoints
break;
}
两个指头移动时,判断是分开还是合拢。
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSSet *allTouches = [event allTouches];
switch ([allTouches count])
{
case 1:
break;
case 2:
{
UITouch *touch1 = [[allTouches allObjects] objectAtIndex:0];
UITouch *touch2 = [[allTouches allObjects] objectAtIndex:1];
//Calculate the distance between the two fingers.
CGFloat finalDistance = [self
//Check if zoom in or zoom out.
if(initialDistance > finalDistance) {
else {
}
} break;
}
}
如果您对本文感兴趣,可以到这里下载完整PDF。示例源代码下载。