2012-03-29 144 views
-1

我已经搜索了所有的网络,没有使这个项目与计时器工作的运气,我最终每次使用计时器时崩溃的应用程序。添加一个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 

回答

1

你必须使用UIControlEvent用于此目的。

1.您需要两个单独的IBAction s用于各种目的,比如说一个用于按住按钮,一个用于释放按钮。

2.要按住按钮,您需要使用UIControlEventTouchDown。所以有rocketRepeat动作,你一直使用NSTimer与定期和使用调用火箭行动:

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

然后用另一行动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中,您编写了以下两行代码:

而不是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. 
} 
+0

我重写了你提供的动作名称并建立了正确的连接。 .M文件中的每个IBAction都出现错误。我将我的动画动作“yourRocketButton”重命名。错误说“使用未声明的标识符'yourRocketButton'(我敢肯定,我误解了你或其他什么) – sdlabs 2012-03-29 07:12:07

+0

@sdlabs'yourRocketButton'只是一个示例名称,您应该用您的按钮名称替换它,您使用的按钮 – iNoob 2012-03-29 07:14:24

+0

我重新连接了动画的IBAction以匹配你的例子我是否错过了一些事实上给一个按钮起一个名字 – sdlabs 2012-03-29 07:20:39

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]; 
} 
相关问题