我需要构建一个iOS Swift单页面应用程序,它具有60:00分钟的倒数计时器.2秒后,我需要显示一个UILabel
,在6秒后隐藏它,然后显示另一个文本.到目前为止这是我的代码:
var startTime = NSTimeInterval()
var timer = NSTimer()
func startCountdownTimer() {
var currentTime = NSDate.timeIntervalSinceReferenceDate()
//Find the difference between current time and start time.
var elapsedTime: NSTimeInterval = 3600-(currentTime-startTime)
//Calculate the minutes in elapsed time.
let minutes = UInt8(elapsedTime / 60.0)
elapsedTime -= (NSTimeInterval(minutes) * 60)
//Calculate the seconds in elapsed time.
var seconds = UInt8(elapsedTime)
elapsedTime -= NSTimeInterval(seconds)
//Add the leading zero for minutes and seconds and store them as string constants
let strMinutes = …
Run Code Online (Sandbox Code Playgroud) 好吧,标题真的说明了一切,我无法在任何适合我的地方找到答案,所以我转向StackOverFlow.我正在尝试获取用户步数并将该值分配给UILabel.所以这里是我的一些代码(请注意,此函数包含在另一个类中,因此标签不在此函数的范围内):
func readTodayHealthData() -> Int {
var stepCount: Int = 0
func getStepsHealthData() {
let stepsUnit = HKUnit.countUnit()
let sumOption = HKStatisticsOptions.CumulativeSum
let stepsHealthDataQuery = HKStatisticsQuery(quantityType: stepsHealth, quantitySamplePredicate: predicate, options: sumOption) {
query, results, error in
if let sumQuantity = results?.sumQuantity() {
dispatch_async(dispatch_get_main_queue(), {
stepCount = sumQuantity.doubleValueForUnit(stepsUnit) * 2
})
}
}
healthKitStore?.executeQuery(stepsHealthDataQuery)
}
return stepCount
}
//Set UILabel Value
//**This code is in my View Controller which is in a separate class as a result this label …
Run Code Online (Sandbox Code Playgroud) 如何NSNotification
延迟发布.
我想到的一个解决方案: - 在performSelectorAfterDelay
方法中发布通知.对此有没有更好的解决方案.
被NSNotificationQueue可以帮助我实现这一目标?
在过去,我曾经遇到过类似的问题,即UIAlertController
在UIAlertController
取消后,UI线程上总是存在滞后。
我现在的问题是,如果用户单击“确定”,我想执行顺序检查,如果按下UIAlertAction
“取消”,UIAlertAction
则什么也不会发生。
这是我的代码:
// create the uialertcontroller to display
let alert = UIAlertController(title: "Do you need help?",
message: "Would you like to look for a consultant?",
preferredStyle: .alert)
// add buttons
let okay = UIAlertAction(title: "Yes, please.", style: .default, handler: {_ in
self.performSegue(withIdentifier: "segue", sender: nil)
})
let no = UIAlertAction(title: "No, I'm okay.", style: .cancel, handler: nil)
alert.addAction(okay)
alert.addAction(no)
self.present(alert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
当前正在发生的是,当我点击“确定”时,segue正在执行,但是我只能看到过渡的最后时刻(即,动画在UIAlertController
被关闭时开始播放)。
一旦UIAlertController
解散,我如何使segue开始?
注意-如果有其他方法,我宁愿不要以骇人的方式解决此问题,例如在固定的延迟后执行segue。 …
我已经阅读了这个答案,我不相信它有我想要的东西,但我是初学者,我很高兴有人在这个链接中指出答案:dispatch_after - 迅捷的GCD?
我的目标:设置一个功能,每天在用户的时区(或系统的时区)上午9点运行.
我已经非常简单地使用GCD来延迟一个函数如下(它工作得非常好):
var dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(10 * Double(NSEC_PER_SEC)))
dispatch_after(dispatchTime, dispatch_get_main_queue(), {
let alert = UIAlertView()
alert.title = "Foo Alert"
alert.message = "foo."
alert.addButtonWithTitle("OK")
alert.show()
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
});
Run Code Online (Sandbox Code Playgroud)
所以我想像我上面提到的那样每天开火,但是我坚持的第一件事是将DISPATCH_TIME_NOW换成时区相关值?我是否甚至需要考虑时区或者只是用军队09:00替换DISPATCH_TIME_NOW就足够了?
此外,任何关于总体目标的建议,每天同一时间安排起火功能都将非常感激.
我也没有结婚使用GCD这个目标,但它是我遇到最多的搜索.
我有这个,但似乎过于复杂:
var connectionTimer = MZTimerLabel(timerType: MZTimerLabelTypeTimer)
connectionTimer.delegate = self
connectionTimer.tag = 0
connectionTimer.setCountDownTime(5)
connectionTimer.start()
func timerLabel(timerLabel: MZTimerLabel!, finshedCountDownTimerWithTime countTime: NSTimeInterval) {
self.callFinished()
}
Run Code Online (Sandbox Code Playgroud) Xcode有一个GCD:Dispatch After Objective-C的代码片段:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(<#delayInSeconds#> * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
<#code to be executed after a specified delay#>
});
Run Code Online (Sandbox Code Playgroud)
Swift的等效代码片段是什么?
我试图在用户按下按钮时使用loadnibnamed加载一个名为"AddProgramView"的xib字段.我在故事板中创建了xib,在xib视图中放置了各种子视图,并将出口连接到名为AddProgramView的相应swift文件.当我加载xib时,没有任何子视图出现.他们都失踪了.救命?这是我的代码
在我的主视图控制器中:
@IBAction func addProgram(sender: UIButton) {
if let _ = addProgramView {
addProgramView!.frame = CGRectMake(0, self.window!.frame.origin.y + self.window!.frame.height, self.window!.frame.width, CGFloat(325))
}
else {
let addProgramViews = NSBundle.mainBundle().loadNibNamed("AddProgramView", owner: self, options: nil)
addProgramView = addProgramViews.first as? AddProgramView
addProgramView!.frame = CGRectMake(0, self.window!.frame.origin.y + self.window!.frame.height, self.window!.frame.width, CGFloat(325))
window?.addSubview(addProgramView!)
}
window?.bringSubviewToFront(addProgramView!)
UIView.animateWithDuration(0.3, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.addProgramView!.frame.origin.y -= CGFloat(325)
}, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
AddProgramView.swift文件:
class AddProgramView: UIView {
@IBOutlet weak var nameField: UITextField!
@IBOutlet weak var cancelButton: UIButton!
@IBOutlet weak var …
Run Code Online (Sandbox Code Playgroud) 我想让我的UILabel以延迟的顺序出现.因此一个接一个.当我使用alpha值使它们淡入时,下面的代码可以正常工作,但是当我使用UILabels的.hidden属性时它不会做我想要它做的事情.
代码使我的UILabel同时出现而不是sum1TimeLabel在5秒后出现,sum2TimeLabel在30秒后出现秒,最后sum3TimeLabel出现在60秒后出现.我究竟做错了什么?
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
UIView.animateWithDuration(5.0, animations: {
self.sum1TimeLabel!.hidden = false;
})
UIView.animateWithDuration(30.0, animations: {
self.sum2TimeLabel!.hidden = false;
})
UIView.animateWithDuration(60.0, animations: {
self.sum3TimeLabel!.hidden = false;
})
}
Run Code Online (Sandbox Code Playgroud) 我有一个NSTimer对象如下:
var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "updateTimer", userInfo: nil, repeats: true)
Run Code Online (Sandbox Code Playgroud)
我想把超时给我的计时器.也许你知道android中的postdelayed方法.我想要同样的东西的快速版本.我怎样才能做到这一点 ?
当我使用dispatch_after方法产生延迟时,它永远不会在它之后执行代码.我需要返回一个数组,但它总是跳过它.
这是matt的延迟方法:
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}
Run Code Online (Sandbox Code Playgroud)
以下是发生错误的方法:
func rollDice() -> Array<Int> {
var diceArray = [Int]()
let timerTime:NSTimeInterval = 0.3
delay(timerTime) {
//my code
}
return diceArray //NEVER GETS HERE
}
Run Code Online (Sandbox Code Playgroud)