UISwipeGestureRecognizer 滑动长度

Posted

技术标签:

【中文标题】UISwipeGestureRecognizer 滑动长度【英文标题】:UISwipeGestureRecognizer Swipe length 【发布时间】:2011-01-28 13:39:22 【问题描述】:

是否有办法获取滑动手势或触摸的长度以便我可以计算距离?

【问题讨论】:

我认为你只能从 UISwipeGestureRecognizer 获得方向。也许你可以得到触摸开始和结束的位置,并从中计算长度。 【参考方案1】:

从滑动手势获取距离是不可能的,因为 SwipeGesture 会在手势结束时触发您可以准确访问该位置的一次方法。 也许你想使用 UIPanGestureRecognizer。

如果您可以使用平移手势,您将保存平移的起点,如果平移已结束,则计算距离。

- (void)panGesture:(UIPanGestureRecognizer *)sender 
    if (sender.state == UIGestureRecognizerStateBegan) 
        startLocation = [sender locationInView:self.view];
    
    else if (sender.state == UIGestureRecognizerStateEnded) 
        CGPoint stopLocation = [sender locationInView:self.view];
        CGFloat dx = stopLocation.x - startLocation.x;
        CGFloat dy = stopLocation.y - startLocation.y;
        CGFloat distance = sqrt(dx*dx + dy*dy );
        NSLog(@"Distance: %f", distance);
    

【讨论】:

如果使用 UIPanGestureRecognizer,内置方法 - (CGPoint)translationInView:(UIView *)view 效果很好。 你肯定会在这里使用translationInView。也许MB有一天可以编辑答案,因为它是如此的高分!例如***.com/questions/15888276/… 摇滚... @MatthiasBauch 这是英寸吗?【参考方案2】:

在 Swift 中

 override func viewDidLoad() 
    super.viewDidLoad()

    // add your pan recognizer to your desired view
    let panRecognizer = UIPanGestureRecognizer(target: self, action:  #selector(panedView))
    self.view.addGestureRecognizer(panRecognizer)



   @objc func panedView(sender:UIPanGestureRecognizer)
        var startLocation = CGPoint()
        //UIGestureRecognizerState has been renamed to UIGestureRecognizer.State in Swift 4
        if (sender.state == UIGestureRecognizer.State.began) 
            startLocation = sender.location(in: self.view)
        
        else if (sender.state == UIGestureRecognizer.State.ended) 
            let stopLocation = sender.location(in: self.view)
            let dx = stopLocation.x - startLocation.x;
            let dy = stopLocation.y - startLocation.y;
            let distance = sqrt(dx*dx + dy*dy );
            NSLog("Distance: %f", distance);

        if distance > 400 
            //do what you want to do
        
    

希望对所有 Swift 开拓者有所帮助

【讨论】:

对于懒惰的人,您还应该在课堂上声明var startLocation = CGPoint() 同意@RaduUrsache,在上面的这个解决方案中没有办法存储开始位置。【参考方案3】:

对于我们这些使用 Xamarin 的人:

void panGesture(UIPanGestureRecognizer gestureRecognizer) 
    if (gestureRecognizer.State == UIGestureRecognizerState.Began) 
        startLocation = gestureRecognizer.TranslationInView (view)
     else if (gestureRecognizer.State == UIGestureRecognizerState.Ended) 
        PointF stopLocation = gestureRecognizer.TranslationInView (view);
        float dX = stopLocation.X - startLocation.X;
        float dY = stopLocation.Y - startLocation.Y;
        float distance = Math.Sqrt(dX * dX + dY * dY);
        System.Console.WriteLine("Distance: 0", distance);
    

【讨论】:

【参考方案4】:
func swipeAction(gesture: UIPanGestureRecognizer) 
    let transition = sqrt(pow(gesture.translation(in: view).x, 2)
                     + pow(gesture.translation(in: view).y, 2))

【讨论】:

请注意:不鼓励仅使用代码回答。请添加一点解释。 它是计算两点之间距离的方程。 mathsisfun.com/algebra/distance-2-points.html【参考方案5】:

你只能做一个标准的方法:记住 touchBegin 的触摸点并比较 touchEnd 的点。

【讨论】:

【参考方案6】:

我有一个类似于 swift 中的答案的实现,它可以区分拖动和滑动,计算相对于容器的距离和滑动的速度。

@objc private func handleSwipe(sender: UIPanGestureRecognizer) 
    if (sender.state == .began) 
        self.swipeStart.location = sender.location(in: self)
        self.swipeStart.time = Date()
    
    else if (sender.state == .ended) 
        let swipeStopLocation : CGPoint = sender.location(in: self)
        let dx : CGFloat = swipeStopLocation.x - swipeStart.location.x
        let dy : CGFloat = swipeStopLocation.y - swipeStart.location.y
        let distance : CGFloat = sqrt(dx*dx + dy*dy );
        let speed : CGFloat = distance / CGFloat(Date().timeIntervalSince(self.swipeStart.time))
        let portraitWidth = min(self.frame.size.width, self.frame.size.height)
        print("Distance: \(distance), speed: \(speed), dy: \(dy), dx: \(dx), portraitWidth: \(portraitWidth), c1: \(distance >  portraitWidth * 0.4), c2: \(abs(dy) < abs(dx) * 0.25), c3: \(speed > portraitWidth * 3.0) ")
        if distance >  portraitWidth * 0.4 && abs(dy) < abs(dx) * 0.25 && speed > portraitWidth * 3.0 
            if dx > 0 
                delegate?.previousAssetPressed(self)
            else
                delegate?.nextAssetPressed(self)
            
        
    

【讨论】:

以上是关于UISwipeGestureRecognizer 滑动长度的主要内容,如果未能解决你的问题,请参考以下文章

识别用户剪切/滑动手势 (UISwipeGestureRecognizer)

UISwipeGestureRecognizer 滑动长度

UISwipeGestureRecognizer 只有一个方向工作

如何检测 UISwipeGestureRecognizer 的结束?

如何使 UISwipeGestureRecognizer 在 UIScrollView 中工作

UISwipeGestureRecognizer 没有触发