我正在实现一个手势识别器,用于在
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)
虽然我认为你需要别的东西.为什么你需要首先模拟这个手势?