2017-01-01 30 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) ,这可能是错误的无论如何,即使它不可见!