全部
当我使用 NSTimer 完成我的任务时,我遇到了一些问题。我需要一些帮助,谢谢。
这是问题所在。
当我使用 NSTimer 在 UIImageView 上显示动画时。但是 UIImageView 只是在我第一次使用 xcode 在我的 iphone4s 中运行代码时更新。当 UIImageView 再次出现时,它不会更新图像内容。我查过日志,每次都调用定时器,运行时代码被覆盖。
NS定时器:
if (networkStatusTimer == nil) {
networkStatusTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(getcurrentNetworkDelayStatus) userInfo:nil repeats:YES];
networkStatusImageView 初始化部分:
- (UIImageView *)networkStatusImageView
{
if (nil == _networkStatusImageView) {
CGRect rect = CGRectMake(246, 12, 24, 8);
_networkStatusImageView = [[UIImageView alloc]initWithFrame:rect];
_networkStatusImageView.contentMode = UIViewContentModeRight | UIViewContentModeTop;
UIImageView *img1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"talk_icon_redsolid_1X"]];
UIImageView *img2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"talk_icon_redhollow_1x"]];
UIImageView *img3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"talk_icon_whitehollow_1x"]];
img2.left = img1.right;
img3.left = img2.right;
[_networkStatusImageView setNeedsDisplay];
[_networkStatusImageView addSubview:img1];
[_networkStatusImageView addSubview:img2];
[_networkStatusImageView addSubview:img3];
}
return _networkStatusImageView;
}
选择器代码:
- (void) getcurrentNetworkDelayStatus {
currentNetworkDelayTime = 0;
[_networkStatusImageView removeAllSubviews];
UIImageView *img1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"talk_icon_redhollow_1x"]];
UIImageView *img2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"talk_icon_redhollow_1x"]];
UIImageView *img3 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"talk_icon_redhollow_1x"]];
dispatch_async(dispatch_get_main_queue(), ^{
if (currentNetworkDelayTime == 0) {
if (currentISRed) {
img1.image = [UIImage imageNamed:@"talk_icon_redhollow_1x"];
img2.image = [UIImage imageNamed:@"talk_icon_redhollow_1x"];
img3.image = [UIImage imageNamed:@"talk_icon_redhollow_1x"];
} else { // 显示红色实心
img1.image = [UIImage imageNamed:@"talk_icon_redsolid_1X"];
img2.image = [UIImage imageNamed:@"talk_icon_redsolid_1X"];
img3.image = [UIImage imageNamed:@"talk_icon_redsolid_1X"];
}
currentISRed = !currentISRed;
} else if(currentNetworkDelayTime > 0 && currentNetworkDelayTime <= 500){
img1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"talk_icon_greensolid1X"]];
img2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"talk_icon_greensolid1X"]];
img3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"talk_icon_greensolid1X"]];
} else if(currentNetworkDelayTime > 500 && currentNetworkDelayTime <= 3000){
img1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"talk_icon_greensolid1X"]];
img2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"talk_icon_greensolid1X"]];
img3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"talk_icon_greenhollow_1x"]];
} else if (currentNetworkDelayTime > 3000){
img1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"talk_icon_greensolid1X"]];
img2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"talk_icon_greenhollow_1x"]];
img3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"talk_icon_greenhollow_1x"]];
}
img2.left = img1.right;
img3.left = img2.right;
[img1 setNeedsDisplay];
[img2 setNeedsDisplay];
[img3 setNeedsDisplay];
[_networkStatusImageView setNeedsDisplay];
[_networkStatusImageView addSubview:img1];
[_networkStatusImageView addSubview:img2];
[_networkStatusImageView addSubview:img3];
});
}