什么是ios文本通知在几秒钟后消失

时间:2013-03-15 18:03:29

标签: ios

在Instagram上,在我发推照片后,屏幕中间会出现一个小文字框,上面写着“推文发布”,如下图所示。它会在一两秒后消失。究竟是什么?我怎样才能在IOS中构建类似的东西?谢谢!

enter image description here

1 个答案:

答案 0 :(得分: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>