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 t开发者_如何学Pythonap (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--;
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论