Excuse me, if it is a stupid question, but i'm just trying to understand GCD.
I tried to do a simple thing: UITableViewController with one UITableViewCell and UIProgressView on the cell. And this progressView should update every second.
In cellForRowAtIndexPatch I did this
let cell = tableView.dequeueReusableCellWithIdentifier("onlyCell", forIndexPath: indexPath) as! OnlyTableViewCell
cell.runChanges()
return cell
In runChanges function I just create timer
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("doSomething"), userInfo: nil, repeats: true)
And doSomething looks like this:
func doSomething() {
dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_USER_INITIATED.rawValue),0)) {
if self.i >= 100 {
self.timer!.invalidate()
return
}
++self.i
dispatch_async(dispatch_get_main_queue()) {
self.prgrs.progress = Float(self.i)/100.0
print(self.i)
}
}
}
So I thought that it would work all the time, while I interact (maybe) with the UI. And it works quite good while I don't touch it. But when I interact with UI, for example, scroll UITableView and hold my finger(mouse in simulator) on the screen, it freezes.
I thought if now it is 22% and I will scroll it and hold my finger for 10 seconds it maybe should continue from about 32% but it shows 23%.
Sorry for my English and stupidness.
Images and source here.
I had a similar issue with NSTimer, adding your timer to mainRunLoop should fix it.
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("doSomething"), userInfo: nil, repeats: true)
NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)
Related
I am loading a progress view from this file into a different view controller, it is displaying fine but It will not remove. I've tried isHidden, removeFromSuperview(), making the alpha=0, but nothing is working. How can I get rid of it?
func timerAction(){
let progressHUD = ProgressHUD(text: "Taking Reading")
if(timerCounter <= 20){
self.vc!.view.addSubview(progressHUD)
timerCounter += 1
} else {
timer.invalidate()
timerCounter = 0
progressHUD.removeFromSuperview()
}
}
self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.timerAction), userInfo: nil, repeats: true)
Each time the timer is called a new subview is added to the view controller. Add the subview 'progressHUD' just once and update it instead.
Sorry if this is a really basic questions, but I can’t seem to work it out, so I thought I would ask the experts.
I’ve got a timer for my project that counts down and updates a label according to what is stored in an array.
var array : String[]()
var x = 0
#IBAction func playBtnPressed(sender: UIButton)
{
timer = NSTimer.scheduledTimerWithTimeInterval(60, target: self, selector: #selector(PlayVC.update), userInfo: nil, repeats: true)
}
func update()
{
if x < array.count {
let item = array[x]
aLbl.text = array.itemTitle
x += 1
}
}
My problem is that the text is only updated after the first countdown and 60 seconds is a long time to wait lol.
I would actually like the first String in my array to appear as soon as the button is tapped.
Is there a way to set the text at the very beginning of the countdown?
Thank you for your help :)
So you want to update a label every minute. And you also want to update it immediately after the button is pressed. Hopefully I didn't misunderstand the question.
It's actually as easy as adding this line before the timer = NSTimer ... line:
update()
Note that your current code can cause two or more timers to be created and run when the button is pressed more than once. You might not want this.
To stop the timer when the button is pressed a second time, do this:
if timer == nil {
timer = NSTimer.scheduledTimerWithTimeInterval(60, target: self, selector: #selector(PlayVC.update), userInfo: nil, repeats: true)
} else {
timer.invalidate()
timer = nil
}
To do nothing when the button is pressed a second time jus remove the else part.
Hello I'm new here and new to app development but I'm building an app with multiple views and I would like to add a button that will go to the next view but the user will have to wait 3 seconds or so on each view before the button is available to be tapped.
You could perform a method after a delay with reveals or enables your button,
[self performSelector:#selector(yourMethod) withObject:nil afterDelay:3.0];
if you want the delay to happen after button tap then have a look at the below code
let tapGesture = UITapGestureRecognizer(target: self, action: "tapAction")
tapGesture.numberOfTapsRequired = 1
Your_Button_name.addGestureRecognizer(tapGesture)
#IBAction func tapAction()
{
Your_Button_name.disable=true
//Delay function to enable your button
NSTimer.scheduledTimerWithTimeInterval(Your_time_value_delay, target: self, selector: Selector("enablefunc"), userInfo: nil, repeats: false)
// DO SOMETHING WHICH YOU WANT TO DO.....
}
func enablefunc()
{
Your_Button_name.disable=false
}
Hope this helps. You can modify it based on your requirement....
i want to display values 1-10 repeatedly in UILable with animation user should be able to see the fluctuation of the values slowly how can i achieve this functionality ?
You can use :
var timer : NSTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "change_label", userInfo: nil, repeats: true)
func change_label()
{
count = count+1
label.text = String(format: "%d",count)
}
I'm writing a music player using Swift, but when I'm tried to auto scroll the lyric, I found something weird.
I use a NSTimer to trigger the scroll:
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "updateTime", userInfo: nil, repeats: true)
func upadate is:
let offset = lyricScrollView.contentOffset
lyricScrollView.setContentOffset(CGPoint(x: offset.x, y: offset.y + 25), animated: true)
but when I change the time interval to 0.1 like this:
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "updateTime", userInfo: nil, repeats: true)
I found that my scrollview doesn't scroll 10x faster than before, instead, it scrolls a little every 0.1 seconds, not 25 points every 0.1 seconds. If I set animated to false, the problem is gone.
Could someone tell me why this happens?