将NSTimer添加到按钮

时间:2012-03-29 06:20:53

标签: iphone objective-c ios xcode nstimer

我已经在网上搜索了没有运气使这个项目与计时器一起工作,每次使用计时器时我都会遇到崩溃的应用程序。

出于测试和学习目的,我构建了一些简单的应用程序。这是一个从屏幕底部发射火箭并​​从屏幕上消失的按钮,以及火箭音效。

我想在按钮上添加一个Timer,这样当按下按钮时,火箭将一直启动,重置和启动,直到我释放按钮。我想我现在唯一的希望就是粘贴我的.h& .m文件,希望有人可以告诉我我需要做什么以及需要为此项目添加正确的代码的位置。

非常感谢您的帮助,非常感谢。

.H文件:

//  MVViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface MVViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIImageView *moveMe2;

//New action to repeat launch (Touch Down)
- (IBAction)rocketRepeat:(id)sender;

//New action to stop launch (Touch Up Inside)
- (IBAction)rocketStop:(id)sender;

//This is the original launch button (Touch Down)
- (IBAction)yourRocketButton:(id)sender; 

@end

.M FILE

//  MVViewController.m

#import "MVViewController.h"

@interface MVViewController ()

@end

@implementation MVViewController {
    AVAudioPlayer *audioPlayer;
}

@synthesize moveMe2;

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [self setMoveMe2:nil];
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return ((interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown));
}

- (IBAction)rocketRepeat:(id)sender
//Getting error "Use of undeclared identifier 'yourRocketButton'
{
    [yourRocketButton addTarget:self action:@selector(rocketRepeat:) forControlEvents:UIControlEventTouchDown];
}

- (IBAction)rocketStop:(id)sender 
//Getting error "Use of undeclared identifier 'yourRocketButton'
{
    [yourRocketButton addTarget:self action:@selector(rocketStop:) forControlEvents:UIControlEventTouchUpInside];
}

- (IBAction)yourRocketButton:(id)sender {
    moveMe2.center = CGPointMake(100.0f, 408.0f);
    [UIView animateWithDuration:2.0 animations:^{moveMe2.center = CGPointMake(100, -55);}]; 
}

@end

@@@@@@@@

编辑 * 这是最终有效的 *

//  RKViewController.m

#import "RKViewController.h"

@interface RKViewController ()

@end

@implementation RKViewController
@synthesize RocketMove;
@synthesize Launch;

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [self setRocketMove:nil];
    [self setLaunch:nil];
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return ((interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown));
}

- (IBAction)rocketRepeat:(id)sender {
    [self performSelector:@selector(rocketRepeat:) withObject:self afterDelay:1.0];
    RocketMove.center = CGPointMake(100.0f, 408.0f);
    [UIView animateWithDuration:1.0 animations:^{RocketMove.center = CGPointMake(100, -55);}];
}

- (IBAction)rocketStop:(id)sender {
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
}
@end

//  RKViewController.h

#import <UIKit/UIKit.h>

@interface RKViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIImageView *RocketMove;
@property (strong, nonatomic) IBOutlet UIButton *Launch;

- (IBAction)rocketRepeat:(id)sender;
- (IBAction)rocketStop:(id)sender;
@end

2 个答案:

答案 0 :(得分:1)

为此必须使用UIControlEvent

1。您需要为每个目的分别使用两个IBAction,例如按住按钮一个,一个按钮释放后。

2。要按住按钮,您需要使用UIControlEventTouchDown。所以有一个rocketRepeat动作,你可以定期使用NSTimer调用火箭动作并使用:

 [yourRocketButton addTarget:self action:@selector(rocketRepeat:) forControlEvents:UIControlEventTouchDown];

3。然后使用UIControlEventTouchUpInside的其他操作,您将使NSTimer无效,以便火箭停止。调用该操作rocketStop或其他内容并使用:

[yourRocketButton addTarget:self action:@selector(rocketStop:) forControlEvents:UIControlEventTouchUpInside];

<强> --- --- EDIT

行动1:

- (IBAction)rocketRepeat:(id)sender
{
    //code for starting rocker, timer action
}

行动2:

- (IBAction)rocketStop:(id)sender
{
   //Code for stopping rocket
}

yourButton不是一个动作,它是一个UIButton。我希望你在IB中创建了一个按钮,拖放按钮。在viewDidLoad中,您可以编写以下两行代码:

而不是yourButton你写下了你从IB上拖下来的按钮的名字。我希望您知道如何从界面构建器添加按钮并连接它。

- (void)viewDidLoad
{
  [yourRocketButton addTarget:self action:@selector(rocketRepeat:) forControlEvents:UIControlEventTouchDown]; //For touch down button action
  [yourRocketButton addTarget:self action:@selector(rocketStop:) forControlEvents:UIControlEventTouchUpInside]; //When button is let go.

    [super viewDidLoad];


   // Do any additional setup after loading the view, typically from a nib.
}

答案 1 :(得分:1)

如果您希望按下按钮后自动启动火箭,则应将以下代码添加到rocketLaunch:方法中。如果您希望它们从头开始出现,请从viewDidLoad方法中调用它。

- (void)launchRocketsTimer
{
    [self.timer invalidate]; //you have to create a NSTimer property to your view controller
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:@selector(scheduledRocketLaunch:) userInfo:nil repeats:YES];
}

- (void)scheduledRocketLaunch:(NSTimer *)t
{
    moveme2.center = _bottom_point_; //set it where you would like it to start
    [UIView animateWithDuration:2.0 animations:^{moveMe2.center = CGPointMake(100, -55);}];
}

请记得在dealloc中释放你的计时器。

哦,还有一件事: 当你分配你的AVAudioPlayer时,你的rocketsound:方法有内存泄漏。您可以用以下代码替换代码:

- (IBAction)rocketsound:(id)sender
 {
    NSURL *url = [NSURL fileURLWithPath: [NSString stringWithFormat:@"%@/rocketlaunch.mp3", [[NSBundle mainBundle] resourcePath]]];

    NSError *error;
    if (self.audioPlayer == nil)
    {
        self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error] autorelease]; //Note the use of setter property and the autorelease. (you could use your own code because the if will prevent the leak in this case).
    }

    audioPlayer.numberOfLoops = 0;

    if (audioPlayer == nil)
        NSLog(@"%@", [error description]);
    else 
        [audioPlayer play];
}