我已經搜索了所有的網絡,沒有使這個項目與計時器工作的運氣,我最終每次使用計時器時崩潰的應用程序。添加一個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