我有适用于 iPhone 的两个视图应用程序。在第一个中,我使用 NSTimer 调用每秒执行此操作的代码:
- 更新变量
- 发出声音
- 更新标签
- 更新图像位置
当我启动应用程序时 - 一切都很完美。当我转到其他视图然后返回第一个屏幕时 - 1 和 2 工作正常,但 3 和 4 重置并且根本不起作用。
可能是什么问题呢?
看起来它失去了元素的“钩子”......
-(void)countup {
DeccelFloat += 0.4;
SpeedFloat += 5/(DeccelFloat);
SpeedInt = lroundf(SpeedFloat);
speed.text = [NSString stringWithFormat:@"%i", SpeedInt];
imageview.center = CGPointMake(imageview.center.x, imageview.center.y+3);
if (imageview.center.y < 280) {
imageview.alpha = 1;
if (SpeedFloat > MaxSpeedValue) {
[self playalarm];
}else{
[self playbeep];
}
}
if ((imageview.center.y >= 280) && (imageview.center.y < 283)) {
if (SpeedFloat > MaxSpeedValue) {
[self playshot];
}else{
[self playbeepbeep];
}
}
if ((imageview.center.y >= 280) && (imageview.center.y < 350)) {
imageview.alpha = 0.5-(0.5/70)*(imageview.center.y - 280);
}
if (imageview.center.y >= 350) {
imageview.alpha = 0;
imageview.center = CGPointMake(imageview.center.x, 180);
}
}
这是我的计时器:
- (void)viewDidLoad
{
[super viewDidLoad];
MaxSpeedValue = 60;
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSNumber *maxspeedsetttings = nil;
NSNumber *blinkingsetttings = nil;
NSNumber *startedsetttings = nil;
if (standardUserDefaults) {
maxspeedsetttings = [standardUserDefaults objectForKey:@"maxspeedsetttings"];
blinkingsetttings = [standardUserDefaults objectForKey:@"blinkingsetttings"];
startedsetttings = [standardUserDefaults objectForKey:@"startedsetttings"];
}
MaxSpeedDef = [maxspeedsetttings intValue];
if (MaxSpeedDef == 1) {
maxspeed.text = [NSString stringWithFormat:@"%i", MaxSpeedValue];
}else{
maxspeed.alpha = 0;
maxspeedlabel.alpha = 0;
}
imageview.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"0.png"],
[UIImage imageNamed:@"5.png"],
[UIImage imageNamed:@"10.png"],
nil];
imageview.animationDuration = 1;
imageview.animationRepeatCount = 0;
BlinkingDef = [blinkingsetttings intValue];
if (BlinkingDef == 1) {
[imageview startAnimating];
}else{
[imageview stopAnimating];
}
[self.view addSubview:imageview];
StartedDef = [startedsetttings intValue];
if (StartedDef == 0) {
SpeedFloat = 0;
DeccelFloat = 0;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countup) userInfo:nil repeats:YES];
StartedDef = 1;
if (standardUserDefaults) {
[standardUserDefaults setObject:[NSNumber numberWithInt:StartedDef] forKey:@"startedsetttings"];
[standardUserDefaults synchronize];
}
}
}