Это проект метронома, который я сделал ранее. Он довольно простой, но он должен помочь. Если вы используете его, просто назовите меня, Jordan Brown 15y Mango Apps. Это заняло какое-то время, но я так и не сделал из этого приложение.
//.h
NSTimer *timer;
int count;
float bpm;
float speed;
UILabel *numberLabel;
IBOutlet UISwitch *vibrate;
IBOutlet UISegmentedControl *timing;
}
- (IBAction)up;
- (IBAction)down;
- (IBAction)stop:(id)sender;
@property (nonatomic, retain)IBOutlet UILabel *numberLabel;
@property (nonatomic, retain)IBOutlet UILabel *bpmLabel;
@property (nonatomic, retain)IBOutlet UISegmentedControl *timing;
//.m
#define SECONDS 60
#import <AudioToolbox/AudioToolbox.h>
@implementation metronome
@synthesize numberLabel; // labels
@synthesize bpmLabel;
@synthesize timing;
-(IBAction)stop:(id)sender{
[timer invalidate];
[self performSelector:@selector(playTockSound)];
numberLabel.text = [NSString stringWithFormat:@"%i",count];
bpm = bpm;
if (bpm > 300) {
bpm = 300;
}
int new = bpm;
bpmLabel.text = [NSString stringWithFormat:@"%i",new];
speed = INFINITY;
NSLog(@"%f",speed);
timer = [NSTimer scheduledTimerWithTimeInterval:speed target:self selector:@selector(updateNumber) userInfo:nil repeats:YES];
}
-(IBAction)up{
[timer invalidate];
count = 1;
[self performSelector:@selector(playTockSound)];
numberLabel.text = [NSString stringWithFormat:@"%i",count];
bpm = bpm+10;
if (bpm > 300) {
bpm = 300;
}
int new = bpm;
bpmLabel.text = [NSString stringWithFormat:@"%i",new];
speed = SECONDS/bpm;
NSLog(@"%f",speed);
timer = [NSTimer scheduledTimerWithTimeInterval:speed target:self selector:@selector(updateNumber) userInfo:nil repeats:YES];
}
-(IBAction)down{
[timer invalidate];
count = 1;
[self performSelector:@selector(playTockSound)];
numberLabel.text = [NSString stringWithFormat:@"%i",count];
bpm = bpm-10;
if (bpm < 10) {
bpm = 10;
}
int new = bpm;
bpmLabel.text = [NSString stringWithFormat:@"%i",new];
speed = SECONDS/bpm;
NSLog(@"%f",speed);
timer = [NSTimer scheduledTimerWithTimeInterval:SECONDS/bpm target:self selector:@selector(updateNumber) userInfo:nil repeats:YES];
}
-(void)updateNumber{
count += 1;
//if 4/4 timing is selected then the count wont go past 4
if (timing.selectedSegmentIndex == 2) {
if (count >= 5) {
count = 1;
}
}
//if 3/4 timing is selected then the count wont go past 3
if (timing.selectedSegmentIndex == 1) {
if (count >= 4) {
count = 1;
}
}
//if 2/4 timing is selected then the count wont go past 2
if (timing.selectedSegmentIndex == 0) {
if (count >= 3) {
count = 1;
}
}
//In each timing case it plays the sound on one and depending
//on the limitiations on the cont value the amount of each tick
if (count == 1) {
[self performSelector:@selector(playTockSound)];
}else {
[self performSelector:@selector(playTickSound)];
}
numberLabel.text = [NSString stringWithFormat:@"%i",count];
}
-(void)playTickSound
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"tick"
ofType:@"caf"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path]
, &soundID);
AudioServicesPlaySystemSound (soundID);
}
-(void)playTockSound
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"tock"
ofType:@"caf"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path]
, &soundID);
AudioServicesPlaySystemSound (soundID);
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidLoad
{
bpm = 60.00;
speed = SECONDS/bpm;
timer = [NSTimer scheduledTimerWithTimeInterval:speed target:self selector:@selector(updateNumber) userInfo:nil repeats:YES];
int new = bpm;
bpmLabel.text = [NSString stringWithFormat:@"%i",new];
[super viewDidLoad];
}