添加倒计时、显示时间并触发操作

发布于 2024-10-31 03:46:43 字数 3713 浏览 1 评论 0原文

您好,我是编程初学者

我已经创建了一个点击应用程序,在按下结果按钮后显示点击计数

我想添加一个 NSTimer,在第一次点击后计数 30 秒(第一次按下点击按钮后) 。

同时,在标签(UILabel timeLabel)上显示时间倒计时

,30秒后,点击计数将重新开始为0。

请告诉我是否需要发布除以下内容以外的任何内容,谢谢!

这是我的 .h 文件

#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

和 .M 文件

#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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城空念 2024-11-07 03:46:43

以下是我在应用程序启动时执行此操作的方法:

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

这将每秒调用一个 countDown 方法。在该 countDown 方法中执行您想要的任何操作,但请确保在完成时停止 NSTimer (当然还要递减计数器):

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

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--;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文