2013-03-15 70 views
1

在Instagram上,当我推送一张照片后,屏幕中间会出现一个小文本框,显示“Tweet posted”,如下图所示。它会在一两秒钟后消失。究竟是什么?我如何在iOS中构建类似的东西?谢谢!什么是几秒钟后消失的ios文本通知

enter image description here

+0

我想这只是一个插入了一些文本的UIView,它是以编程方式控制的,而不是标准控件。 – DVG 2013-03-15 18:06:29

+0

我希望这是IOS的标准控制。这将节省一些工作。任何人都知道这件事? – zhengwx 2013-03-15 18:07:40

回答

2

这的确是一个标准的控制。它叫做UILabel

NSString *text = @"Tweet posted"; 
UIFont *font = [UIFont boldSystemFontOfSize:20.0f]; 
CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(320, 100)]; 

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, size.width + 20, size.height + 20)]; 
label.textColor = [UIColor whiteColor]; 
label.backgroundColor = [[UIColor darkGrayColor] colorWithAlphaComponent:0.8]; 
label.textAlignment = NSTextAlignmentCenter; 
label.font = font; 
label.text = text; 
label.layer.cornerRadius = 5.0f; 
label.shadowColor = [UIColor darkGrayColor]; 

label.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2); 

[self.view addSubview:label]; 

double delayInSeconds = 2.0; 
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); 
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
    [UIView animateWithDuration:0.5f animations:^{ 
     label.alpha = 0.0f; 
    } completion:^(BOOL finished) { 
     [label removeFromSuperview]; 
    }]; 
}); 

没什么特别的,只是标准的属性和一些图层“魔法”。和GCD。
代码应该是不言自明的。别忘了#import <QuartzCore/QuartzCore.h>

+0

嘿马提亚斯,谢谢你的回答。我知道我们可以用UILabel来构建它。我只是想知道IOS中是否有标准可以为我做所有这些样式和动画。 – zhengwx 2013-03-15 18:17:59

+0

比UILabel更标准吗?这已经是你需要的一切。但是我确定有些人做了“我做你想做的一切”--Cocoapods-Thingie为此使用了1000行代码。 – 2013-03-15 18:20:25

+0

我阅读了http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/mobilehig/UIElementGuidelines/UIElementGuidelines.html#//apple_ref/doc/uid/TP40006556-CH13-SW41,但没有找到什么。所以我会接受你的代码。谢谢! – zhengwx 2013-03-15 18:42:14