- (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--;
}
}
当我平移手势向左移动时,然后向右移动。它会使用权利。 我想知道如何知道平移手势改变的方向?
答案 0 :(得分:17)
试试这个,
- (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");
}
}
答案 1 :(得分:5)
当你想要处理多个方向的移动时,这段代码用一个OptionSet扩展了Luca Davanzo的答案,以获得正确的结果。
Swift 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) {
// ...
}
答案 2 :(得分:2)
在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 :(得分:1)
Swift 3
创建手势并将其附加到您的视图中:
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handleGesture))
yourView.addGestureRecognizer(panGesture)
创建一个保存最新方向值的类变量:
var latestDirection: Int = 0
在yourView
上进行平移会触发手势处理程序,我们会检查平移方向是否已更改:
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")
}