In UIView I have taken one UIProgressView to show the Progress of the data downloaded from the server and to show those data I have taken an UITableView. The web service is via Polling, so which data is loading, I am loading it into an UITableView. But when user is scrolling the UITableView the ProgressBar paused its progress, but when again I am stopping my manual scroll of UITableView that time again the Progress bar is starting its progress from last paused position.
Code which I have Written:
Taken an outlet:
IBOutlet UIProgressView *pgrsVw;
When I am starting my web service call:
pgrsVw.progress = 0.0;
pgrsVw.hidden=false;
[self performSelectorOnMainThread:#selector(makeMyProgressBarMoving) withObject:nil waitUntilDone:NO];
Now to show the Progress:
- (void)makeMyProgressBarMoving
{
float actual = [pgrsVw progress];
if (actual < 1)
{
pgrsVw.progress = actual + PROGRESSOFDATA;
[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:#selector(makeMyProgressBarMoving) userInfo:nil repeats:NO];
}
else
{
pgrsVw.progress=1.0;
pgrsVw.hidden=true;
}
}
Now when the data downloading is completed:
pgrsVw.progress=1.0;
I have just changed my timer code, Now it is working fine. That mean when data is loading in background, the progress bar is also moving when user is scrolling the tableview.
Changed code:
NSTimer *timer = [NSTimer timerWithTimeInterval:0.10 target:self selector:#selector(makeMyProgressBarMoving) userInfo:nil repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
Related
My ProgressView doesn't update the progress.
I Did this by Interface Builder.
My ProgressView is in a tableViewCell.
//Declared in TableViewCell
#property(nonatomic,retain) IBOutlet UIProgressView *progressview;
and also Synthesized in Cell implementation.
then imported in my viewController
//Declared AVAudioPlayer
AVAudioPlayer *player;
in cellForRowAtIndexpath method :
obj.progressview.progress = 0.0;
in didSelectRowAtIndexPath method :
NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(methodToUpdateProgress) userInfo:nil repeats:YES];
m_timer = [NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:#selector(updateTime:)
userInfo:nil
repeats:YES];
For every 1 second my methodToUpdateProgress is Called
-(void)methodToUpdateProgress{
dispatch_async(dispatch_get_main_queue(), ^{
[obj.progressview setProgress:(float)(player.currentTime/player.duration) animated:YES];
NSLog(#"method");
});
}
I have a button with 2 actions Play and Pause.
Those 2 actions works completely fine.
If I click on a Row in tableview, corresponding song plays, timer starts and for every 1 sec it called my methodToUpdateProgress,but my ProgressView doesn't show any progress.
Help me what should I do ?
I am having a banners array and displaying it in my custom cell. When user clicks on banner, I push a detailViewController and opens in-app browser.
I am changing banners after every 5 seconds. For that, I am using NSTimer to schedule the selector call. Everything works great.... Until, user clicks on banner and come back from detailViewController. When user comes back, NSTimer behaves really weird. It changes first banner after 5 seconds (as assigned) and then next banner is changed after 1 second and so on.
Her is the code I am using:
#pragma mark - User Methods
-(void) resetBannerRotationTimer {
[self.bannerTimer invalidate];
self.bannerTimer = nil;
self.timeInterval = 5.0f;
self.bannerTimer = [NSTimer scheduledTimerWithTimeInterval:self.timeInterval target:self selector:#selector(rotateBanner) userInfo:nil repeats:YES];
self.timeInterval = self.bannerTimer.timeInterval;
[[NSRunLoop currentRunLoop] addTimer:self.bannerTimer forMode:NSRunLoopCommonModes];
}
rotateBanner:
-(void) rotateBanner {
BannerCell *bannerCell = (BannerCell *)[self.dealsTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
[bannerCell updateBanner];
}
In my updateBanner method, I am handeling UIPageControl to change pages. (I don't think that code needs to be posted).
I am calling resetBannerRotationTimer method in viewWillAppear method.
i think you are missing to fire your timer
add this line after timer scheduling
[self.bannerTimer fire];
I've created a simple button game that gives the user a point with every tap of the button. The button randomly appears on screen every 1.5 seconds. I want the game to end after 30 seconds or after 20 random button pop ups. I've been using the code below to have the button randomly pop-up on the screen:
timer = [NSTimer scheduledTimerWithTimeInterval: 1.5 target:self
selector:#selector(moveButton:)
userInfo:nil
repeats:YES];
I've declared the timer in the header file:
NSTimer *timer;
#property (nonatomic, retain) NSTimer *timer;
I've read Apple Docs on Using Timers but fail to fully understand it. I thought maybe I could use:
- (void)countedTimerFireMethod:(NSTimer *)timer{
count ++;
if(count > 20){
[self.timer invalidate];
self.timer = nil;
But it does not work properly. What am I doing wrong? I'm new to objective-C so I'm not that familiar with how things work.
The problem is on your timer method you are passing moveButton method but in below method where you are stopping the timer that method name is different so try this:-
self.timer = [NSTimer
scheduledTimerWithTimeInterval: 1.5 target:self
selector:#selector(moveButton:)
userInfo:nil
repeats:YES];
//just change the method name below
- (void)moveButton:(NSTimer *)timer{
count ++;
if(count > 20){
[self.timer invalidate];
self.timer = nil;}
If you are using new version of Xcode then you don not need to declare
NSTimer *timer;
and when scheduling a timer you can use
self.timer = [NSTimer scheduledTimerWithTimeInterval: 1.5 target:self
selector:#selector(moveButton:)
userInfo:nil
repeats:YES]
instead of
timer = [NSTimer scheduledTimerWithTimeInterval: 1.5 target:self
selector:#selector(moveButton:)
userInfo:nil
repeats:YES]
You are using correct method to stop the timer i.e invalidate
You can also refer the link for more clarification.
Please let me know if you solve this problem through the above code.
I have a real weird behavior of NSTimer object.
I'm trying to call ChangePic method with repeat counts of 2 seconds, But the timer isn't repeating. and i have this problem only in 1 class in the project
I have 5 View Controllers in my App, and the same code works in all classes except this one.
Does anybody has idea how could it be? is it possible that something blocking the timer ?
-BTW, ChangePic Method is called only once, and not repeating.
My Code:
ViewController.h
#property (nonatomic, strong) NSTimer *timer;
ViewController.m
_timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(ChangePic) userInfo:nil repeats:YES];
[_timer fire];
-(void) ChangePic {
NSLog(#"testing");
}
Assign it to the property not the backing variable.
self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(ChangePic) userInfo:nil repeats:YES];
It's difficult to say exactly what your issue is given that there isn't a whole lot of background given. I can tell you that I answered another question where the problem turned out to be the fact that one of the views was blocking the timer. Timers can run on a number of different modes and different scenarios can cause the timer to sometimes be blocked. Try adding the timer to all common modes like this:
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
Also, like #Hejazi said, remove the 'fire' method call. It's not needed on a scheduled timer.
I have an NSTimer
timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:#selector(periodicTimer)
userInfo:nil
repeats:YES];
which does
- (void)periodicTimer
{
NSLog(#"Bang!");
if (timerStart != nil)
[timerLabel setText:[[NSDate date] timeDifference:timerStart]];
}
The problem is that while scrolling a tableview (or doing other tasks) the label doesn't get updated, furthermore, "Bang!" doesn't appear, so I supposed the method doesn't get called.
My question is how to update the label periodically even when the user is playing around with the app interface.
You'll need to add your timer to the UITrackingRunLoopMode to make sure your timer also fires during scrolling.
NSRunLoop *runloop = [NSRunLoop currentRunLoop];
NSTimer *timer = [NSTimer timerWithTimeInterval:0.1 target:self selector:#selector(myTimerAction:) userInfo:nil repeats:YES];
[runloop addTimer:timer forMode:NSRunLoopCommonModes];
[runloop addTimer:timer forMode:UITrackingRunLoopMode];
From:
https://stackoverflow.com/a/1997018/474896
Not sure about this one, but my first guess would be that the main thread on which the interface is being rendered your timer just doesn't get a chance to do anything while its updating the interface.
You could create a new thread with a new run loop for your timer, but that is a bit of an ugly solution maybe. What functionality in your app are you trying to achieve? Maybe we can advise a better strategy than using a timer.