如何设置它以使pan识别器增加一个值

时间:2016-10-04 18:06:19

标签: ios swift xcode swift3

如何设置它以使平移识别器在向右滑动时增加Int变量,然后在向左滑动时减小现有变量。

@IBAction func customiseWhiteBalance(_ sender: UIPanGestureRecognizer) {
    // Insert gesture value change here.... 
    if sender.state == .began || sender.state == .changed {

        let translation = sender.translation(in: self.view)
        sender.setTranslation(CGPoint.zero, in: view)
        print(translation)
    }
}

由于

1 个答案:

答案 0 :(得分:1)

非常简单:

1:添加一个计数器:

var counter = 0

2:声明你的手势并将它们添加到你的视图中(或你需要它们的地方)

let right = UISwipeGestureRecognizer(target: self, action: #selector(didViewSwipe(_:)))
right.direction = .right
self.view.addGestureRecognizer(right)

let left = UISwipeGestureRecognizer(target: self, action: #selector(didViewSwipe(_:)))
left.direction = .left
self.view.addGestureRecognizer(left)

3:获取手势并开始计算

func didViewSwipe(_ gesture: UIGestureRecognizer) {
    if let swipeGesture = gesture as? UISwipeGestureRecognizer {
        switch swipeGesture.direction {
        case UISwipeGestureRecognizerDirection.right:
            counter = counter + 1
        case UISwipeGestureRecognizerDirection.left:
            if counter > 0{
               counter = counter - 1
            }
        default:
            break
        }
    }
}