[英]iOS - Using NSTimer, The UIImageView Just update at first time
所有
當我使用NSTimer完成任務時,遇到了一些問題。 我需要一些幫助,謝謝。
這是問題所在。
當我使用NSTimer在UIImageView上顯示動畫時。 但是UIImageView只是在我第一次使用xcode在iphone4s中運行代碼時更新。 當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];
});
}
每次調用該函數時,請將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]];
我已經在計算機上測試了您的代碼,並使其正常運行。
在頭文件中:
@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];
函數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:
。
我已經解決了我的問題,下面是解決方案:
我仔細檢查了問題,並創建了三個UIImage字段var。 下面:
@property (nonatomic, weak) UIImage *imgFile1;
@property (nonatomic, weak) UIImage *imgFile2;
@property (nonatomic, weak) UIImage *imgFile3;
並將它們與xib圖像資源鏈接。
該功能的實現不好,我計划在下一個版本中重新實現它,內存分配和釋放過多,可以刪除一些分配和釋放以提高代碼效率。
謝謝大家的幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.