Iphone 将NSTimer添加到按钮
我已经在网上搜索了很多次,但都没能用定时器完成这个项目,每次使用定时器的时候,我都会遇到一个崩溃的应用程序 出于测试和学习的目的,我构建了一些简单的应用程序。它是一个按钮,从屏幕底部发射火箭,并在屏幕外消失,同时产生火箭音效 我想在按钮上添加一个计时器,这样当按下按钮时,火箭将发射、复位,并反复发射,直到我松开按钮。我想我现在唯一的希望是从我的.h和.m文件中粘贴代码,希望有人能告诉我需要做什么,以及在什么地方需要为这个项目添加适当的代码 非常感谢你的帮助,非常感谢 .H文件:Iphone 将NSTimer添加到按钮,iphone,objective-c,ios,xcode,nstimer,Iphone,Objective C,Ios,Xcode,Nstimer,我已经在网上搜索了很多次,但都没能用定时器完成这个项目,每次使用定时器的时候,我都会遇到一个崩溃的应用程序 出于测试和学习的目的,我构建了一些简单的应用程序。它是一个按钮,从屏幕底部发射火箭,并在屏幕外消失,同时产生火箭音效 我想在按钮上添加一个计时器,这样当按下按钮时,火箭将发射、复位,并反复发射,直到我松开按钮。我想我现在唯一的希望是从我的.h和.m文件中粘贴代码,希望有人能告诉我需要做什么,以及在什么地方需要为这个项目添加适当的代码 非常感谢你的帮助,非常感谢 .H文件: // MVVi
// 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
@@@@@@@@
编辑*这就是最终奏效的方法*
//RKViewController.h
#进口
@接口RKViewController:UIViewController
@属性(强,非原子)IBUIImageView*RocketMove;
@属性(强,非原子)IBUIButton*启动;
-(iAction)rocketRepeat:(id)发送方;
-(iAction)rocketStop:(id)发送方;
@结束
为此,您必须使用uicontrol事件
1.您需要两个独立的iAction
s用于每个目的,例如一个用于按住按钮,另一个用于松开按钮
2。按住按钮时,需要使用UIControlEventTouchDown
。因此,有一个rocketRepeat
操作,您可以使用NSTimer
定期调用rocket操作,并使用:
[yourRocketButton addTarget:self action:@selector(rocketRepeat:) forControlEvents:UIControlEventTouchDown];
3。然后使用UIControlEventTouchUpInside
的另一个操作,您将使NSTimer无效,因此火箭停止。调用该操作rocketStop
或其他操作并使用:
[yourRocketButton addTarget:self action:@selector(rocketStop:) forControlEvents:UIControlEventTouchUpInside];
---编辑---
行动1:
- (IBAction)rocketRepeat:(id)sender
{
//code for starting rocker, timer action
}
行动2:
- (IBAction)rocketStop:(id)sender
{
//Code for stopping rocket
}
yourButton
不是一个动作,而是一个UIButton。我希望您已经在IB中创建了一个按钮,拖放该按钮。然后在viewDidLoad
中编写以下两行代码:
您可以编写从IB拖放的按钮的名称,而不是yourButton
。我希望您知道如何从interface builder添加按钮并将其连接起来
- (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.
}
如果希望火箭在按下按钮后自动发射,则应向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];
}
我用您提供的操作名称重新编写了代码,并建立了正确的连接。我在.M文件中的每个iAction上都有一个错误。我将我的动画动作重命名为“yourRocketButton”。错误是“使用未声明的标识符‘yourRocketButton’(我肯定我只是误解了你或其他什么)@sdlabsyourRocketButton
只是一个示例名称,您应该将其替换为您的按钮名称,即您用来点击的按钮。我重新连接了iAction以使动画与您的示例相匹配。在实际为按钮命名时,我是否遗漏了一些内容?@sdlabs,在我给出的示例中,我假设您想移动按钮火箭取决于按钮的点击,所以我假设你有一个从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.
}
- (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);}];
}
- (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];
}