1

在 Instagram 上,当我发布一张照片后,屏幕中间会出现一个小文本框,上面写着“已发布推文”,如下图所示。它会在一两秒钟后消失。它到底是什么?如何在 IOS 中构建类似的东西?谢谢!

在此处输入图像描述

4

1 回答 1

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>

于 2013-03-15T18:12:21.600 回答