iOS - 使用NSTimer,UIImageView只是第一次更新

时间:2013-10-26 11:50:03

标签: ios uiimageview

所有

当我使用NSTimer完成任务时,我遇到了一些问题。我需要一些帮助,谢谢。

这是问题。

当我使用NSTimer在UIImageView上显示动画时。但是当我在iphone4s中使用xcode运行代码时,UIImageView才第一次更新。当UIImageView再次出现时,它不会更新图像内容。 我检查了日志,每次都调用定时器,代码在运行时被覆盖。

NSTimer:

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];
    });
}

3 个答案:

答案 0 :(得分:0)

每次调用函数时,都会将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.
    }
}

并像这样称呼它

[self changeView:[[NSArray alloc] initWithObjects:@"Image_for_img1.suffix", @"Image_for_img2.suffix", @"Image_for_img3.suffix", nil]];

答案 1 :(得分:0)

我已经在我的计算机上测试了你的代码,我开始工作了。

在页眉文件中:

@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];

function 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;
}

功能测试:

-(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]];
    }
}
}

我使用背景颜色而不是图像,因为我不想制作用于测试的图像,只需将其更改为setImage:即可。

答案 2 :(得分:-1)

我已经解决了我的问题,解决方案如下:

我仔细检查了问题并创建了三个UIImage字段var。下面:

@property (nonatomic, weak) UIImage *imgFile1;
@property (nonatomic, weak) UIImage *imgFile2;
@property (nonatomic, weak) UIImage *imgFile3;

并将它们与xib图像资源链接。

该功能的实现并不好,我打算重新实现  下一个版本,内存分配和释放太多,可以删除一些分配和释放,以提高代码的效率。

谢谢所有人帮助我。