2012-03-29 190 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]; 
} 
相關問題