以编程方式模拟Swift中的滑动手势

时间:2016-08-15 10:37:04

标签: ios swift uigesturerecognizer swipe-gesture

我正在实施一个手势识别器,用于在Swift中滑动。我希望能够模拟卡的投掷(以编程方式滑动视图)。

我认为会有一个内置功能,但我找到的只是一个轻击手势而不是轻扫手势。

这就是我实施滑动手势的方式:

  let gesture = UIPanGestureRecognizer(target: self, action: Selector("wasDragged:"))
    cardView.addGestureRecognizer(gesture)
    cardView.userInteractionEnabled = true
}

func wasDragged (gesture: UIPanGestureRecognizer) {        
    let translation = gesture.translationInView(self.view)
    let cardView = gesture.view!

    // Move the object depending on the drag position
    cardView.center = CGPoint(x: self.view.bounds.width / 2 + translation.x,
                              y:  self.view.bounds.height / 2 + translation.y)

5 个答案:

答案 0 :(得分:1)

您可以自己创建UIPanGestureRecognizer并将其传递给wasDragged方法。您应该检查不同的翻译值:

let gesture = UIPanGestureRecognizer()
gesture.setTranslation(CGPointMake(0, 100), inView: self.view)
wasDragged(gesture)

SWIFT 4.2

 let gesture = UIPanGestureRecognizer()
 gesture.setTranslation(CGPoint(x: 0, y: 100), in: self.view)
 wasDragged(gesture)

虽然我认为你需要别的东西。为什么你需要首先模拟这个手势?

答案 1 :(得分:0)

适用于SWIFT 3.0

let swipeRightOrange:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(slideToRightWithGestureRecognizer))
swipeRightOrange.direction = .Right;

let swipeLeftOrange:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(slideToLeftWithGestureRecognizer))
swipeLeftOrange.direction = .Left;

@IBAction func slideToLeftWithGestureRecognizer(gestureRecognizer:UISwipeGestureRecognizer)
{
viewOrange.backgroundColor = UIColor.blue
}
@IBAction func slideToRightWithGestureRecognizer(gestureRecognizer:UISwipeGestureRecognizer)
{
viewOrange.backgroundColor = UIColor.lightGray
}

答案 2 :(得分:0)

您无法模拟手势识别器的全部含义(我的意思是,您实际上无法让iOS认为这是真正的用户操作)。

但是,你可以欺骗自己的代码,让它看起来像是真正的滑动。为此,您需要首先创建一个手势识别器:

var gestureRecognizerSwipeRight = UISwipeGestureRecognizer(target: self, action: "activatedGestureRecognizer:")
gestureRecognizerSwipeRight.direction = UISwipeGestureRecognizerDirection.Right
yourView.addGestureRecognizer(gestureRecognizerSwipeRight)

然后将其直接传递给您的行动:

// Some other place in your code
self.activatedGestureRecognizer(gesture: gestureRecognizerSwipeRight)

您的activatedGestureRecognizer(gesture:)方法应该类似于:

func activatedGestureRecognizer(gesture: UIGestureRecognizer) {
    if let gestureRecognizer = gesture as? UIGestureRecognizer {

        // Here you can compare using if gestureRecognizer == gestureRecognizerSwipeRight
        // ...or you could compare the direction of the gesture recognizer.
        // It all depends on your implementation really.

        if gestureRecognizer == gestureRecognizerSwipeRight {
            // Swipe right detected
        }
    }
}

公平地说,我认为这样做并没有任何实际的好处。简单地执行与滑动相关联的操作而不是实际模拟手势识别器应该更好。

例如,如果您需要在刷卡时为卡片制作动画,为什么不简单地禁用卡片视图上的用户互动并以编程方式为其设置动画?

答案 3 :(得分:0)

这是可用于以编程方式滑动单元格的代码。执行此操作后,该单元将动画到滑动位置。

@IBAction
func swipeFirstCell() {
    if let cell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) {
        tableView.perform(Selector("_endSwipeToDeleteRowDidDelete:"), with: nil) // cancel any existing swipes
        tableView.perform(Selector("_swipeToDeleteCell:"), with: cell)
    }
}

请注意,您还需要为canEditRowAtIndexPath中的单元格返回true。

这可能会被Apple的私有API检测标记,但是您至少可以将其用于开发,如果确实需要,可以混淆字符串。

答案 4 :(得分:-1)

您需要实施UISwipeGestureRecognizer

override func viewDidLoad() {
super.viewDidLoad()

var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
self.view.addGestureRecognizer(swipeRight)

var swipeDown = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeDown.direction = UISwipeGestureRecognizerDirection.Down
self.view.addGestureRecognizer(swipeDown)

}

func respondToSwipeGesture(gesture: UIGestureRecognizer) {

if let swipeGesture = gesture as? UISwipeGestureRecognizer {


    switch swipeGesture.direction {
        case UISwipeGestureRecognizerDirection.Right:
            print("Swiped right")
        case UISwipeGestureRecognizerDirection.Down:
            print("Swiped down")
        case UISwipeGestureRecognizerDirection.Left:
            print("Swiped left")
        case UISwipeGestureRecognizerDirection.Up:
            print("Swiped up")
        default:
            break
    }
 }
}