【发布时间】:2012-11-22 21:24:08
【问题描述】:
我正在尝试为儿童制作一个关于如何省钱的应用程序。作为一个视觉效果,一开始,我有一个空罐子 (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"];
}
【问题讨论】:
-
“我尝试了 NSUserdefault 但这似乎不起作用”——你尝试了什么?在什么情况下它不起作用?
标签: iphone touchesbegan