Iphone 如何保存图像上的点击次数?
我正在尝试为孩子们制作一个关于如何省钱的应用程序。作为一种视觉效果,在一开始,我有一个空罐子(UIIMage),当点击它时,图像会变成一个装满硬币的罐子(模拟存款/储蓄),罐子下面的标签会说一些话,比如祝贺你,再存款。当用户轻敲罐子多达5次时,会添加更多的硬币,直到第5次轻敲时罐子装满,罐子下面的标签上写着:“看着你的钱增长。” 请注意,轻触1可能在今天发生,轻触2可能在另一时间发生,轻触3可能在一周后发生,等等 我使用touchesbearth作为抽头计数(带开关)来说明这一点。在UIImageView中点击和更改图像效果很好(现在演示时,点击间隔为0.5秒)Iphone 如何保存图像上的点击次数?,iphone,touchesbegan,Iphone,Touchesbegan,我正在尝试为孩子们制作一个关于如何省钱的应用程序。作为一种视觉效果,在一开始,我有一个空罐子(UIIMage),当点击它时,图像会变成一个装满硬币的罐子(模拟存款/储蓄),罐子下面的标签会说一些话,比如祝贺你,再存款。当用户轻敲罐子多达5次时,会添加更多的硬币,直到第5次轻敲时罐子装满,罐子下面的标签上写着:“看着你的钱增长。” 请注意,轻触1可能在今天发生,轻触2可能在另一时间发生,轻触3可能在一周后发生,等等 我使用touchesbearth作为抽头计数(带开关)来说明这一点。在UIImag
(1) 我想知道我是否做得对-动画效果很好-但当用户离开应用程序并再次点击时,它会重置为点击1。是否可以保存最后一个操作,以便当孩子退出并返回应用程序时,他/她到达与离开时相同的状态。我尝试了默认设置,但这似乎不起作用。我甚至想知道是否可以从ToucheSStart(使用开关)保存数据 非常感谢您的任何想法。谢谢 以下是动画的代码(为简洁起见,缩短为三次点击): } NSUserdefault(在appdelegate处):
对于此类问题,使用触摸事件的点击计数是错误的:
- 假设您希望每次点击次数递增,而不考虑点击之间的暂停
- 应用程序退出/重新启动后,您将丢失点击计数的状态
相反,只需担心检测到单个抽头,但将当前抽头计数存储为类属性。每次点击一次,增加该值。您可以使用
NSUserDefaults
在应用程序后台/前台存储和还原该类属性。“我尝试了NSUserDefaults,但似乎不起作用”——您尝试了什么?它以什么方式不起作用?正如我所想。非常感谢。我知道我会为此被炒鱿鱼,但你能给我举个例子吗?谢谢你的接受。想不出一个例子,但看看如何向类中添加属性-您需要一个int属性,每次点击时都会向其中添加1。提示:将“@property(nonatomic)int-tapCount;”添加到头文件中(如果愿意,也可以在.m文件顶部添加私有类扩展名)。然后以“self.tapCount”的形式访问它,使用“self.tapCount++”等增加它。很酷,会尝试的,再次感谢。你非常乐于助人,我们非常感激你。我正朝那个方向走去(不幸的是,在课堂上讨论这个问题时,我的笔记被我的狗吃掉了……哈哈哈,真的没有。)
- (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.";
}
- (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"];
}