iPhone 简单手势的判断

本文介绍了一种在iOS早期版本中实现滑动(swipe)手势识别的方法。通过自定义`touchesMoved:`及`touchesEnded:`方法,实现了水平或垂直滑动的判定。当滑动距离超过设定阈值且偏离方向不超过允许的最大偏移时,视为滑动手势有效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

不知道4.0SDK带有手势的直接支持没有,至少3.2已经可以用了.但是如果想支持早期的版本,那么手势的识别无疑是一种痛苦,因为需要自己写代码来判定手势...

 

下面代码是判断一个滑动的手势(swipe),虽然很简单但是总体思想就是这样了.当在一个水平,或者纵向滑动时给出一个滑动距离以及偏移量.当实际滑动距离超过指定的距离,且水平或者纵向的偏移量小于指定的偏移量则视为这个滑动手势判定成功!

 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
	UITouch *touch = touches.anyObject; 
	CGPoint currentTouchPosition = [touch locationInView:self]; 
	
	if (fabsf(startTouchPosition.x - currentTouchPosition.x) >= 
		HORIZ_SWIPE_DRAG_MIN && 
		fabsf(startTouchPosition.y - currentTouchPosition.y) <= 
		VERT_SWIPE_DRAG_MAX) 
	{ 
		// Horizontal Swipe
		if (startTouchPosition.x < currentTouchPosition.x) {
			NSLog(@"from left");
			dirString = kCATransitionFromLeft;
		}
		else 
			NSLog(@"from right");
			dirString = kCATransitionFromRight;
	} 
	else if (fabsf(startTouchPosition.y - currentTouchPosition.y) >= 
			 HORIZ_SWIPE_DRAG_MIN && 
			 fabsf(startTouchPosition.x - currentTouchPosition.x) <= 
			 VERT_SWIPE_DRAG_MAX)
	{ 
		// Vertical Swipe
		if (startTouchPosition.y < currentTouchPosition.y) 
			dirString = kCATransitionFromBottom;
		else 
			dirString = kCATransitionFromTop;
	} else 
	{
		// Process a non-swipe event. 
		// dirString = NULL;
	}
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
	if (dirString) 
	{
		// do it 	
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值