【问题标题】:UIPanGesture recognizer swiping only in one directionUIPanGesture 识别器仅向一个方向滑动
【发布时间】:2018-09-27 04:46:32
【问题描述】:

我正在开发 UIPanGestureRecognizer,对我来说它正在工作。但我在这里遇到了一些问题,因为我是 iOS 新手,刚刚从 Android 转移到 iOS。 先看看我要做什么:

我想要什么:我有一个 UITableView,我想在单元格上执行滑动。我只想将它们从左向右拖动并移动/删除该单元格。与在android中完成的非常相似。

但我只想将项目移动到一个方向。那就是“从左到右”。但不是从右到左。现在在这里看看我到目前为止做了什么

我做了什么:

    @objc func handlePan(recognizer: UIPanGestureRecognizer) {
    // 1
    if recognizer.state == .began {
        // when the gesture begins, record the current center location
        originalCenter = center
        print("Center",originalCenter)

    }
    // 2
    if recognizer.state == .changed {
        let translation = recognizer.translation(in: self)


        center = CGPoint(x: originalCenter.x+translation.x, y: originalCenter.y)

        // has the user dragged the item far enough to initiate a delete/complete?
        deleteOnDragRelease = frame.origin.x < -frame.size.width / 2.0
        completeOnDragRelease = frame.origin.x > frame.size.width / 2.0
        // print ("FrameX = ",frame.origin.x , " , ","Width = ",frame.size.width / 2.0 , "Total = ",frame.origin.x < -frame.size.width / 2.0 )
        //print ("DelOnDrag = ",deleteOnDragRelease , " , ","CompOnDrag = ",completeOnDragRelease)

    }
    // 3
    if recognizer.state == .ended {
        // the frame this cell had before user dragged it
        let originalFrame = CGRect(x: 0, y: frame.origin.y,
                                   width: bounds.size.width, height: bounds.size.height)
        if deleteOnDragRelease {
            if delegate != nil && clickedItem != nil {
                // notify the delegate that this item should be deleted
                delegate!.toDoItemDeleted(clickedItem: clickedItem!)
            }
        } else if completeOnDragRelease {

            UIView.animate(withDuration: 8.2, animations: {self.frame = originalFrame})
        } else {
            UIView.animate(withDuration: 8.2, animations: {self.frame = originalFrame})
        }
    }
}

我知道我可以对 ".changed" 进行检查,并计算 X 值是否趋向于 0 或小于 0。但有一段时间它会从右向左移动项目。

问题:有什么方法可以得到接触点的 x 值吗?或者只是一些如何让用户想要从右向左滑动并阻止用户这样做?请分享你的知识

【问题讨论】:

  • 你应该尝试使用UISwipeGesture而不是UIPanGesture
  • 我为什么要使用这个?
  • 给我它的例子,或者教程
  • @QuocNguyen 是对的,您必须在 uitableviewcell 上执行 Uiswipegesture 并且您不需要任何 cgpoint 只需使用交换手势.direction == 的委托方法然后执行您的任务
  • 感谢@HimanshuMoradiya 这很棒

标签: ios swift4 xcode9 uipangesturerecognizer


【解决方案1】:

您的相同代码只需将您的 UIGestureRecognizer 方法中的一项更改替换为此代码即可解决您的问题。仅在您的 tableview 单元格上从左到右交换工作。任何重新评分的查询只需在下面发表评论。

override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if let panGestureRecognizer = gestureRecognizer as? UIPanGestureRecognizer {
            let translation = panGestureRecognizer.translation(in: superview!)
            if translation.x >=  0 {
                return true
            }
            return false
        }
        return false
    }

祝你好运。

继续编码。

【讨论】:

    【解决方案2】:

    您可以使用以下扩展来做到这一点

    extension UIPanGestureRecognizer {
    
        enum GestureDirection {
            case Up
            case Down
            case Left
            case Right
        }
    
        func verticalDirection(target: UIView) -> GestureDirection {
            return self.velocity(in: target).y > 0 ? .Down : .Up
        }
    
        func horizontalDirection(target: UIView) -> GestureDirection {
            return self.velocity(in: target).x > 0 ? .Right : .Left
        }   
    }
    

    你可以像下面这样得到方向

    gestureRecognizer.horizontalDirection(target: self)
    

    【讨论】:

    • 它没有帮助。因为我只想检查运动是从左到右还是从右到左水平移动。你提出的想法我已经以更紧凑的方式实现了它
    猜你喜欢
    • 2012-05-12
    • 1970-01-01
    • 2014-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    相关资源
    最近更新 更多