Need help changing code - iphone

I want to change my NSTimer code to to use applicationSignificantTimeChange
This is what I have right now.
Basically I want to change the timer, instead of changing every 5 seconds for example I want these textLabels to change at 12am every night anyone help me out?
h file
NSTimer *timer;
IBOutlet UILabel *textLabel;
m file
- (void)onTimer {
static int i = 0;
if ( i == 0 ) {
textLabel.text = #"iphone app";
}
else if ( i == 1 ) {
textLabel.text = #" Great App!";
}
else if ( i == 2 ) {
textLabel.text = #" WOW!";
}
else {
textLabel.text = #" great application again!!";
i = -1;
}
i++;
}
timer =[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:#selector(onTimer) userInfo:nil repeats:YES];

Did you check initWithFireDate:interval:target:selector:userInfo:repeats: method of NSTimer? Click here for more details

Related

I need a way to detect last Five clicks time on my UIButton

I have an UIButton that the user must click on it 5 times in three seconds, im trying to implement a method for that, but im getting the correct result if the user clicked on the button 5 times in 3 seconds in a row, if the user clicked once and stopped for 2 seconds for example, the counter take the first click in the calculation.
in a short words, i need a method that detect the last five clicks and know if the clicks were in three second or not...
Here is my old code:
-(void)btnClicked{
counter++;
if (totalTime <=3 && counter==5) {
NSLog(#"My action");
// My action
}}
I know that my code is too simple, so that why i asked you pro's
Try altering this example appropriately:
// somewhere in the initialization - counter is an int, timedOut is a BOOL
counter = 0;
timedOut = NO;
- (void)buttonClicked:(UIButton *)btn
{
if ((++counter >= 5) && !timedOut) {
NSLog(#"User clicked button 5 times within 3 secs");
// for nitpickers
timedOut = NO;
counter = 0;
}
}
// ...
[NSTimer scheduledTimerWithTimeInterval:3.0
target:self
selector:#selector(timedOut)
userInfo:nil
repeats:NO
];
- (void)timedOut
{
timedOut = YES;
}
Simply have an array with the timestamp of the last four clicks, and every time there is a click, check if the previous four are within 3 seconds from the current time. If it is not the case, discard the oldest timestamp and replace with current time, but if it is the case, you got your event and you can clear the array so that they are not used in the next 5-clicks-in-3-seconds event.
Here is 'my version' of H2CO3's code. This should suit your requirement better.
int counter = 0;
BOOL didTimeOut = NO;
- (void)buttonClicked:(UIButton *)button {
counter ++;
if (counter == 1) {
didTimeOut = NO;
[NSTimer scheduledTimerWithTimeInterval:3.0f
target:self
selector:#selector(timedOut)
userInfo:nil
repeats:NO
];
} else {
if ((counter >= 5) && !didTimeOut) {
//Do your action as user clicked 5 times in 3 seconds
counter = 0;
didTimeOut = NO;
}
}
}
- (void)timedOut {
didTimeOut = YES;
}

UIWebView stop auto scrolling

How can I stop my UIWebView from automatically scrolling on the press of a button?
[web_try loadHTMLString:[NSString stringWithFormat:#"<marquee scrollamount='2' direction='up' loop='true' width='278' height='48'> <font size='3' face='HelveticaNeue-Bold' color='#242424'>%#</font></marquee>", option_value] baseURL:final_url];
i think you can use this code to scroll your view and to stop it just works fine for me
-(void)scrollView
{
NSInteger scroll = 1;
NSString* javascript = [NSString stringWithFormat:#"window.scrollBy(0, %d);",scroll];
[self.currentView stringByEvaluatingJavaScriptFromString:javascript];
if(validateTimer)
{
[timer invalidate];
validateTimer = false;
}
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(scrollView) userInfo:nil repeats:YES];
validateTimer = true;
}
-(IBAction)stopScroll
{
[timer invalidate];
validateTimer = false;
hidden.enabled = FALSE;
}
use -
EDIT -
webView.scrollView.scrollEnabled = NO;
webView.scrollView.bounces = NO;
Here, I think you want to stop scrolling the content of web view. Check code. You have used "marquee" tag which is not controllable by objective c code.

a NSTimer in While Loop

Greeting !!
The following codes are now I am doing :
BOOL bJobDone = NO ;
bBrickDropDone = NO ; //global var here !!
NSTimer* timer = nil ;
while(bJobDone==NO)
{
if(timer == nil)
{
timer = [NSTimer scheduledTimerWithTimeInterval:0.3
target:self
selector:#selector(dropBrick:)
userInfo:nil
repeats:YES];
}
if(bBrickDropDone==YES)
{
bBrickDropDone = NO ;
[timer invalidate] ;
timer = nil ;
if([self MarkBrickBomb]==YES)
{
bJobDone = NO ;
[self dealBomb] ;
if([self AllClean]==YES)
{
bJobDone = YES ;
igameidx = igameidx + 1 ;
}
}else
{
bJobDone = YES ;
}
}//if(bBrickDropDone==YES)
}//while bJobDone==NO
As you can see , the timer call dropBrick function once for 0.3 second ,
if bBrickDropDone is finally = YES (dropBrick function modify this to YES if something happen)
it will process another function MarkBrickBomb and goes on until bJobDone=YES,break out of loop!!
I think my code is lousy , I should not check bBrickDropDone flag in the while loop,
because it is not efficient , also cost resource a lot !!
I need a timer to get an animated UIImageView switch , buy Still don't like
to check the done flag in while loop(it is not good ,I think) , so what can I do
in this case ? Can I get a hint for this ?
And , sorry for my english !!
The timer can never fire whilst bJobDone == NO, this is because, NSTimers are added to the NSRunLoop and can only fire whilst waiting for an event.

How do you make an count-up timer in Objective-C?

I have looked everywhere but I cannot find out how to do so. I need a simple timer that can count up in milliseconds and can be stopped. (XCode 3.1, Objective-C, iPhone OS Development)
Am I just being daft or is "count-up timer" the same as a stopwatch.
If it is, check out this video tutorial
Here is the sample code which i have done one timer with minute, second, and mili seconds.. So, its helpful for you. And also do it release after not use..
-(void)showTime:(NSTimer *)sender
{
miniSeconds++;
if (miniSeconds == 10)
{
miniSeconds = 0;
seconds++;
if (seconds == 60)
{
seconds = 0;
minutes++;
}
}
timeLabel.text = [NSString stringWithFormat:#"%02i:%02i:%02i",minutes,seconds,miniSeconds];
}
-(void)nextLevelTime
{
[labelTimer invalidate];
miniSeconds = 0;
seconds = 0;
minutes = 0;
}
The nextLevel method use for to stop timer and release all thing..
Use it, I have done with this code..
NSTimer should do it:
Class Reference
Timer Programming Topics
#interface ViewController()
{
UILabel *progress;
NSTimer *timer;
int currMinute;
int currSeconds;
int currHours;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
progress=[[UILabel alloc] initWithFrame:CGRectMake(80, 15, 100, 50)];
progress.textColor=[UIColor redColor];
[progress setText:#"Time : 00:00:00"];
progress.backgroundColor=[UIColor clearColor];
[self.view addSubview:progress];
currMinute=00;
currSeconds=00;
currHours=00;
// Do any additional setup after loading the view, typically from a nib.
}
-(void)start
{
timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(timerFired) userInfo:nil repeats:YES];
}
-(void)timerFired
{
currSeconds+=1;
if(currSeconds==60)
{
currSeconds=0;
currMinute+=1;
if(currMinute==60)
{
currMinute=0;
currHours+=1;
}
}
[_progress setText:[NSString stringWithFormat:#"%#%02d%#%02d%#%02d",#"Time : ",currHours,#":",currMinute,#":",currSeconds]];
if(currMinute==2)//stop at 2 minute
{
[timer invalidate];
}
}

Uilabel effect like in games

Does anyone know how to make a UILabel appear letter by letter like you can see in some games or other.
thanks
Set up an NSTimer to set the text property of the UILabel.
-(void) revealTimer:(NSTimer *)inTimer {
if ( ++mIndex < [mFullString length] ) {
mLabel.text = [mFullString substringToIndex:mIndex];
} else {
mLabel.text = mFullString;
[inTimer invalidate];
}
}
start it up with something like this:
-(void) revealString:(NSString *)inString {
mIndex = 0;
mLabel.text = "";
mFullString = [inString retain];
[NSTimer scheduledTimerWithTimeInterval:0.125 target:self selector:#selector(revealTimer:) userInfo:nil repeats:YES];
}
be sure not to leak mFullString as the above does, and store the timer if you may need to invalidate it.