【发布时间】:2016-12-21 13:35:14
【问题描述】:
我正在实现一个手势识别器,用于在 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)
【问题讨论】:
-
视图是否与您当前使用的代码一起移动?
-
那么你想要以编程方式执行滑动手势的操作吗?为什么不在滑动和手动中调用相同的函数呢?当然,偏移量和位置也必须手动设置。
-
@NSGangster 是的,它可以很好地移动和滑动。
-
@Imbue 我不确定你手动调用它是什么意思?
-
我的意思是。如果要“模拟”出牌,可以给实际出牌的函数传入一些随机值,进行模拟。然后就调用它。还是我误会了?
标签: ios swift uigesturerecognizer swipe-gesture