gpt4 book ai didi

swift - 按钮需要消失然后回来

转载 作者:搜寻专家 更新时间:2023-11-01 06:44:35 27 4
gpt4 key购买 nike

我现在有一个应用程序,如果你点击一个按钮它就会消失,现在我想让按钮消失,即使你没有点击那个按钮,但它会在几秒钟后恢复(甚至不到一秒)。目前,这就是按钮在代码中的样子。

    @IBAction func increaseCount(button: UIButton) -> Void {
button.hidden = true
ourScore.text = "\(++score)"


let time = dispatch_time(DISPATCH_TIME_NOW, Int64(Double((arc4random_uniform(1) + 2)) * Double(NSEC_PER_SEC)))
dispatch_after(time, dispatch_get_main_queue()) {
button.hidden = false
}
}

即使您没有点击按钮,如何让按钮消失,但它会在短短几秒钟(不到一秒)内返回?时间应该在 2 秒半之间随机。当您点击它时,它也应该消失,并且会在 2 秒内返回。

谁能帮帮我?

最佳答案

此代码将使按钮每 2 秒出现并重新出现。您可以修改时间,使其随机(如果您需要帮助,请告诉我)。

在 Storyboard上链接您的按钮,下面的代码应该可以解决问题。

@IBOutlet weak var button: UIButton!

override func viewDidLoad() {
super.viewDidLoad()
button.hidden = true
NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: "appear:", userInfo: self, repeats: false)
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func appear(timer: NSTimer) {
self.button.hidden = true
NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: "disappear:", userInfo: self, repeats: false)

}

func disappear(timer: NSTimer) {
self.button.hidden = false
NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: "appear:", userInfo: self, repeats: false)

}

编辑:要使按钮在单击时消失,请从按钮注册一个 Action 事件并使用代码:

@IBAction func clicked(sender: UIButton) {
self.button.hidden = true
NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "appear:", userInfo: self, repeats: false)
}

同样,单击它时它只会隐藏 1 秒,但您可以将时间更改为随机时间。

编辑 2:你应该看到这个:

Storyboard Connections

关于swift - 按钮需要消失然后回来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31784983/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com