更新标签的线程问题使UIImageview不可见

时间:2015-07-11 02:37:05

标签: ios objective-c multithreading animation uiimageview

我的情况令我感到困惑。我有一个类有2个完全独立的东西:一个动画UIImage和一个UILabel。它们有不同的插座,没有连接。

当应用运行时,它会执行以下操作:

dispatch_async(dispatch_get_main_queue(), ^{
    [self.monsterMachine setHidden:NO]; //monsterMachine is UIImageView
    [self.monsterMachine startAnimating];
});

但是当我这样做时:

    [self.futText setText:@"blah"]; // is UILabel

它导致monsterMachine UIImageView不再动画。我发现的事情:

我setText后:@ blah"我可以使用NSLog来观察并看到self.monsterMachine.isAnimated突然从1变为0,即从YES变为NO。

如果self.futText已经说" blah",那么我可以运行setText:@" blah"在它上面我想要的次数没有任何反应,只有当我将它改为某些值,而不是等于UIImageView停止动画并消失的时候

如果我不使用main_queue来显示和设置monsterMachine的动画,它将无法显示在所有位置,那么我该如何诊断或解决这个问题?

1 个答案:

答案 0 :(得分:1)

这对我来说很完美。你打电话给[self.futText setText:@" blah"];在主队列中也? 这里有一个小答案,为什么设置文本@" blah"不会停止动画: 像@" blah"等字符串存储在具有相同引用的堆栈中,只要它们具有相同的上下文即可。在这种情况下[self.futText setText:@" blah"]将不执行任何操作,因为内部实现如下:

void setText:(NSString*) text {
  if ( _text == text ) then return;//if reference is the same then do nothing
  _text = text;
  some rendering..
}