我已经搜索了所有的网络,没有使这个项目与计时器工作的运气,我最终每次使用计时器时崩溃的应用程序。添加一个NSTimer到一个按钮
为了测试和学习的目的,我构建了一些简单的应用程序。这是一个从屏幕底部发射火箭并从屏幕消失的按钮,以及火箭音效。
我想添加一个计时器到按钮,这样当按钮被按下时,火箭会一直启动,重置和启动,直到我释放按钮。我认为我现在唯一的希望是粘贴我的.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文件
// 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
我重写了你提供的动作名称并建立了正确的连接。 .M文件中的每个IBAction都出现错误。我将我的动画动作“yourRocketButton”重命名。错误说“使用未声明的标识符'yourRocketButton'(我敢肯定,我误解了你或其他什么) – sdlabs 2012-03-29 07:12:07
@sdlabs'yourRocketButton'只是一个示例名称,您应该用您的按钮名称替换它,您使用的按钮 – iNoob 2012-03-29 07:14:24
我重新连接了动画的IBAction以匹配你的例子我是否错过了一些事实上给一个按钮起一个名字 – sdlabs 2012-03-29 07:20:39