Search code examples
iphonetouchesbegan

How to save number of taps on image?


I am trying to make an app for children on how to save money. As a visual, at the beginning, I have an empty jar (UIIMage) that, when tapped, the image changes to a jar filled with coins (to simulate deposit/saving), the label below the jar then says something - like congratulations, make another deposit. When the user taps the jar up to 5 times, more coins are added until the jar is filled at the 5th tap and the label below the jar says, watch your money grow.

Note that the tap 1 may occur today, tap 2 at another time, tap three, a week later, etc.

I am using touchesBegan for the tapCount (with switch) to illustrate this. The tapping and changing of images in the UIImageView works well (right now for demo at 0.5 sec interval between taps).

(1) I am wondering if I am doing this right - the animation works well - but it resets to tap 1at when the user leaves the app and comes back again to tap. Can the last action be saved so that when the child quits and comes back to the app, he/she arrives at the same state where/when he/she left. I tried NSUserdefault but this seemed not to work. I am even wondering if data can be saved from touchesBegan (with switch).

Any ideas greatly appreciated. Thanks.

Here is the code for the animation (shortened to three taps for brevity):

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    NSUInteger tapCount = [touch tapCount];

    switch (tapCount) {
    case 1:
        [self performSelector:@selector(tappedOnce) withObject:nil afterDelay:0.5];
        break;

    case 2:
        [NSObject cancelPreviousPerformRequestsWithTarget:self          selector:@selector(tappedOnce) object:nil];
        [self performSelector:@selector(tappedTwice) withObject:nil afterDelay:0.5];
        break;

    case 3:
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(tappedTwice) object:nil];
        [self performSelector:@selector(tappedThreeTimes) withObject:nil afterDelay:0.5];
        break;

    default:
        break;

}

}

 - (void)tappedOnce {

  UIImageView *imageView = [[UIImageView alloc] initWithFrame:
                          CGRectMake(95, 93, 142, 205)];
  imageView.contentMode = UIViewContentModeBottom;
imageView.image = [UIImage imageNamed:@"bucketb.png"];
[self.view addSubview:imageView];
  instructionLabel.text=@"Congratulations, you have deposited money.  Deposit some more.";
 }

 - (void)tappedTwice {

 UIImageView *imageView = [[UIImageView alloc] initWithFrame:
                           CGRectMake(95, 93, 142, 205)];
 imageView.contentMode = UIViewContentModeBottom;
imageView.image = [UIImage imageNamed:@"bucketc.png"];
[self.view addSubview:imageView];
    instructionLabel.text=@"Congratulations, you have added more money to your  account.";
  }

 - (void)tappedThreeTimes {

 UIImageView *imageView = [[UIImageView alloc] initWithFrame:
                         CGRectMake(95, 93, 142, 205)];
imageView.contentMode = UIViewContentModeBottom;
 imageView.image = [UIImage imageNamed:@"bucketd.png"];
[self.view addSubview:imageView];
 instructionLabel.text=@"Watch your money grow.";
}

The NSUserdefault (at appdelegate):

 - (void)applicationDidEnterBackground:(UIApplication *)application
{


NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

[userDefaults setInteger:5 forKey:@"lastTouch"];


[userDefaults synchronize];


 }

 - (void)applicationWillEnterForeground:(UIApplication *)application
 {

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

int temp = [userDefaults integerForKey:@"lastTouch"];

 }

Solution

  • Using the touch event's tap count is the wrong way to count taps for this kind of problem:

    • presumably you want the tap count to increment each time, regardless of pause between taps

    • you lose the state of the tap count after app exit/restart

    Instead, just worry about detecting a single tap, but store as a class property the current tap count. Increment this each time you get a single tap. And you could use NSUserDefaults to store and restore this class property at app backgrounding/foregrounding.