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

前端之家收集整理的这篇文章主要介绍了ios – 以编程方式模拟Swift中的滑动手势前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在实现一个手势识别器,用于在 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)

解决方法

您可以自己创建 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)

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

猜你在找的iOS相关文章