Я искал по всему Интернету, и мне не повезло, что этот проект работал с таймером. Я получаю аварийное приложение каждый раз, когда использую таймер.
Для тестирования и обучения я создаю маленькие простые приложения. Это кнопка, которая посылает ракету из нижней части экрана и исчезает с экрана вместе со звуковым эффектом ракеты.
Я хочу добавить таймер к кнопке, чтобы при удерживании кнопки ракета запускалась, сбрасывалась и запускалась снова и снова, пока я не отпущу кнопку. Я думаю, что теперь моя единственная надежда - вставить код из моих файлов .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