iPhone 简单手势的判断
不知道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 Swipeif (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 Swipeif (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 }}