簡體   English   中英

如何知道ios中的平移手勢改變方向?

[英]How to know pan gesture change direction in ios?

- (void)panRecognized:(UIPanGestureRecognizer *)rec
{
    CGPoint vel = [rec velocityInView:self.view];
    if (vel.x > 0)
    {
        // user dragged towards the right
        counter++;
    }
    else
    {
        // user dragged towards the left
        counter--;
    }
}

當我平移手勢向左移動,然后向右移動。 它將使用正確的。 我想如何知道平移手勢改變方向?

嘗試這個,

- (void)handleGesture:(UIPanGestureRecognizer *)gestureRecognizer
{
    CGPoint velocity = [gestureRecognizer velocityInView:yourView];

    if(velocity.x > 0)
    {
        NSLog(@"gesture moving right");
    }
    else
    {
        NSLog(@"gesture moving left");
    }

     if(velocity.y > 0)
    {
        NSLog(@"gesture moving Up");
    }
    else
    {
        NSLog(@"gesture moving Bottom");
    }

}

當您要處理多個方向的移動時,這段代碼用OptionSet擴展了Luca Davanzo的答案,以獲取正確的結果。

斯威夫特3.1

extension UIPanGestureRecognizer {

    public struct PanGestureDirection: OptionSet {
        public let rawValue: UInt8

        public init(rawValue: UInt8) {
            self.rawValue = rawValue
        }

        static let Up = PanGestureDirection(rawValue: 1 << 0)
        static let Down = PanGestureDirection(rawValue: 1 << 1)
        static let Left = PanGestureDirection(rawValue: 1 << 2)
        static let Right = PanGestureDirection(rawValue: 1 << 3)
    }

    private func getDirectionBy(velocity: CGFloat, greater: PanGestureDirection, lower: PanGestureDirection) -> PanGestureDirection {
        if velocity == 0 {
            return []
        }
        return velocity > 0 ? greater : lower
    }

    public func direction(in view: UIView) -> PanGestureDirection {
        let velocity = self.velocity(in: view)
        let yDirection = getDirectionBy(velocity: velocity.y, greater: PanGestureDirection.Down, lower: PanGestureDirection.Up)
        let xDirection = getDirectionBy(velocity: velocity.x, greater: PanGestureDirection.Right, lower: PanGestureDirection.Left)
        return xDirection.union(yDirection)
    }
}

像這樣使用它:

let direction = panGestureRecognizer.direction(in: superview)
if direction.contains(.Left) && direction.contains(.Down) {
    // do stuff
} else if direction.contains(.Up) {
    // ...
}

在Swift中,我創建了自己的擴展程序,可在擁有UIPanGestureRecognizer的任何地方輕松使用。

extension UIPanGestureRecognizer {

    enum GestureDirection {
        case Up
        case Down
        case Left
        case Right
    }

    /// Get current vertical direction
    ///
    /// - Parameter target: view target
    /// - Returns: current direction
    func verticalDirection(target target: UIView) -> GestureDirection {
        return self.velocityInView(target).y > 0 ? .Down : .Up
    }

    /// Get current horizontal direction
    ///
    /// - Parameter target: view target
    /// - Returns: current direction
    func horizontalDirection(target target: UIView) -> GestureDirection {
        return self.velocityInView(target).x > 0 ? .Right : .Left
    }

    /// Get a tuple for current horizontal/vertical direction
    ///
    /// - Parameter target: view target
    /// - Returns: current direction
    func versus(target target: UIView) -> (horizontal: GestureDirection, vertical: GestureDirection) {
        return (self.horizontalDirection(target: target), self.verticalDirection(target: target))
    }

}

您可以通過以下方式使用:

override viewDidLoad() {
    super.viewDidLoad()
    let panGesture = UIPanGestureRecognizer(target: self, action: #selector(ViewController.didSwipeOnView(_:)))
    self.addGestureRecognizer(panGesture) 
}

func didSwipeOnView(gestureRecognizer: UIPanGestureRecognizer) {
    if case .Down = gestureRecognizer.verticalDirection(target: self) {
       print("Swiping down")
    } else {
       print("Swiping up")
    }
}

迅捷3

創建手勢並將其附加到您的視圖:

let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handleGesture))
yourView.addGestureRecognizer(panGesture)

創建一個將保存最新方向值的類變量:

var latestDirection: Int = 0

yourViewyourView將觸發手勢處理程序,我們將在該處檢查平移方向是否已更改:

func handleGesture(gesture: UIPanGestureRecognizer) {
  let velocity = gesture.velocity(in: yourView)
  var currentDirection: Int = 0

  if velocity.x > 0 {
    print("panning right")
    currentDirection = 1
  } else {
    print("panning left")
    currentDirection = 2
  }

  // check if direction was changed
  if currentDirection != latestDirection {
    print("direction was changed")
  }

  latestDirection = currentDirection
}

這只能處理左右平移之間的方向變化,但是如果有人想檢測上/下平移,請將此代碼添加到handleGesture函數中:

if velocity.y > 0 {
  print("panning down")
} else {
  print("panning up")
}

斯威夫特 5.x

extension UIPanGestureRecognizer {

    public struct PanGestureDirection: OptionSet {
        public let rawValue: UInt8

        public init(rawValue: UInt8) {
            self.rawValue = rawValue
        }

        static let Up = PanGestureDirection(rawValue: 1 << 0)
        static let Down = PanGestureDirection(rawValue: 1 << 1)
        static let Left = PanGestureDirection(rawValue: 1 << 2)
        static let Right = PanGestureDirection(rawValue: 1 << 3)
    }

    public func direction(in view: UIView) -> PanGestureDirection {
        let velocity = self.velocity(in: view)
        let isVerticalGesture = abs(velocity.y) > abs(velocity.x)
        if isVerticalGesture {
            return velocity.y > 0 ? .Down : .Up
        } else {
            return velocity.x > 0 ? .Right : .Left
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM