2011-04-14 139 views
4

我想知道是否有一种简单的方法来设置最小滑动长度,即用户需要滑动的像素长度,以便将手势识别为滑动。iPhone:设置所需的滑动手势长度

我注意到,正常的刷卡可能非常没有响应(例如与在照片库中刷新照片相比)。

这是正常的方式,但我想,以减少刷卡所需的长度:

- (void)viewDidLoad 
    { 


    // SWIPING GESTURES: 

    UISwipeGestureRecognizer *swipeLeftRecognizer; 
    swipeLeftRecognizer=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(foundLeftSwipe:)]; 
    swipeLeftRecognizer.direction=UISwipeGestureRecognizerDirectionLeft; 
    //swipeRecognizer.numberOfTouchesRequired=1; 
    [self.view addGestureRecognizer:swipeLeftRecognizer]; 
    [swipeLeftRecognizer release]; 

    UISwipeGestureRecognizer *swipeRightRecognizer; 
    swipeRightRecognizer=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(foundRightSwipe:)]; 
    swipeRightRecognizer.direction=UISwipeGestureRecognizerDirectionRight; 
    //swipeRecognizer.numberOfTouchesRequired=1; 
    [self.view addGestureRecognizer:swipeRightRecognizer]; 
    [swipeRightRecognizer release]; 


    [super viewDidLoad]; 
} 

#pragma mark - 
#pragma mark Swipes 

- (void)foundLeftSwipe:(UISwipeGestureRecognizer *)recognizer { 
    // do something 
} 

- (void)foundRightSwipe:(UISwipeGestureRecognizer *)recognizer { 
    // do something 
    } 

我记得有一种方式来获得像素起始位置和结束位置,然后比较两个,但只是想知道是否有一个更简单的方法,即通过简单地定义我在这里的代码中所需的最小滑动长度的值。


编辑:

这是我如何重新编码整个事情:

- (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 deltaXX = (gestureStartPoint.x - currentPosition.x); // positive = left, negative = right 
    CGFloat deltaYY = (gestureStartPoint.y - currentPosition.y); // positive = up, negative = down 

    CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x); // will always be positive 
    CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y); // will always be positive 

    if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance) { 
     if (deltaXX > 0) { 
       label.text = @"Horizontal Left swipe detected"; 
       [self performSelector:@selector(eraseText) withObject:nil afterDelay:2]; 
      } 
     else { 
       label.text = @"Horizontal Right swipe detected"; 
       [self performSelector:@selector(eraseText) withObject:nil afterDelay:2]; 
      } 

    } 


    if (deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance) { 
     if (deltaYY > 0) { 
      label.text = @"Vertical up swipe detected"; 
      [self performSelector:@selector(eraseText) withObject:nil afterDelay:2]; 
      } 
     else { 
      label.text = @"Vertical down swipe detected"; 
      [self performSelector:@selector(eraseText) withObject:nil afterDelay:2]; 
     } 


    } 



} 

回答

5

请参阅本文档http://developer.apple.com/library/ios/#documentation/uikit/reference/UIResponder_Class/Reference/Reference.html

你可以使用touchesBegan,touchesMoved和touc hesEnded方法来查找滑动的开始/结束位置。通过查找开始和结束之间的增量,您可以获得滑动长度。

也许是这样的。

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

} 

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

    // compare gestureStartPoint and gestureEndPoint to determine swipe length 
} 
5

我正面临完全相同的事情,我一直在寻找解决方案。没有这样的东西所需的距离UISwipeGestureRecognizer但这并不是你可以使用的所有。我结束了使用UIPanGestureRecognizer!它看起来像一个超级类型的滑动手势识别器,它肯定会暴露更多有用的数据。

有在这个类叫做translationInView:(UIView *)aView的回报CGPoint其中X & Ÿ是在整个移动手势的总距离可用的方法!对我们的要求非常有用的东西!它由正负值尊重的方向,意思是:Y = + 100 - >向上滑动即可下跌100点,X = + 100刷卡左至右100分,等...

什么我在做的是检查用户是否已经平移了一定数量的点(我正在寻找类似向下滑动的东西,所以对我来说这是y> 150)然后做我的事情...

这里是一些代码摘录:

-(void)viewDidLoad 
    { 
    UIPanGestureRecoginzer *panDown=[[UIPanGestureRecoginzer alloc] initWithTarget:self action:@selector(swipedDown:)]; 
    [self.view addGestureRecognizer:panDown]; 
    } 
    . 
    . 
    . 
    -(void)swipedDown:(UIPanGestureRecoginzer *)recognizer 
    { 
    CGPoint panned=[recognizer translationInView:self.view]; 
    if(panned.y>150){ 
    //swiped down for 150 points 
     doSomething(); 
     } 
    } 

它也给你的平移速度:velocityInView:

注意:你应该使用你的superview(自我。查看大多数情况下)如果你想有一个整体泛的效果。

希望这有助于:UIPanGestureRecognize Class Reference

0

斯威夫特3:

var gestureStartPoint: CGPoint = .zero 

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    let touch = touches.first 

    gestureStartPoint = touch!.location(in: self.view) 
} 

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    let touch = touches.first 
    let gestureEndPoint = touch!.location(in: self.view) 

    let dist = distance(gestureStartPoint, gestureEndPoint) 
    print(dist) 
} 

func distance(_ a: CGPoint, _ b: CGPoint) -> CGFloat { 
    let xDist = a.x - b.x 
    let yDist = a.y - b.y 
    return CGFloat(sqrt((xDist * xDist) + (yDist * yDist))) 
}