如何保存图像上的水龙头数量?

时间:2012-11-22 21:24:09

标签: iphone touchesbegan

我正在尝试为孩子们制作一个如何省钱的应用程序。作为一个视觉,一开始,我有一个空罐子(UIIMage),当点击时,图像变为装满硬币的罐子(模拟存款/保存),罐子下方的标签然后说出一些东西 - 比如祝贺,另外存款。当用户点击罐子最多5次时,会添加更多硬币,直到第5个水龙头装满罐子,罐子下方的标签显示,看着你的钱增长。

请注意,点击1可能会在今天发生,在另一个时间点击2,点击三,一周后等等。

我使用touchesBegan作为tapCount(带开关)来说明这一点。 UIImageView中图像的点击和更改效果很好(现在在点击之间以0.5秒的间隔进行演示)。

(1)我想知道我是否正确行事 - 动画效果很好 - 但当用户离开应用程序并重新点击时,它会重置为点按1at。可以保存最后一个动作,以便当孩子退出并返回应用程序时,他/她到达他/她离开时的相同状态。我试过NSUserdefault,但这似乎不起作用。我甚至想知道是否可以从touchesBegan(带开关)保存数据。

任何想法都非常感激。感谢。

以下是动画的代码(为简洁起见缩短为三次点击):

 - (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.";
}

NSUserdefault(在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"];

 }

1 个答案:

答案 0 :(得分:0)

使用触摸事件的点击计数是计算此类问题的点按的错误方法:

  • 大概你想要每次都会增加点击次数,而不管点击之间的暂停

  • 在app退出/重启

  • 后,您将失去点按计数的状态

相反,只需担心检测到单击,但将当前点击计数存储为类属性。每次点击都会增加此值。您可以使用NSUserDefaults在app backgrounding / foregrounding中存储和恢复此类属性。