2013-03-11 116 views
3

我有一个UILabe为我的匹配纸牌游戏写下“好匹配!”例如,如果您有匹配项,并且想在通知用户消息后使其消失。如何让UILabel文字在几秒钟后消失?

这是什么方法?我可以让它流行起来..?谢谢。

这是我的updateUI方法:

-(void) updateUI { 

    for (UIButton *cardButton in self.cardButtons) { 
     Card *card = [self.game cardAtIndex:[self.cardButtons indexOfObject:cardButton]]; 
     [cardButton setTitle:card.contents forState:UIControlStateSelected]; 
     [cardButton setTitle:card.contents forState:UIControlStateSelected|UIControlStateDisabled]; 
     cardButton.selected = card.isFaceUp; 
     cardButton.enabled = !card.unplayble; 
     if (card.unplayble) { 
      cardButton.alpha = 0.1; 
     } 
     self.scoreCounter.text = [NSString stringWithFormat:@"Score: %d", self.game.score]; 


     if (self.game.notification) { 
      [UIView animateWithDuration:2 animations:^{ 
       self.notificationLabel.text = [NSString stringWithFormat:@"%@", self.game.notification]; 
       self.notificationLabel.alpha = 0; 
      }]; 
     } 

    } 
} 
+0

阅读[核心动画节目指南](http://developer.apple.com/documentation/Cocoa/Conceptual/CoreAnimation_guide/Introduction/Introduction.html),就是这样。 – CodaFi 2013-03-11 22:49:39

回答

3

这将消逝的标签超过两秒钟。更改animateWithDuration:但是到长你想

if (self.game.notification == nil) { 
[UIView animateWithDuration:2 animations:^{ 
self.notificationLabel.alpha = 0; 
}]; 
} 

如果要更改文本,试试这个(我现在不能测试它,所以它可能无法正常工作)

[UIView animateWithDuration:2 animations:^{ 
self.notificationLabel.text = @"text you want to change it to"; 
}]; 

然后当你想再次使用的标签,你必须设置self.notificationLabel.alpha回1.因此,尝试

if (self.game.notification) { 
self.notificationLabel.alpha = 1; 
[UIView animateWithDuration:2 animations:^{ 
self.notificationLabel.text = [NSString stringWithFormat:@"%@", self.game.notification]; 
self.notificationLabel.alpha = 0; 
}]; 
} 
+0

请参阅我添加的代码行,谢谢! @ChrisLoonam – JohnBigs 2013-03-11 22:57:49

+0

@JohnBigs做编辑帮助? – 2013-03-11 23:02:00

+0

它确实有助于改变标签的状态,但我试图改变通知中的文本(nsstring)。因为得到这个字符串我使用了self.notificationLabel.text ..你知道我的意思吗..? @ ChrisLoonam – JohnBigs 2013-03-11 23:03:52

-2

我不认为这是可能的。很可能,您需要在界面生成器中用NSView替换UILabel,并使用其中包含的绘图函数来绘制透明文本。

+0

这是完全错误的。 iOS上不存在NSView。 – 2013-03-11 22:53:26

+0

哎呦。不太熟悉iOS,认为它与OS X类似。我不知道,也许尝试使用Core Animation进行调整? – SevenBits 2013-03-11 22:56:36