如何知道ios中的平移手势更改方向?
当我平移手势时,向左移动,然后向右移动。它将正确使用。 我想知道如何知道平移手势改变方向?试试这个如何知道ios中的平移手势更改方向?,ios,objective-c,swift,Ios,Objective C,Swift,当我平移手势时,向左移动,然后向右移动。它将正确使用。 我想知道如何知道平移手势改变方向?试试这个 - (void)panRecognized:(UIPanGestureRecognizer *)rec { CGPoint vel = [rec velocityInView:self.view]; if (vel.x > 0) { // user dragged towards the right counter++; }
- (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--;
}
}
在Swift中,我创建了自己的扩展,无论您在哪里都可以使用UIPangestureRecognitor
- (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");
}
}
您可以这样使用:
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))
}
}
这段代码使用OptiStart扩展了Luca Davanzo的答案,当您想要处理多个方向的移动时,可以获得正确的结果 Swift 3.1
Swift 3 创建手势并将其附加到视图:
let direction = panGestureRecognizer.direction(in: superview)
if direction.contains(.Left) && direction.contains(.Down) {
// do stuff
} else if direction.contains(.Up) {
// ...
}
创建将保存最新方向值的类变量:
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handleGesture))
yourView.addGestureRecognizer(panGesture)
在yourView
上平移将触发手势处理程序,我们在其中检查平移方向是否已更改:
var latestDirection: Int = 0
这只处理左右平移之间的方向变化,但如果有人想检测上下平移,请将此代码添加到handleesture
功能:
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
}
试试这个可能的复制品这个是单方向手指向左移动,然后向右移动,如何判断你改变方向?如果速度(x/y)等于零怎么办?在
func vs(target:)
中,您返回一个具有两个方向的元组,而实际上您有时只能向一个方向移动。您确定会发生这种情况吗?我不确定。。但是,以防万一,我可以通过一个新的手势Direction=>None来改进。这很好,谢谢。尽管用户应该注意,即使从左向右拖动,如果有一点向南的方向,它也会触发。向下。有没有办法降低灵敏度,使它不会触发其他方向?我试着玩速度值,但没用。
var latestDirection: Int = 0
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
}
if velocity.y > 0 {
print("panning down")
} else {
print("panning up")
}