2017-01-01 69 views
-5

我的代碼如下是一個計時器。一旦達到2秒鐘,就會出現一個按鈕。現在,如果你按下按鈕將永遠消失。我希望按鈕在10秒後重新出現。通過10秒後,我想覆蓋如果在2和9範圍內選擇按鈕消失的操作。所以如果有人在2到9秒之間點擊按鈕。我希望按鈕在10秒後自動重新出現。因爲你躲這一次如何使用if else語句重寫按鈕操作。

Rest.isHidden = true 

而不再顯示回

import UIKit 
class ViewController: UIViewController { 
@IBOutlet var Rest: UIButton! 
@IBOutlet var start: UIButton! 
var timer = Timer() 
var counter = 0.0 
var isRunning = false 

@IBOutlet var dx: UILabel! 
override func viewDidLoad() { 
super.viewDidLoad() 
     dx.text = "\(counter)" 
start.isEnabled = true 
    } 
@IBAction func play(_ sender: Any) { 
if !isRunning{ 
    timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(ViewController.updateTimer), userInfo: nil, repeats: true) 



    start.isEnabled = false 

    isRunning = true 
    }} 
@IBAction func disaper(_ sender: Any) { 

Rest.isHidden = true 

} 


func updateTimer(){ 
counter += 0.1 

dx.text = String(format: "%.1f", counter) 


if counter > 2 && counter < 9 
{ 
    Rest.alpha = 1 
    Rest.isEnabled = true 


} else if counter > 10 { 
    Rest.alpha = 1 
    Rest.isEnabled = true 
} 

else { 
    return 
    Rest.alpha = 0 
    Rest.isEnabled = false 

}}} 
+0

您的代碼和描述都是亂碼。您當前的代碼在2-9秒和9-10秒週期內有不同的邏輯,但這兩個邏輯塊執行相同的操作。您當前的計時器代碼會將按鈕從0秒隱藏到2秒,在2-9和9-10秒內顯示並啓用它,然後在超過10秒後隱藏它。你只能在IBaction'play'中啓動你的計時器,但不清楚該動作連接到哪個按鈕。 –

+0

你有另一個行動'便宜'隱藏一個名爲「休息」的字段,但不對定時器做任何事情。你有2個不同的按鈕,一個開始按鈕和一個「休息」按鈕? 「Rest」按鈕是應該隱藏的按鈕嗎? –

+0

請勿重新發布您的問題。如果需要,請更新您的上一個問題。 – rmaddy

回答

0

你永遠按鈕消失。 管理此按鈕的alpha/isEnabled不會直接影響它,因爲它根本沒有被繪製。

雖然按鈕邏輯似乎是錯誤的。

而且,你不重置計數器,因此它被一遍又一遍地增加,讓你無論是要重置的每個10秒,或者你要達到它由10

if counter.divided(by: 10) > someValue { ... } 

分如果我的權利明白了,只有你需要的是重新放映按鈕後,其點擊10秒,那麼你應該使用類似:

import UIKit 

class ViewController: UIViewController { 
    @IBOutlet var Rest: UIButton! 
    @IBOutlet var start: UIButton! 
    @IBOutlet var dx: UILabel! 

    var timer: Timer? 
    var isRunning: Bool { 
     get { 
      // if you ever pressed the button, then after everytime the timer will be present 
      return timer != nil 
     } 
    } 
    var counter = 0.0 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     dx.text = String(format: "%.1f", counter) 

     // enable only if you disabled it in XIB/Storyboard, otherwise just remove the line below 
     start.isEnabled = true 
    } 

    @IBAction func play(_ sender: Any) { 
     if isRunning { 
      // we already disable the button, but who knows what code will be after? 
      // maybe once in future you will re-enable it accidentally 
      return 
     } 

     refreshTimer() 
    } 

    @IBAction func disaper(_ sender: Any) { 
     // hide button when it's clicked 
     Rest.isHidden = true 

     // refresh timer/game/whatever 
     refreshTimer() 

     // setup this block to fire after 10 seconds from now 
     DispatchQueue.main.asyncAfter(deadline: .now() + 10.0) { [weak self] in 
      // only use self as weak reference for cases if user would like to go back from screen 
      // or this won't deallocate the screen 
      if let `self` = self { 
       // return button's vision 
       self.Rest.isHidden = false 
      } 
     } 
    } 

    func refreshTimer() { 
     // invalidate timer if it exists 
     if let timer: Timer = timer { 
      timer.invalidate() 
     } 

     // setup new timer 
     timer = Timer.scheduledTimer(timeInterval: 0.1, 
            target: self, 
            selector: #selector(updateTimer), 
            userInfo: nil, repeats: true) 

     start.isEnabled = false 
    } 

    func updateTimer() { 
     counter += 0.1 
     dx.text = String(format: "%.1f", counter) 
    } 
} 

而且記得,如果你只設置按鈕的alpha透明(0.0) ,這可能是錯誤的無論如何,即使它不可見!