2010-12-06 36 views

回答

3

您需要在应用程序中实现手势识别器。

在你的界面:

#define kMinimumGestureLength 30 
#define kMaximumVariance 5 
#import <UIKit/UIKit.h> 
@interface *yourView* : UIViewController { 
    CGPoint gestureStartPoint; 
} 
@end 

kMinimumGestureLength是最小距离为旅行前,他会成为一个轻扫手指。 kMaximumVariance是手指可以在y轴上的起始点之上结束的最大距离(以像素为单位)。

现在打开你的界面.xib文件,并选择在IB视图,并确保Multiple TouchView Attributes.

在您的实现启用,实现这些方法。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
     UITouch *touch = [touches anyObject]; 
     gestureStartPoint = [touch locationInView:self.view]; 
    } 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentPosition = [touch locationInView:self.view];  

    CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x); 
    CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y); 


    if(deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance){ 
     //do something 
} 
else if(deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance){ 
     //do something 
    } 
} 

这是一种实现滑动识别器的方法。此外,你真的应该看看文档关于这一主题:

UISwipeGestureRecognizer

+0

你没有解释kMinimumVariance。 – Moshe 2010-12-06 18:24:51

3

UIGestureRecognizer是你想要的。特别是UISwipeGestureRecognizer子类