iOS-使用NSTimer,UIImageView仅在第一时间更新
全部 当我使用NSTimer完成任务时,遇到了一些问题。我需要一些帮助,谢谢 问题就在这里 当我使用NSTimer在UIImageView上显示动画时。但当我在iphone4s中使用xcode运行代码时,UIImageView只是在第一次更新。当UIImageView再次出现时,它不会更新图像内容。 我已经检查了日志,计时器每次都被调用,代码在运行时被覆盖 NSTimer:iOS-使用NSTimer,UIImageView仅在第一时间更新,ios,uiimageview,Ios,Uiimageview,全部 当我使用NSTimer完成任务时,遇到了一些问题。我需要一些帮助,谢谢 问题就在这里 当我使用NSTimer在UIImageView上显示动画时。但当我在iphone4s中使用xcode运行代码时,UIImageView只是在第一次更新。当UIImageView再次出现时,它不会更新图像内容。 我已经检查了日志,计时器每次都被调用,代码在运行时被覆盖 NSTimer: if (networkStatusTimer == nil) { networkStatusTimer =
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];
});
}
-(void)getcurrentNetworkDelayStatus{
currentNetworkDelayTime=0;
[_networkstatusimageviewremoveallsubview];
UIImageView*img1=[[UIImageView alloc]initWithImage:[UIIMAGENAME:@“talk_icon_redhollow_1x”];
UIImageView*img2=[[UIImageView alloc]initWithImage:[UIIMAGENAME:@“talk_icon_redhollow_1x”];
UIImageView*img3=[[UIImageView alloc]initWithImage:[UIIMAGENAME:@“talk_icon_redhollow_1x”];
dispatch\u async(dispatch\u get\u main\u queue()^{
如果(currentNetworkDelayTime==0){
如果(当前ISRED){
img1.image=[UIImage ImageName:@“talk_icon_redhollow_1x”];
img2.image=[UIImage ImageName:@“talk_icon_redhollow_1x”];
img3.image=[UIImage ImageName:@“talk_icon_redhollow_1x”];
}else{//显示红色实心
img1.image=[UIImage ImageName:@“talk_icon_redsolid_1X”];
img2.image=[UIImage ImageName:@“talk_icon_redsolid_1X”];
img3.image=[UIImage ImageName:@“talk_icon_redsolid_1X”];
}
currentISRed=!currentISRed;
}否则如果(currentNetworkDelayTime>0&¤tNetworkDelayTime 500&¤tNetworkDelayTime 3000){
img1=[[UIImageView alloc]initWithImage:[UIImageName:@“talk_icon_greensolid1X”];
img2=[[UIImageView alloc]initWithImage:[UIImageName:@“talk_icon_greenhollow_1x”];
img3=[[UIImageView alloc]initWithImage:[UIImageName:@“talk_icon_greenhollow_1x”];
}
img2.left=img1.right;
img3.left=img2.right;
[img1设置需要显示];
[img2设置需要显示];
[img3设置需要显示];
[\u网络状态图像视图设置需要显示];
[\u网络状态图像视图添加子视图:img1];
[_networkstatusimageviewaddsubview:img2];
[\u网络状态图像视图添加子视图:img3];
});
}
每次调用函数时,都将currentNetworkDelayTime
设置为0。这意味着只有:
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;
}
有人打电话来。因此您需要更改行currentNetworkDelayTime=0代码>至currentNetworkDelayTime++代码>
您不必每次都将图像添加到当前的UIView
,只需在ex.viewDidLoad
中创建一次图像,并为其创建一个全局变量
完成此操作后,添加此方法以缩短代码:
-(void)changeView:(NSArray *)strArray {
NSArray *viewArray = [[NSArray alloc] initWithObjects:view1, view2, view3, nil];
for (int a = 0; 0 < 3; a++) {
UIImageView *img = viewArray[a];
img.image = [UIImage imageNamed:strArray[a]];
[img setNeedsDisplay]; // If you really want to do this.
}
}
我已经在我的电脑上测试了你的代码,我让它正常工作了
在头文件中:
@property(nonatomic,strong)UIImageView *networkStatusImageView;
在viewDidLoad中:
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(test) userInfo:Nil repeats:YES];
self.networkStatusImageView = [self networkStatusImageView];
[self.view addSubview:self.networkStatusImageView];
功能网络状态图像视图:
- (UIImageView *)networkStatusImageView
{
if (nil == _networkStatusImageView) {
CGRect rect = CGRectMake(0, 0, 300, 50);
_networkStatusImageView = [[UIImageView alloc]initWithFrame:rect];
_networkStatusImageView.contentMode = UIViewContentModeRight | UIViewContentModeTop;
for (int a = 0; a < 3; a++) {
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(100*a, 0, 100, 50)];
[img setBackgroundColor:[UIColor blueColor]];
[_networkStatusImageView addSubview:img];
}
}
return _networkStatusImageView;
}
我使用背景色而不是图像,因为我不想制作用于测试的图像,只需将其更改为setImage:
,它应该可以工作。我已经解决了我的问题,解决方案如下:
我仔细检查了问题,并创建了以下三个UIImage字段变量:
@property (nonatomic, weak) UIImage *imgFile1;
@property (nonatomic, weak) UIImage *imgFile2;
@property (nonatomic, weak) UIImage *imgFile3;
并将它们与xib图像资源链接
此功能的实现不好,我计划重新实现
它的下一个版本,有太多的内存分配和释放,一些分配和释放可以删除,使代码更有效率
谢谢所有帮助我的人 感谢您的帮助,currentNetworkDelayTime由我设置,用于测试以下代码,我知道这一点。我尝试了你的答案,比如changeView方法,但是imageView仍然没有更新。你创建了对象吗?例如,在viewDidLoad中,img=[[UIIMageView alloc]init];或者在故事板中。我现在看到您没有为图像输入后缀“.jpg”。currentNetworkDelayTime==命名意味着这是网络延迟,与计时器运行的频率无关。关于观点:声音advice@Arbitur是的,我创建了这些对象。顺便说一句,我尝试添加图像文件的扩展名,如“.png”,但仍然不起作用。我想,可能不是后缀的问题。不需要setNeedsDisplay调用可能您的_networkStatusImageView为零最后:为什么调度异步?是的,我也没有看到setNeedsDisplay工作,我将删除setNeedsDisplay调用_networkStatusImageView在之前已初始化,它将不会为零。下面的代码:我想可能是进程同步的问题。所以我添加了dispatch_async来更新主线程中的UI。Arbiutr,谢谢你的帮助,也许有什么不同。我没有将视图放在UIViewController中。我制作了一个定制的UIView并将这些图像放入其中。请参阅。@interface RHCVoIPCardView:UIButtonIt不必仅位于viewDidLoad内,它也可以位于initWithFrame内:。好的,我还有一个问题,当UIView消失时将调用什么?像viewDidUnload一样,当UIViewController消失时会调用它。我已经尝试过你的解决方案,但只是第一次工作,当视图再次出现时,解决方案将不再工作。我认为缓存了一些东西,现在视图实现使用的是ARC。当视图消失时,我只调用[self removeFromSuperView];恐怕对我来说似乎很深奥。。arc没有发布影响UI的内容:)一定是其他内容
-(void)test {
int currentNetworkDelayTime = 0;
BOOL currentISRed = NO;
NSArray *subViews = [self.networkStatusImageView subviews];
UIImageView *img1 = [subViews objectAtIndex:0];
UIImageView *img2 = [subViews objectAtIndex:1];
UIImageView *img3 = [subViews objectAtIndex:2];
if (currentNetworkDelayTime == 0) {
if (currentISRed) {
[img1 setBackgroundColor:[UIColor redColor]];
[img2 setBackgroundColor:[UIColor redColor]];
[img3 setBackgroundColor:[UIColor redColor]];
} else {
[img1 setBackgroundColor:[UIColor greenColor]];
[img2 setBackgroundColor:[UIColor greenColor]];
[img3 setBackgroundColor:[UIColor greenColor]];
}
}
}
@property (nonatomic, weak) UIImage *imgFile1;
@property (nonatomic, weak) UIImage *imgFile2;
@property (nonatomic, weak) UIImage *imgFile3;