Can't use NSTimer update UI - ios

In my app, I want to use a NSTimer update my UIButton instance, but my button's title didn't change as the time elapse. My code is here:
-(void)fireTimer{
self.leftTime = 30;
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(timerAction) userInfo:nil repeats:YES];
[self.timer fire];
}
- (void)timerAction
{
self.leftTime--;
NSLog(#"%d current runloop and mode is %#", self.leftTime, [NSRunLoop currentRunLoop].currentMode);
[self.customBtn setTitle:[NSString stringWithFormat:#"leftTime:%d", self.leftTime] forState:UIControlStateNormal];
if(self.leftTime == 0)
{
[self.customBtn setTitle:[NSString stringWithFormat:#leftTime :%d, self.leftTime] forState:UIControlStateNormal];
[self.timer invalidate];
}
}
And the NSLog output is correct, when self.timeLeft == 0, my button can change it's title correctly.
Can someone tell me what's the problem? thanks!

I find out the problem, I use KVO observe self.leftTimer, when the timer fire, I set my button.enable = NO, so it can't change the title, and when self.leftTime == 0, my button.enable = YES. My careless. Thanks all.

Related

iOS UISwitch keep sending message every 4 second

I have a chatting app that has a uiswitch. If the Switch button is on, I want the app to send "hi" continuously every 3 seconds even the app is in background mode. I know that I can use NSTimer, but I don't really know how to implement that in this code(This is my first time to develop iOS app). Please help me with this.
My code is :
// Allocate, initialize, and add the automatic button.
_AutomaticSend = [[UISwitch alloc] initWithFrame:CGRectMake([self width] - 50.0, textAreaY + Center(160.0, textAreaHeight), 50.0, 50.0)];
[_AutomaticSend addTarget:self action:#selector(changeSwitch:) forControlEvents:UIControlEventValueChanged];
and
// Switch action
//NSUInteger counter = 0;
- (void)changeSwitch:(id)sender{
if([sender isOn]){
for (int a=1; a<=300; a++)
{
//[self performSelector:#selector(changeSwitch:) withObject:_textField afterDelay:70.0];
[sender setOn:YES animated:YES];
[_textField setText:#"hi"];
NSString * text = [_textField text];
// Update status.
[[TSNAppContext singleton] updateStatus:text];
// Add the status to the bubble.
[self appendLocalPeerTableViewCellWithMessage:text];
}
// NSLog(#"Switch is ON");
} else{
NSLog(#"Switch is OFF");
}
}
Now, the app is showing all "hi" after all 300 "hi" is ready to show. But I want it to send it one by one continuously.
Define a NSTimer instance and a counter in your view controller:
#property (nonatomic, strong) NSTimer *timer;
#property (nonatomic, assign) NSInteger counter;
Implement the method when timer is fired:
- (void)timerAction:(id)sender {
[_textField setText:#"hi"];
NSString * text = [_textField text];
// Update status.
[[TSNAppContext singleton] updateStatus:text];
// Add the status to the bubble.
[self appendLocalPeerTableViewCellWithMessage:text];
// stop the timer after sending for 300 times
self.counter += 1;
if (self.counter >= 300) {
[self.timer invalidate];
self.timer = nil;
}
}
Starts the timer when switch is on:
- (void)changeSwitch:(id)sender{
if ([sender isOn]) {
self.counter = 0;
self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:#selector(timerAction:) userInfo:nil repeats:YES];
} else {
// kill the timer when switch is off
[self.timer invalidate];
self.timer = nil;
}
}
However, you can't make the timer to continue working indefinitely after your app is entering background (with some exceptions: VoIP, GPS applications, etc.). Please refer to the official document Background Execution.
//Nstimer *timer in .h
In your UISwitch Method Write like this
- (void)changeSwitch:(id)sender{
if([sender isOn]){
timer=[NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:#selector(targetMethod)
userInfo:nil
repeats:NO];
}
else
{
[timer invalidate];
}
}
targetMethod Like this
-(void)targetMethod
{
[sender setOn:YES animated:YES];
[_textField setText:#"hi"];
NSString * text = [_textField text];
// Update status.
[[TSNAppContext singleton] updateStatus:text];
// Add the status to the bubble.
[self appendLocalPeerTableViewCellWithMessage:text];
}
Use NSTimer: Documentation of NSTimer
Declare property of NSTimer in .h file:
#property (retain,nonatomic) NSTimer *myTimer;
- (void)changeSwitch:(id)sender
{
if([sender isOn]){
myTimer = [NSTimer scheduledTimerWithTimeInterval: 4 target: self
selector: #selector(AddMessage:) userInfo: nil repeats: YES];
} else{
[self.timer invalidate];
}
}
After each 4 second, timer will call the below function:
-(void) AddMessage:(NSTimer*) timer
{
//Add Message
}

NSTimer stops my image from moving

When I hold down on a button my image will move to a direction.
The game objective is that they are going to move image to hit another image, and to do that I'm using an NSTimer.
Then they also have another NSTimer because they only have 15 seconds on them to hit the other image. If they don't the game will [self gameover] but, the NSTimer that counts the 15 seconds is started and I move the image the image moves a bit and then it goes back to center again.
When I delete the countdown timer, the image does not go back to center after I stop pressing the button but it stops on its current position.
And that's what I want it to do while I'm using the countdown timer as well.
Code:
-(void)goLeft
{
ball.center = CGPointMake(ball.center.x -5, ball.center.y);
}
-(IBAction)left
{
goLeft = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:#selector(goLeft) userInfo:nil repeats:YES];
if (goLeft == nil) goLeft = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:#selector(goLeft) userInfo:nil repeats:YES];
}
-(IBAction)stopLeft
{
[goLeft invalidate];
goLeft = nil;
}
//And heres the timer :
-(IBAction)doCountdown: (id)sender;{
if (countdownTimer)
return;
remainingTicks = 25;
[self updateLabel];
countdownTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector: #selector(handleTimerTick) userInfo: nil repeats: YES];
}
-(void)handleTimerTick
{
remainingTicks--;
[self updateLabel];
if (remainingTicks <= 0) {
[countdownTimer invalidate];
countdownTimer = nil;
}
}
-(void)updateLabel
{
theLabel.text = [[NSNumber numberWithUnsignedInt: remainingTicks] stringValue];
}

How do you move a UIImage when you hold a button?

I declared 2 IBActions "rightButtonPress" and "leftButtonPress" I linked them to 2 IBOutlets and ran a block of code in the IBactions:
-(void)leftButtonPress:(id)sender{
PlayerSprite.center = CGPointMake(PlayerSprite.center.x - 1, PlayerSprite.center.y);
}
-(void)rightButtonPress:(id)sender{
PlayerSprite.center = CGPointMake(PlayerSprite.center.x + 1, PlayerSprite.center.y);
}
and i also created 2 timers:
- (void)viewDidLoad
{
TouchLeftCheck = [NSTimer scheduledTimerWithTimeInterval:.01
target:self
selector:#selector(leftButtonPress:)
userInfo:nil
repeats:YES];
TouchRightCheck = [NSTimer scheduledTimerWithTimeInterval:.01
target:self
selector:#selector(rightButtonPress:)
userInfo:nil
repeats:YES];
BarrelEntrance = [NSTimer scheduledTimerWithTimeInterval:.01
target:self
selector:#selector(barrelStartDown)
userInfo:nil
repeats:YES];
[self frontBarrelAnimation];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
it all works well but when i linked it up to two IBObject Buttons something didn't work, when i linked my IBActions to my Objects(buttons), with "Touch Down" it didn't work. When i was in the ios simulator it only moved my object i time when i held and then it stopped, if you touch it again then the object moves one more time. But is there a link option that will allow me to make it do a smooth animation across the screen when i hold it down, and then stop when i let go? Thanks.
There are three components here:
A timer for animation
The start-touch event
The end-touch event
I recommend using an NSTimer to animate the view while the button is being pressed. This timer will be initialized and started when the button is pressed (touchDown:) and invalidated/nullified when the button is released (touchUpInside: and touchUpOutside:).
So to start off, bind the touch events to the button. (I assume that you are creating these buttons in interface builder, which is fine. If so, you can skip the initialization and just go straight to the addTarget: methods):
UIButton* leftButton = [[UIButton alloc] initWithFrame:...];
[leftButton addTarget:self action:#selector(startLeftAnimation) forControlEvents:UIControlEventTouchDown];
[leftButton addTarget:self action:#selector(stopAnimation) forControlEvents:UIControlEventTouchUpInside];
[leftButton addTarget:self action:#selector(stopAnimation) forControlEvents:UIControlEventTouchUpOutside];
Next, declare an instance variable timer:
#interface MyViewController
{
NSTimer* animationTimer;
}
Create the methods for the button events:
- (void)startLeftAnimation
{
animationTimer = [NSTimer scheduledTimerWithInterval: 0.02 target:self selector:#selector(animateLeft) userInfo:nil repeats:YES];
}
- (void)stopAnimation
{
if(animationTimer != nil)
{
[animationTimer invalidate];
animationTimer = nil;
}
}
Finally, create the animation method
- (void)animateLeft
{
PlayerSprite.center = CGPointMake(PlayerSprite.center.x - 1, PlayerSprite.center.y);
}
Rinse and repeat for the right button animation.

NSTimer automatically increases when I change view controller

In my viewController I inserted a timer for a calculation of numbers taken directly from my database of Parse.com.
The timer is working correctly, were inserted in viewDidAppear and in ViewDidDisappear in such a way as to lock the sequence when changing view controller.
The problem I have is that when I change the view controller with the Push the timer does not stop you give an example:
I open the application and the calculation is done correctly with the animation of NSTimer.
Change View, and then I go back with the back button to view where the timer at this point I see that the numbers contiunano to increase every time I change view and go back ...
Can you explain where I'm wrong?
-(void)viewDidAppear:(BOOL)animated {
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
[self.navigationController.navigationBar setBackgroundImage:[UIImage new]forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
[self.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]];
self.title = NSLocalizedString(#"", nil);
[self animatelayertopoint:0];
[self AnimationMenuView:600];
[self QueryForViewPrincipal];
[self QueryForCollectionView];
[self ShowBadgeNumber];
[self Timer];
[collectionView reloadData];
}
-(void)Timer{
timer = [NSTimer scheduledTimerWithTimeInterval:0.012 target:self selector:#selector(CalcoloMediadiLaurea) userInfo:nil repeats:YES];
}
-(void) CalcoloMediadiLaurea{
PFUser *CurrentUser = [PFUser currentUser];
NSNumber *NumeroUndici = [NSNumber numberWithInt:11];
NSNumber *NumeroTre = [NSNumber numberWithInt:3];
NSNumber *ValoreMediaPonderata = [CurrentUser objectForKey:FF_USER_MEDIA_PONDERATA];
int FFValoreTre = [NumeroTre intValue];
int FFValoreUndici = [NumeroUndici intValue];
double MediaPonderata = [ValoreMediaPonderata doubleValue];
double RisultatoMoltiplicazioneValoriPonderataUndici;
RisultatoMoltiplicazioneValoriPonderataUndici = MediaPonderata * FFValoreUndici;
double RisultatoMediaLaurea;
RisultatoMediaLaurea = RisultatoMoltiplicazioneValoriPonderataUndici / FFValoreTre;
CalcoloMediaLaureaLabel.text = [NSString stringWithFormat:#"%.1f", FFTickValoreMediaLaurea ];
FFTickValoreMediaLaurea++;
if(FFTickValoreMediaLaurea > RisultatoMediaLaurea){
[timer invalidate];
timer = nil;
}
}
-(void)viewDidDisappear:(BOOL)animated {
[self animatelayertopoint:0];
[self AnimationMenuView:600];
[self CalcoloMediadiLaurea];
}
The timer get rescheduled again on view did appear without getting invalidated to the previous one.You should make instance of timer in .h and then hold the reference of timer in it .And on view did appear when you are scheduling the timer just invalidate it and then schedule it again.
if([self.timer isValid])
{
[self.timer invalidate];
self.timer=nil;
self.timer=[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:#selector(CalcoloMediadiLaurea:) userInfo:nil repeats:YES];
}
This worked for me which is along the lines of the above answer but a little more explicitly stated.
- (void)viewWillAppear:(BOOL)animated
{
NSTimeInterval secondsBetween = [endDate timeIntervalSinceDate:currentTime];
secondsLeft = secondsBetween;
[self countdownTimer];
}
- (void)viewDidDisappear:(BOOL)animated
{
[self.timer invalidate];
self.timer = nil;
}
//Initiates timer
- (void)countdownTimer
{
[self.timer invalidate];
self.timer = nil;
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(updateCounter:) userInfo:nil repeats:YES];
}
It's because you are creating new timers without invalidating old ones. The timers don't get invalidated automatically, you have to do it yourself. So if U go on second view and return before your invalidate condition is true you will create new timer and have two of them :)... And so on.

How to make a button appear and disappear throughout a timer

I have this app which has a timer in it and I want to be able to display a button at random points in that timer. Sort Of like a game setting where you have to get the button to get more time before it disappears. I already have some code , but the problem is that the timer that i already have is being messed up ( i can tell because it is linked to a label. It messed up because it goes down by 2 instead of 1 and this only happens when i have the code to make the button appear and disappear with the timer.
Here is the code to my timer that is being messed up:
-(IBAction)Ready:(id)sender {
[self performSelector:#selector(TimerDelay) withObject:nil afterDelay:1.0];
[self performSelector:#selector(randomTimer) withObject:nil afterDelay:0.5];
}
-(void)TimerDelay {
MainInt = 36;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(countDownDuration)
userInfo:nil
repeats:YES];
}
Here is the code to the button:
-(IBAction)Ready:(id)sender { //the same ready action as above
[self performSelector:#selector(TimerDelay) withObject:nil afterDelay:1.0];
[self performSelector:#selector(randomTimer) withObject:nil afterDelay:0.5];
}
-(void)TimerDelay {
MainInt = 36;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(countDownDuration)
userInfo:nil
repeats:YES];
}
- (void)randomTimer {
NSInteger randomTime = arc4random_uniform(0); //Some number between 0 and 9
timer = [NSTimer scheduledTimerWithTimeInterval:randomTime
target:self
selector:#selector(showButton)
userInfo:nil
repeats:NO];
}
- (void)showButton {
if(self.plus5.hidden == YES) {
self.plus5.hidden = NO;
}
else {
self.plus5.hidden = YES;
}
[self TimerDelay]; //Call random timer function again to run this method at a future random time
}
You are calling "randomTimer" and "TimerDelay" (this should be "timerDelay") one after the other and scheduling and REscheduling "timer".
I think you should reconsider your architecture, here...
EDIT: Use just one timer and let it manage your button.
- (void)somewhereInYourCode
{
shouldShowButton = YES;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(timeFired)
userInfo:nil
repeats:YES];
}
- (void)timeFired
{
// Here your countdown and...
if (shouldShowButton) {
NSInteger randomTimeInt = arc4random_uniform(9); // as suggested by Bergasms
float randomTimeFloat = randomTimeInt;
[self performSelector:#selector(showButton) withObject:nil afterDelay:randomTimeFloat];
shouldShowButton = NO;
}
}
This way your timer won't (probably) mess up.
This:
arc4random_uniform() will return a uniformly distributed random number
less than upper_bound.
is one of your problems.
NSInteger randomTime = arc4random_uniform(0); //Some number between 0 and 9
needs to be
NSInteger randomTime = arc4random_uniform(9); //Some number between 0 and 9
I cannot say for the rest of your code. But the line you have will always return 0, which means the random timer will always execute instantly.

Resources