I am creating a game application in which I need to set timer of 2 minutes throughout the app screens in objective c.
I am creating it in viewDidLoad but it is creating a new instance every-time the view loads.
Here is the code which I am using :
#interface SomeViewController ()
{
int timerCounter;
NSTimer *timer;
}
#property (strong, nonatomic) IBOutlet UILabel *timerLbl;
#end
#implementation SomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self startCountdown];
}
- (void)startCountdown
{
timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:#selector(countdownTimer:)
userInfo:nil
repeats:YES];
}
- (void)countdownTimer:(NSTimer *)timer
{
timerCounter--;
int minutes = timerCounter / 60;
int seconds = timerCounter % 60;
NSString *string = [NSString stringWithFormat:#"%02d", minutes];
NSString *string2 = [NSString stringWithFormat:#"%02d", seconds];
NSString *timeTotal = [string stringByAppendingString:#":"];
NSString *timeTotal2 = [timeTotal stringByAppendingString:string2];
_timerLbl.text = timeTotal2;
if (timerCounter <= 0) {
[timer invalidate];
}
}
You need to invalidate it whenever the VC deallocates
- (void)dealloc {
[timer invalidate];
}
//
The second VC may look like this
#import "ggViewController.h"
NSInteger timerCounter = 120; // declared global to hold the value
#interface ggViewController ()
{
NSTimer*timer;
}
#end
#implementation ggViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// instead of using selector use this inline callback
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:true block:^(NSTimer * timer) {
timerCounter--;
int minutes = timerCounter / 60;
int seconds = timerCounter % 60;
NSString *string = [NSString stringWithFormat:#"%02d", minutes];
NSString *string2 = [NSString stringWithFormat:#"%02d", seconds];
NSString *timeTotal = [string stringByAppendingString:#":"];
NSString *timeTotal2 = [timeTotal stringByAppendingString:string2];
_timerLbl.text = timeTotal2;
if (timerCounter <= 0) {
[timer invalidate];
}
}];
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
[timer invalidate];
}
- (IBAction)gg:(id)sender {
[self dismissViewControllerAnimated:true completion:nil];
}
#end
Related
The app is a music player, and I need to show a trackTimeLabel that updates the playback duration during the song.
So, update the label every second with the playback track time duration.
Right now I'm doing a hack where I am just counting seconds that have nothing to do with the song, but that's not a good solution:
- (void)viewWillAppear:(BOOL)animated {
currentItem = [musicPlayer nowPlayingItem];
_titleLabel.text = [self currentItemValue:MPMediaItemPropertyTitle];
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:#selector(timerDidTick:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}
-(void) timerDidTick:(NSTimer*) theTimer{
long currentPlaybackTime = musicPlayer.currentPlaybackTime;
int currentHours = (int)(currentPlaybackTime / 3600);
int currentMinutes = (int)((currentPlaybackTime / 60) - currentHours*60); // Whole minutes
int currentSeconds = (currentPlaybackTime % 60);
_trackTimeLabel.text = [NSString stringWithFormat:#"%i:%02d:%02d", currentHours, currentMinutes, currentSeconds];
}
Apple has a MPMediaItem class which I can get a MPMediaItemPropertyPlaybackDuration for the track, but I can't seem to get anything working with that.
Step 1
In your ViewController.h
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#interface ViewController : UIViewController
{
AVAudioPlayer * player;
}
#property (weak, nonatomic) IBOutlet UISlider *seekSlider;
#property (weak, nonatomic) IBOutlet UILabel *lbl;
Step 2
In your ViewController.m
- (void)viewDidLoad {
NSURL * fileURL = [[NSBundle mainBundle] URLForResource:#"01 - Saturday Saturday - DownloadMing.SE" withExtension:#"mp3"];
NSError * error = nil;
player = [[AVAudioPlayer alloc]initWithContentsOfURL:fileURL error:&error];
if(error)
NSLog(#"Error : %# ", error);
[player prepareToPlay];
player.volume = 0.5;
self.seekSlider.maximumValue = player.duration;
}
-(void)timerMethod:(NSTimer *)timer
{
float progress = player.currentTime;
// if(!self.seekSlider.isFocused)
self.seekSlider.value = progress;
_lbl.text = [NSString stringWithFormat:#"%.f:%.2d", (progress / 60), ((int)progress % 60 )];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)play_pause_clicked:(id)sender {
if(player.playing)
{
[player pause];
[(UIButton*)sender setTitle:#"Play" forState:UIControlStateNormal];
}
else
{
[player play];
[(UIButton*)sender setTitle:#"Pause" forState:UIControlStateNormal];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(timerMethod:) userInfo:nil repeats:YES];
}
}
- (IBAction)seekPlayer:(id)sender {
player.currentTime = [(UISlider*)sender value];
}
- (IBAction)changeVolume:(id)sender {
player.volume = [(UISlider*)sender value];
}
This is perfect working code for me... Try this, I hope, this will help you :)
I have a small sample project i'm using to figure out how to implement this on my main project. This simple project has 2 VC's both with segues to each other.
On the initial VC is a button which leads to the TimerVC (both using the same class).
The TimerVC has a button and a label. When the button is pressed the label will increase by 1 every second.
If the timer is on and I segue back to the initial VC and then to the TimerVC the timer continues but the label stops updating.
How can I keep the label updating? The timer keeps going in the back-end but once the segue happens the label stops updating.
EDIT: Code provided below. The Timer is also more complex to represent a little of what I'm trying to do.
VC.H
NSTimer * timer;
NSTimer * updateTimer;
#property (weak, nonatomic) IBOutlet UIButton *idleOutler;
- (IBAction)idleAttack:(id)sender;
VC.M
int enemy001Hp = 100;
int deadEnemy = NO;
int noHealth = 0;
bool enemy001Active = NO;
- (void) enemy1 {
enemy001Active = YES;
self.enemyHpLabel.text = [NSString stringWithFormat:#"%i", enemy001Hp];
}
- (void) enemyDamageTimer {
if (enemy001Active == YES) {
enemy001Hp -= 50;
self.enemyHpLabel.text = [NSString stringWithFormat:#"%i",enemy001Hp];
}
}
- (void) updateLabelTimer {
if (enemy001Active == YES) {
self.enemyHpLabel.text = [NSString stringWithFormat:#"%i", enemy001Hp];
}
}
- (void) stopTimer {
[timer invalidate];
timer = nil;
self.enemyHpLabel.text = [NSString stringWithFormat:#"0"];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self enemy1];
if (enemy001Active) {
self.enemyHpLabel.text = [NSString stringWithFormat:#"%i", enemy001Hp];
} else if (enemy002Active == YES) {
self.enemyHpLabel.text = [NSString stringWithFormat:#"%i", enemy002Hp];
}
}
- (IBAction)idleAttack:(id)sender {
idleOn = YES;
self.idleOutler.hidden = YES;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(enemyDamageTimer) userInfo:nil repeats:YES];
updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(updateLabelTimer) userInfo:nil repeats:YES];
if (enemy001Active == YES) {
if (enemy001Hp <= 0) {
[self enemy2];
enemy001Active = NO;
enemy002Active = YES;
}
} else if (enemy002Active == YES) {
if (enemy002Hp <= 0) {
self.enemyHpLabel.text = [NSString stringWithFormat:#"0"];
enemy002Hp = 0;
enemy002Active = NO;
[self stopTimer];
}
}
}
I've been searching for a simple way to show the progress of playing an MP3 in a UIProgressView. I have a feeling it's going to involve an NSTimer object but I'm not sure how to implement it.
Here's what I have so far.
#import "ViewController.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UISlider *VolumeLevel;
#property (weak, nonatomic) IBOutlet UIProgressView *progressBar;
#end
#implementation ViewController
AVAudioPlayer *player;
- (IBAction)play:(id)sender {
[player play];
}
- (IBAction)pause:(id)sender {
[player stop];
}
- (IBAction)volume:(id)sender {
player.volume = _VolumeLevel.value;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *songURL =[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"Gregory Is Here" ofType:#"mp3"]];
player =[[AVAudioPlayer alloc]initWithContentsOfURL:songURL error:nil];
player.volume = 0.5;
// Do any additional setup after loading the view, typically from a nib.
}
Using a NSTImer, you can do :
- (IBAction)play:(id)sender {
[player play];
myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(updateProgressBar) userInfo:nil repeats:YES];
}
- (IBAction)pause:(id)sender {
[player stop];
[myTimer invalidate];
}
- (void)updateProgressBar {
float progress = player.currentTime / player.duration;
[myProgressBar setProgress:progress animated:YES];
}
You can change the timer timeInterval parameter to update the progress bar more or less frequently (1.0 is for 1 second).
-(void) setPlayProgress
{
if (player.isPlaying) {
slider.value=abs(player.currentTime);
NSString *duration=[self getTimeStrFromInterval:player.duration-player.currentTime];
lblDuration.text=duration;
}
[self performSelector:#selector(setPlayProgress) withObject:nil afterDelay:0.1];
}
// Show time
-(NSString *) getTimeStrFromInterval:(NSInteger) timeSinceStart{
int seconds = timeSinceStart;
int minutes = seconds/60;
int hours = minutes/60;
minutes = minutes%60;
seconds = seconds%60;
NSString *strHours;
NSString *strMinutes;
NSString *strSeconds;
strHours = [NSString stringWithFormat:#"%d",hours];
strMinutes = [NSString stringWithFormat:#"%d",minutes];
strSeconds = [NSString stringWithFormat:#"%d",seconds];
if (hours<10) {
strHours = [NSString stringWithFormat:#"0%d",hours];
}
if (minutes<10) {
strMinutes = [NSString stringWithFormat:#"0%d",minutes];
}
if (seconds<10) {
strSeconds = [NSString stringWithFormat:#"0%d",seconds];
}
NSString* intervalString;
if ([strHours intValue]>0) {
intervalString = [NSString stringWithFormat:#"%#:%#:%#",strHours,strMinutes,strSeconds];
}
else{
intervalString = [NSString stringWithFormat:#"%#:%#",strMinutes,strSeconds];
}
return intervalString;
}
I'm trying to display a set of numbers on a UILabel sequentially.
For example: I have an array with 5 numbers and each time I want one number from the array to be displayed for 2 seconds on the screen then disappear and then the next number…
This is what i have done so far, but it is not quite what I want. The code below just displays the last number in the array for 2 seconds and then disappears.
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray* myArray = [NSMutableArray array];
for (int i=0; i<5; i++) {
NSNumber *temp;
temp = [myArray objectAtIndex:i];
[self displayNumber:temp];
[myLabel setHidden:NO];
}
}
-(void) displayNumber: (NSNumber *) myNumber{
myLabel.text = [NSString stringWithFormat:#"%#", myNumber];
NSTimer *myTimer;
myTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(hideLabel) userInfo:Nil repeats:NO];
}
-(void) hideLabel{
[myLabel setHidden:YES];
}
I thought about having 5 functions and each gets called sequentially after 2 seconds, but that is very inefficient.
Are there any other ways I can do this?
Change this method
Hope it will works for you what you want
1) Define
NSMutableArray* myArray , int repeatCount; and NSTimer *myTimer in .h file and set its property;
2)
-(void)viewDidLoad
{
[super viewDidLoad];
//This will initialize your timer
myTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(showLabel) userInfo:Nil repeats:NO];
//This will trigger your timer
[myTimer fire];
//Your timer will run in a loop
[[NSRunLoop currentRunLoop] addTimer:myTimer forMode:NSDefaultRunLoopMode];
myArray = [NSMutableArray array];
}
-(void) displayNumber: (NSNumber *) myNumber{
myLabel.text = [NSString stringWithFormat:#"%#", myNumber];
}
-(void) showLabel{
if(repeatCount >= [myArray count])
{
repeatCount = 0;
}
NSNumber *temp = [myArray objectAtIndex:repeatCount];
repeatCount ++;
[self displayNumber:temp];
[myLabel setHidden:NO];
}
I think, that you can use timer with counter.
Create 3 properties:
#property (nonatomic, assign) int counter;
#property (nonatomic, strong) NSArray *array;
#property (nonatomic, strong) NSTimer *timer;
Set them in viewDidLoad: and start timer(with repetition):
- (void)viewDidLoad {
[super viewDidLoad];
_counter = 0;
_array = #[#0, #1, #2, #3, #4, #5];
_timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(nextLabel) userInfo:Nil repeats:YES];
}
Update label:
- (void)nextLabel{
if (_counter == [_array count]) {
//stop in the end of array
[_timer invalidate];
return;
}
[_label setText:[NSString stringWithFormat:#"%#", _array[_counter]]];
_counter++;
}
Pass the array with NSNumber instances to the below method
- (void)updateLabelWithArray:(NSMutableArray *)array
{
if ([array count] != 0)
{
myLabel.text = [NSString stringWithFormat:#"%#", [array firstObject]];
[array removeObjectAtIndex:0];
[self performSelector:#selector(updateLabelWithArray:)
withObject:array
afterDelay:2.0f];
}
}
This should do.
Try this:
// Initialize the timer when you want to change the first number
[NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:#selector(changeNumber:) userInfo:#{#"currentNumber" : [NSNumber numberWithInt:0]} repeats:NO];
-(void)changeNumber:(NSTimer *)timer
{
NSDictionary * dictionary = [timer userInfo];
int number = [(NSNumber *)[dictionary objectForKey:#"currentNumber"] integerValue];
if (number < [numbersArray count])
{
myLabel.text = [NSString stringWithFormat:#"%#", [numbersArray objectAtIndex:number]];
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(changeNumber:) userInfo:#{#"currentNumber" : [NSNumber numberWithInt:number + 1]} repeats:NO];
}
}
try this hope this helps u .. :)
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[super viewDidLoad];
myArray = [[NSMutableArray alloc]initWithObjects:[NSNumber numberWithInt:3],[NSNumber numberWithInt:5],[NSNumber numberWithInt:6],[NSNumber numberWithInt:7],[NSNumber numberWithInt:10], nil]; //your array contains any number you want
index = 0; //showing the numbers from 0th index to last indx
[self.myLabel setHidden:NO];
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(DisplayFortwoSec) userInfo:nil repeats:YES];//call the method reguraly for 2 sec
}
- (void)DisplayFortwoSec
{
[self.myLabel setHidden:YES];
if(index == ([myArray count] - 1))
index = 0;//other stuffs to come out of loop like invalidating the timer
[self.myLabel setHidden:NO];
NSNumber *num = [myArray objectAtIndex:index];
NSString *strNum = [num stringValue];
self.myLabel.text = strNum;
[self.myLabel setHidden:NO];
index ++;
}
I have two classes, Class1 (UIViewController) and Class2 (NSObject). In Class2 I have a property, timerCount, that is increased by 1 every 4th second. I want to set that value to a label in Class1, but it only returns 0. But directly in the Class2 it returns the correct value.
Class2.h
#property (nonatomic) int timerCount;
Class2.m
#synthesize timerCount;
- (void)timerStart {
self.timerCount = 0;
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector:#selector(timerIncrement) userInfo:nil repeats:YES];
[timer fire];
}
- (void)timerIncrement {
self.timerCount = self.timerCount+1;
Class1 *cls1 = [[Class1 alloc] init];
[cls1 updateTimeLabel];
NSLog(#"%#", [NSString stringWithFormat:#"%i", self.timerCount]); //logs correct numbers
}
Class1.m
- (void)updateTimeLabel {
Class2 *cls2 = [[Class2 alloc] init];
NSLog(#"%#", [NSString stringWithFormat:#"%i", cls2.timerCount]); //logs 0 every 4 second
}
So my question is, what could be wrong?
UPDATE
Class2.m
- (void)completeTransaction:(SKPaymentTransaction *)transaction {
NSLog(#"completeTransaction...");
[self provideContentForProductIdentifier:transaction.payment.productIdentifier];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
//initialize the timer and start it
cls1 = [[Class1 alloc] init];
self.timerCount = 0;
timer = [NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector:#selector(timerIncrement) userInfo:nil repeats:YES];
[timer fire];
}
- (void)timerIncrement {
self.timerCount = self.timerCount-1;
[cls1 updateTimeLeftLabel];
NSLog(#"%#", [NSString stringWithFormat:#"IAP: %i", self.timerCount]);
}
Class1.m
- (void)viewDidLoad {
cls2 = [[Class2 alloc] init];
}
- (void)updateTimeLeftLabel
{
NSLog([NSString stringWithFormat:#"%i", cls2.timerCount]);
}
You're creating a new, temporary Class2 object in your updateTimeLabel method. You should be referencing the original Class2 object that is running the timer.
You should not create each time new instance. That is only reason you are receiving 0. Because each new instance point to new address.
Create only single instance and use it.