【问题标题】:Long Press Recognize Gesture via Button - Swift 3长按通过按钮识别手势 - Swift 3
【发布时间】:2017-08-02 07:23:43
【问题描述】:

我是 Swift 新手,我正在尝试制作从长按按钮开始的计时器(在标签中)。同时我想在长按按钮时更改长按按钮图像。我离开按钮,我希望按钮恢复原状。

可能出了什么问题?

@IBOutlet weak var myBtn: UIButton!

func initGesture()
{

    {   let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap(_:)))
        myBtn.addGestureRecognizer(longGesture)
    }
}

func TimerAction()
{
    Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(longTap), userInfo: nil, repeats: false)
    myBtn.setImage(UIImage(named: "xxx.png"), for: .normal)


}
@IBOutlet weak var lbl: UILabel!

func start()
{
    timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(ViewController2.updateTime as (ViewController2) -> () -> ())), userInfo: nil, repeats: true)

}

func updateTimer () {
    count += 1
    let hours = Int(count) / 3600
    let minutes = Int(count) / 60 % 60
    let seconds = Int(count) % 60
    label.text = String(format: "%02i:%02i:%02i",hours,minutes,seconds)
}

func reset()
{
    timer.invalidate()
    count = 0
    label.text = "00:00:00"

}

【问题讨论】:

  • 你的代码在做什么?
  • 我正在尝试做一些我写的事情。我在长按手势识别时敲了myBtn。我哪里错了? @MohammadBashirSidani
  • 发布你的 longTap 方法
  • 您正在调用 updateTime 方法(在您的代码中不存在),而不是在 start 方法中调用 updateTimer 方法。然后将您的图像设置为@Kiester 解决方案。

标签: ios iphone swift swift3 uilongpressgesturerecogni


【解决方案1】:

你可以得到 TouchUpInside 和 TouchUpOutside 按钮的 TouchDown 动作不使用手势。

class ViewController: UIViewController {
@IBOutlet weak var lbl: UILabel!
@IBOutlet weak var myBtn: UIButton!
var timer: NSTimer!

override func viewDidLoad() {
      super.viewDidLoad()
      myBtn.addTarget(self, action: "buttonDown:", forControlEvents: .TouchDown)
      myBtn.addTarget(self, action: "buttonUp:", forControlEvents: [.TouchUpInside, .TouchUpOutside])
    }
func buttonDown(sender: AnyObject) {
    singleFire()
    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "rapidFire", userInfo: nil, repeats: true)
}

func buttonUp(sender: AnyObject) {
    timer.invalidate()
    count = 0
    label.text = "00:00:00"
}

func singleFire() {
    myBtn.setImage(UIImage(named: "xxx.png"), for: .normal)
}

func rapidFire() {
    count += 1
    let hours = Int(count) / 3600
    let minutes = Int(count) / 60 % 60
    let seconds = Int(count) % 60
    label.text = String(format: "%02i:%02i:%02i",hours,minutes,seconds)
}

}

【讨论】:

  • 不错的一个。但它增加了识别长按的代码。
  • @jegadeesh 感谢您的建议,但我正在展示行动的用途。
  • 乐于助人?
【解决方案2】:

您应该在此处实现按钮事件而不是使用手势识别器。我谈论的按钮事件是 TouchDown 和 TouchUpInside。 TouchDown 会告诉您何时按下按钮,而 touchupinside 会告诉您用户何时从按钮上抬起手指。

因此,您将更改按钮图像并在触地事件中启动计时器。然后您将恢复到 touchupinside 事件。

https://developer.apple.com/documentation/uikit/uicontrolevents

【讨论】:

    【解决方案3】:

    试试这个,

    首先根据状态设置按钮的图像。

        self.arrowIcon.setImage(UIImage(named: "normalImage.png"), for: .normal)
        self.arrowIcon.setImage(UIImage(named: "pressedImage.png"), for: .highlighted)
    

    【讨论】:

    • 很高兴帮助@MarryJoe!
    猜你喜欢
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    相关资源
    最近更新 更多