- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我现在有一个应用程序,如果你点击一个按钮它就会消失,现在我想让按钮消失,即使你没有点击那个按钮,但它会在几秒钟后恢复(甚至不到一秒)。目前,这就是按钮在代码中的样子。
@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:你应该看到这个:
关于swift - 按钮需要消失然后回来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31784983/
我对 Objective C 和 iOS 开发总体来说是新手。我正在尝试创建一个应用程序,它会发出 http 请求并在标签上显示内容。 当我开始测试时,我注意到标签是空白的,尽管我的日志显示我已经恢复
我实现了一个 MVP 应用程序。后退按钮工作正常。我想以编程方式回到以前的地方。我该怎么做? 我正在寻找类似的东西: clientFactory.getPlaceController().goBack
我想从 mySubView 制作后退按钮。我有带有按钮的付款ViewController,这个按钮使view.addSubview(WkWebView)。所以我可以打开 WkWebView 但我不可能
我有一个 master 分支,里面有很多不会发布的特性。我被要求从 master 中删除这些功能并创建一个包含它们的新分支,以便我们稍后可以 merge 回 master。 我采取的步骤是: 在 ma
如何禁用页面上的所有 onclick 事件,绑定(bind)我的自定义函数,并在执行后启用所有之前的事件? 我正在构建一个小书签,它应该适用于任何已加载的页面,并且我正在使用 jQuery 来处理我的
我正在尝试从 Spotify api 接收 token 。不幸的是,我一直收到 415。你能帮帮我,让我知道我做错了什么吗? const axios = require('axios'); const
我有一个提供上下文的函数: def buildContext(s:String)(request:RequestHeader):Future[Granite.Context] = { ....
我有一个列表,其中包含几个不同形状的 numpy 数组。我想将这个数组列表 reshape 为一个 numpy 向量,然后更改向量中的每个元素,然后将其 reshape 回原始数组列表。 例如: 输入
我目前有这个工作 fiddle - http://jsfiddle.net/B8Abd/ 我想在函数中使用 jquerys 淡出然后淡入。目前的代码是这样的: function chan
我有 2 个分支,一个是 main 分支,另一个是我正在开发一个 parallel 版本。 A --> B --> C (master) \ -> E --> F (parallel) pa
我试图从我的“ super 项目”中的文件目录中创建一个“子项目”以与其他人一起工作,但我一直在努力使其在 git subtree 中工作。 理想情况下,其他人可以在子项目上工作,然后我从上游更改中提
有没有其他人看到这个? 我正在使用带有回形针的 rails 3,当我上传 .doc 时,它的应用程序/msword 效果很好,但是当我上传 .docx 时,content_type 被保存为应用程序/
我是一名优秀的程序员,十分优秀!