-1

Im creating an app and one part of it has a tap counter in it.
The UI consists of a button which is pressed for counting, a reset button and a label which displays the amount of taps. The problem is that I want the iDevice to vibrate or make a sound after a specific amount of taps and then end there so the counter label doesn't react to the button anymore.

Here is my code so far:

.h

@interface TapsViewController : UIViewController {

    int counter;
    IBOutlet UILabel *count;
}

-(IBAction)plus;
-(IBAction)reset;

@end

.m

- (void)viewDidLoad {
    counter=0;
    count.text = @"Start";
}

-(IBAction)plus{
    counter=counter + 1;    
    count.text = [NSString stringWithFormat:@"%i", counter];
}

-(IBAction)reset{
    counter=0;
    count.text = [NSString stringWithFormat:@"Start"];
}

How do I have the app vibrate or make a sound when the counter reaches a predetermined value?

Thank you for your help!

Kian
  • 122
  • 13
  • 1
    Welcome to StackOverflow! It looks like you have some code which, if wired up correctly, would implement the counting behavior you describe. What do you not understand? How to make a sound? Have you looked at AVAudioPlayer? Try to refine your question a little more. – Ben Flynn Jan 13 '14 at 02:32

2 Answers2

2

well to make it stop responding to the taps, just do an if statement

For example,f if you want it to stop after 5 taps.

-(IBAction)plus{
    if (counter < 4) {
        //just a tip, counter++; does the same thing as counter += 1; which does the same thing as counter = counter+1;
        counter++;
    }
    else if (counter == 4) {
    //after four taps, counter will equal 4, so this part will be called on the 5th tap.
        counter++;
        //play your sound or do your vibrate here
    }
     count.text = [NSString stringWithFormat:@"%i",counter];
}

To do the vibrate, look at Wain's answer. To play a sound, check out AVAudioPlayer.

WolfLink
  • 3,308
  • 2
  • 26
  • 44
0

Create an outlet to the button and disable it when the count limit is reached (so it won't respond to touches any more). Then enable it again when you reset.

To vibrate:

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
Wain
  • 118,658
  • 15
  • 128
  • 151
  • I had to import 'AudioToolbox.framework' '#import ' And then everything worked so thank you! – Kian Jan 13 '14 at 10:06