簡體   English   中英

在Xcode 8.3.3上的Swift 3中按住標簽值增加按鈕

[英]Hold down label value increment button in Swift 3 on xcode 8.3.3

我有一個簡單的應用程序,可以進行一些概率計算。 我有一個加號按鈕,可一次將標簽值增加0.1%。 我想做的是如果按住加號按鈕,則可以更快地增加它。 我搜索的所有代碼都是針對較早版本的Swift或Xcode的,我不知道該怎么做!

目前,我已經返回了一個功能正常的應用程序,該應用程序帶有一個名為plusButton@IBAction函數,該函數僅向名為preAssessmentProbability的標簽@IBOutlet添加了0.1%。

如果有人可以幫助我告訴我如何保持此功能,但又增加了按住plusButton的功能以plusButton地增加preAssessmentProbability標簽的能力,我將不勝感激(如果您能幫助我告訴我如何設置此功能的速度,請多謝)。

這是實現。 首先,請確保以這種方式連接按鈕的三個動作。 在此處輸入圖片說明

然后你的代碼應該看起來像

@IBAction func buttonUpInside(_ sender: Any) {
    self.buttonUp()
}

@IBAction func buttonUpOutside(_ sender: Any) {
    self.buttonUp()
}

@IBAction func buttonTouchDown(_ sender: Any) {
    self.buttonDown()
}

func buttonDown() {
    increaseSpeed()
    holdTimer = Timer.scheduledTimer(timeInterval: 2, target: self, selector:#selector(increaseSpeed), userInfo: nil, repeats: true)
}

func buttonUp() {
    speed = 1.0
    holdTimer.invalidate()
    addPercentTimer.invalidate()
}

func increaseSpeed(){
    if speed != 1 {
        addPercentTimer.invalidate()
    }
    if speed > 0 {
        speed -= 0.2 //make it faster!
    }
    addPercentTimer = Timer.scheduledTimer(timeInterval: speed, target: self, selector:#selector(addPercent), userInfo: nil, repeats: true)
}

func addPercent(){
    preAssessmentProbability += 0.1
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM