如何检测抽头数

问题描述 投票:1回答:6

我想检测水龙头用户的数量可以点击多次,我有一个基于抽头数来执行动作。

我试着用UIButton与下面的代码,但其检测所有的水龙头

如果我点击三次,它打印

1 2 3

代码 -

tapButton.addTarget(self, action: #selector(multipleTap(_:event:)), for: UIControl.Event.touchDownRepeat)

@objc func multipleTap(_ sender: UIButton, event: UIEvent) {
    let touch: UITouch = event.allTouches!.first!
    print(touch.tapCount)
}

我需要的输出只是3,如果我点击三次。

编辑1:前 - 如果你在YouTube上点击三次,将前方30秒,如果你点击4次,将前方40秒。

ios swift uibutton uigesturerecognizer
6个回答
1
投票

您还没有明确界定的问题。你是说要在很短的时间内检测一组重复水龙头为单,多抽头事件,并报告抽头数?如果是这样,你需要添加逻辑来做到这一点。决定你考虑的情况下完成前抽头之间需要等待多久。

然后,您需要保留的许多水龙头是如何发生的轨道,并且它多久了自上次自来水。你需要的时候抽头之间的间隔,没有新的水龙头通过触发一个定时器,你可以考虑的情况下完成。

这一切听起来很像一个水龙头手势识别比一个按钮。点击手势recoginizers写入检测抽头的具体人数,超时等,但不以水龙头的变量数量作出回应。您可能希望创建一个自定义手势识别,响应水龙头,而不是一个按钮的可变数目。


0
投票
class ViewController: UIViewController {

  var tapCount: Int = 0

  override func viewDidLoad() {
        super.viewDidLoad()

       tapButton.addTarget(self, action: #selector(multipleTap(sender:)), for: .touchUpInside)

  }

      @objc func multipleTap(sender: UIButton) {
         tapCount += 1
         if tapCount == 3 {
             print(tapCount) //3
         }
    }
}

0
投票

引进VAR到按点击次数

修改后的代码将是:

tapButton.addTarget(self, action: #selector(singleTap(_:)), for: .touchUpInside)


    private var numberOfTaps = 0
    private var lastTapDate: Date?

    @objc private func singleTap(_ sender: UIButton) {
        if let lastTapDate = lastTapDate, Date().timeIntervalSince(lastTapDate) <= 1 { // less then a second
            numberOfTaps += 1
        } else {
            numberOfTaps = 0
        }

        lastTapDate = Date()

        if numberOfTaps == 3 {
          // do your rewind stuff here 30 sec
          numberOfTaps = 0
        }

    }

编辑:我不介意的读者,但我猜你看像上述(更新代码)


0
投票

我不知道对正是你需要这个东西,但低于邓肯的回答,我读了你需要复制从YouTube逻辑的东西。

是的,当然你也可以使用自来水手势识别,但如果从某些原因UIButton需要,你可以创建它的子类。

第一触摸没有反应后,但此后你会在视图控制器设置每一个触摸valueChanged处理程序被调用。如果不再次按下按钮,以一定的等待时长,touches将被重置为0。

class TouchableButton: UIButton {

    var waitDuration: Double = 1.5   // change this for custom duration to reset
    var valueChanged: (() -> Void)?  // set this to handle press of button
    var minimumTouches: Int = 2      // set this to change number of minimum presses

    override init(frame: CGRect) {
        super.init(frame: frame)
        setTarget()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setTarget()
    }

    private func setTarget() {
        addTarget(self, action: #selector(buttonTouched), for: .touchUpInside)
    }

    @objc private func buttonTouched() {
        touches += 1
    }

    private var timer: Timer?

    private var touches: Int = 0 {
        didSet {
            if touches >= minimumTouches {
                valueChanged?()
                timer?.invalidate()
                timer = Timer.scheduledTimer(withTimeInterval: waitDuration, repeats: false) { _ in
                    self.touches = 0
                }
            }
        }
    }

}

那么当你需要设置每一个接触后会发生什么,你可以设定值改变处理程序

button.valueChanged = { // button of type `TouchableButton`
    //print("touched")
    ... // move 10s forward or backwards
}

您也可以更改指定等待最后新闻界和时间之间的时间waitDuration将被重置touches财产

button.waitDuration = 1

你也可以设置(当valueChanged被执行第一次)最少次数的触摸

button.minimumTouches = 3

0
投票

你需要检查的时间差为触摸事件。对于例如:如果按下按钮4次,在特定的时间,说1秒,如果它不会再次按下,那么你可以打印4个抽头否则不打印。

为此,您还需要调用计时器,这样你可以检查上次事件的时间,并相应地进行打印。

var timer : Timer?
var timeDuration = 2.0
var tapCount = 0
var lastButtonPressedTime : Date?

@IBAction func tapCountButtonAction(_ sender: UIButton) {
    lastButtonPressedTime = Date()
    if(tapCount == 0){
        tapCount = 1
        timer = Timer.scheduledTimer(withTimeInterval: timeDuration, repeats: true, block: { (timer) in
            let difference = Calendar.current.dateComponents([.second], from:self.lastButtonPressedTime!, to:Date())
            if(difference.second! > 1){
                print(self.tapCount)
                timer.invalidate()
                self.tapCount = 0
            }
        })
    }else{
        tapCount += 1
    }
}

0
投票

这里使用的技术是在执行最后的动作引入的时间延迟

static let tapCount = 0

tapButton.addTarget(self, action: #selector(multipleTap(_:event:)), for: UIControl.Event.touchDownRepeat)

// An average user can tap about 7 times in 1 second using 1 finger 
DispatchQueue.main.asyncAfter(deadLine: .now() + 1.0 /* We're waiting for a second till executing the target method to observe the total no of taps */ ) {
    self.performAction(for: tapCount)
}

@objc func multipleTap(_ sender: UIButton, event: UIEvent) {
    tapCount += 1   
}

/// Perform the respective action based on the taps
private func performAction(for count: Int) {

    // Resetting the taps after 1 second
    tapCount = 0

    switch (count){
    case 2:
        // Handle 2 clicks  
    case 3:
        // Handle 3 clicks  
    case 4:
        // Handle 4 clicks
    default:
        print("Action undefined for count: \(count)")
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.