这不是Sprite Kit。
如果我有一个类似下面的变量
var value = 0
如果用户向右拖动我如何增加值?如果他们向左拖动则如何减少?
谢谢!
答案 0 :(得分:1)
就像Caleb评论的那样,Ray的教程很棒,但是如果你想要实际的快速示例,请查看下一个例子:
class ViewController: UIViewController, UIGestureRecognizerDelegate {
private var value: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.blackColor()
let recognizer = UIPanGestureRecognizer(target: self, action: Selector("handleDragging:"))
let inputView = UIView(frame: CGRectMake(0, 0, 100, 100))
inputView.backgroundColor = UIColor.whiteColor()
inputView.userInteractionEnabled = true
inputView.addGestureRecognizer(recognizer)
self.view.addSubview(inputView)
}
func handleDragging(recognizer: UIPanGestureRecognizer) {
if (recognizer.state == .Changed) {
let point = recognizer.velocityInView(recognizer.view?.superview)
if (point.x > 0) {
self.value++;
} else {
self.value--;
}
println(self.value)
}
}
}
答案 1 :(得分:0)
您可以使用UIPanGestureRecognizer的velocityInView
方法来确定您要去的方向。它返回一个CGPoint,因此您可以根据需要提取x和y值。正向右/向下,负向左/向上。