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.
Related
I am trying to move my collection view vertically 5px at a time using content offset using time interval trigger but it's not animating, my code for that is
timer = Timer.init(timeInterval: 1.0, target: self, selector: #selector(animateScroll), userInfo: nil, repeats: true)
#objc func animateScroll() {
self.previousOffset = self.collectionView.contentOffset.y
self.collectionView.contentOffset.y = self.previousOffset + 5
}
just use method setContentOffset:animated:
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.
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)
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)
}
In my app I have a view, and I want this view to be dismissed everytime it is shown after 8 seconds. In order to do that I created a timer inside view creation function that hides view after firing. It works. But in some cases I want to present two copies of the view I want both be dismissed after time it was created +8 seconds. Issue is that timer works only once - it hides one copy of view, and second remains on screen
My code is
func presentShowStatusShort(text text:String) {
pointsShort = NSBundle.mainBundle().loadNibNamed("helperShowStatusShort", owner: self, options: nil).first! as! helperShowStatusShort
pointsShort?.frame.size.width = self.view.bounds.width
if pointsShortOnScreen {
pointsShort!.frame.origin.y = pointsShort!.frame.origin.y + 39
}
pointsShort?.text.text = "\(text.uppercaseString)"
pointsShort!.dismissButton.addTarget(self, action: "dismissShowStatusShort", forControlEvents: UIControlEvents.TouchUpInside)
var timer = NSTimer.scheduledTimerWithTimeInterval(8, target: self, selector: "dismissShowStatusShort", userInfo: nil, repeats: false)
pointsShortOnScreen = true
view.addSubview(pointsShort!)
}
func dismissShowStatusShort() {
pointsShortOnScreen = false
UIView.animateWithDuration(0.5, animations: {
self.pointsShort?.frame.origin.y = (self.pointsShort?.frame.origin.y)! - 64
}, completion: {
finished in
self.pointsShort?.removeFromSuperview()
})
}
From an OOP point of view you should create a class to help you with this. The class would own the timer and maintain a reference to the view. When the timer expires it calls a callback, passing itself as the parameter, so the controller can get the associated view and dismiss it.
All of the instances of this class would be stored in an array instance variable on the controller so you can add multiple and remove each one once it expires.