Search code examples
objective-ciosmathtap

Tap Count & Lap


I created sample tap count app. But I don't know how to count lap. Example: when hit count 100, lap is 1. when hit count 200 lap is 2. I used following code. Thanks. I am xcode beginner.

Following Code is ViewController.m

#import "ViewController.h"

 @interface ViewController ()

 @end

@implementation ViewController

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

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

 -(IBAction)zero {
     counter=0;
     count.text = [NSString stringWithFormat:@"%i", counter];
}
- (void)viewDidLoad
{
    counter=0;
     count.text=@"0";
     [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

 - (void)didReceiveMemoryWarning
 {
     [super didReceiveMemoryWarning];
     // Dispose of any resources that can be recreated.
 }

 @end

Following Code is ViewController.h #import

 int counter;

 @interface ViewController : UIViewController {
     IBOutlet UILabel *count;
     IBOutlet UILabel *lap;
 }

 -(IBAction)plus;
 -(IBAction)minus;
 -(IBAction)zero;
 @end

Solution

  • Here is how I accomplished this. Now note that I am not using your exact set up, but this should work, or at least give you the right idea. This should in theory be a lot harder, as you really want to check the lap for an infinite amounts of 100's, so we need some logic too check, EXCEPT if we use int. Int is not precise, so if we take say 50/100, it will still return 0. However, 100/100 = 1, 200/100 = 2, 275/100 still equals 2. This will accomplish what you want easily.

    Here's a simple example with only a button that increments the amount, I use global variables for lap count and tap count.

    In the .h

    #import <UIKit/UIKit.h>
    
    @interface TapCounterViewController : UIViewController
    - (IBAction)buttonTouched:(id)sender;
    
    @end 
    

    In the .m

    #import "TapCounterViewController.h"
    
    @interface TapCounterViewController ()
    
    @end
    
    @implementation TapCounterViewController
    
    int lapNumber = 0;
    int buttonTaps = 0;
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    - (IBAction)buttonTouched:(id)sender {
        buttonTaps = buttonTaps + 1;
        NSLog(@"Current Button Taps = %i", buttonTaps);
    
        //Check for Lap
        [self checkForLap];
    }
    
    
    -(void)checkForLap {
        //We must divide by 100 to check for the number if laps.
        int laps = buttonTaps/100;
        NSLog(@"Number of laps = %i", laps);
        lapNumber = laps;
    }
    @end
    

    This can be easily adapted for decrementing, resetting you whatever. Just whenever you make a change to button tap, execute checkForLap.

    Good luck, and if you need more info let me know!

    EDIT

    As for the vibration:

    There are two seemingly similar functions that take a parameter kSystemSoundID_Vibrate:

    1) AudioServicesPlayAlertSound(kSystemSoundID_Vibrate); 2) AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

    Both the functions vibrate the iPhone. But when you use the first function on devices that don’t support vibration, it plays a beep sound. The second function on the other hand does nothing on unsupported devices. So if you are going to vibrate the device continuously, as a alert, common sense says, use function 2.

    See also "iPhone Tutorial: Better way to check capabilities of iOS devices" article.

    Header files to import:

    #import <AudioToolbox/AudioServices.h>