UISwipeGestureRecognizer刷卡长度

问题描述 投票:36回答:6

任何想法,如果有一种方式来获得滑动手势或触摸,这样我可以计算距离的长度?

iphone ipad ios uigesturerecognizer gesture
6个回答
55
投票

这是不可能得到从轻扫手势的距离,因为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);
    }
}

16
投票

在斯威夫特

 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
        }
    }
}

希望可以帮助你斯威夫特开拓者


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);
    }
}

2
投票

你只能做一个标准的方法:记得touchBegin的接触点和比较来自touchEnd点。


2
投票
func swipeAction(gesture: UIPanGestureRecognizer) {
    let transition = sqrt(pow(gesture.translation(in: view).x, 2)
                     + pow(gesture.translation(in: view).y, 2))
}

0
投票

我有类似的阻力和刷卡相对于容器的距离和刷卡的速度计算区别对待斯威夫特答案的实现。

@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)
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.