1

全部

当我使用 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];
    });
}
4

3 回答 3

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

功能网络状态图像视图:

- (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:它应该可以工作。

于 2013-10-28T11:41:08.020 回答
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]];
于 2013-10-26T12:37:00.360 回答
-1

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

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

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

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

这个特性的实现不好,我打算下个版本重新实现,内存分配和释放太多,可以去掉一些分配和释放,让代码更有效率。

感谢所有帮助我的人。

于 2013-10-29T10:56:05.560 回答