简体   繁体   中英

add a countdown, display the time and fire an action

Hi I am a beginner in programming

I have already created a tapping application, displaying the tap count after pressing the result button

I want to add a NSTimer, counting 30 second after the first tap (after the tap button was pressed for the first time).

at the same time, displaying the time count down on a label (UILabel timeLabel)

and after 30 second, the tap count will restart to 0.

Please kindly tell me if I need to post anything other than the following, Thanks!!

Here is my .h file

#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>
#import <AudioToolbox/AudioToolbox.h>

@class Player;
@interface Tapping2ViewController : UIViewController 
<AVAudioPlayerDelegate>

{
    Player *aPlayer;
    IBOutlet UILabel *timerLabel;
    IBOutlet UILabel *resultLabel;
    AVAudioPlayer *buttonPlayer;
    NSTimer *lv1Timer;
    NSInteger *counter1;

}
- (IBAction)addTap:(id)sender;
- (IBAction)getResult:(id)sender;

-(void)restartTapCount;
-(void)start;

@property (retain) NSTimer *lv1Timer;
@property (nonatomic, retain) IBOutlet UILabel *timerLabel;

@end

and my .M file

#import "Tapping2ViewController.h"
#import "Player.h"


@implementation Tapping2ViewController

@synthesize lv1Timer;
@synthesize timerLabel;

- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil
                bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}


- (void)dealloc
{
    [resultLabel release];
    [lv1Timer release];
    [aPlayer release];
    [timerLabel release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle


- (void)viewDidLoad
{
    aPlayer = [[Player alloc] init];
    [super viewDidLoad];
}



- (IBAction)addTap:(id)sender 

{

    //呢到係設定聲音, 首先要用NSSTRING 去 SET 左條路徑先
    NSString *buttonFile = [[NSBundle mainBundle] pathForResource:@"button" ofType:@"wav"];

    //之後再條NSSTRING 轉做NSURL (因為AVPLAYER 只認URL)
    NSURL *buttonFileURL = [NSURL fileURLWithPath:buttonFile];
    NSError *error = nil;

    //設定AUDIO PLAYER 要播邊條 聲音 *記得SET DELEGATE 做自已去執行
    buttonPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:buttonFileURL error:&error]; 
    [buttonPlayer setDelegate:self];

    NSLog(@"Before: %d", aPlayer.tapCount);
    aPlayer.tapCount++;

    //呼叫播放既METHOD
    [buttonPlayer play];


    NSLog(@"After: %d", aPlayer.tapCount);



    /*
    //即時顯示數字
    aPlayer.result = aPlayer.tapCount;

    NSString *sResult = [NSString stringWithFormat:@"%D", aPlayer.result];

    resultLabel.text = sResult;
    */
}



- (IBAction)getResult:(id)sender {

    aPlayer.result = aPlayer.tapCount;

    NSString *aResult = [NSString stringWithFormat:@"%D", aPlayer.result];

    resultLabel.text = aResult;

}


-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event

{
    if (motion == UIEventSubtypeMotionShake) 
    {
        aPlayer.tapCount = 0;
        resultLabel.text = @"0";
    }
}


- (void)viewDidUnload
{

    [resultLabel release];
    resultLabel = nil;
    [timerLabel release];
    timerLabel = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

//下面係PART OF DETECT SHAKE 既METHOD

-(BOOL)canBecomeFirstResponder
{
    return YES;
}


-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self becomeFirstResponder];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [self resignFirstResponder];
    [super viewWillDisappear:animated];
}

//去到呢到都係

@end

Here is how I do it in an app, on start :

count = COUNTDOWN_DURATION;
countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1
                  target:self selector:@selector(countDown)
                  userInfo:nil repeats:YES];

this will call a countDown method every second. Do whatever you want in that countDown method but make sure to stop the NSTimer on completion (and of course to decrement counter):

if (count < 0) {
  [countdownTimer invalidate];
  countdownTimer = nil;
}
...
count--;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM