我对 jsd 采取了不同的策略,将其添加到视图中。也许窗户更好。但是当我尝试它时,这段代码似乎做了想要的事情:
@implementation EWView
+ (void)show:(UIView *)superView {
EWView *instantView = [[EWView alloc]initWithFrame:CGRectMake(20, 20, 50, 50)];
instantView.backgroundColor = [UIColor greenColor];
[superView addSubview:instantView];
int64_t delayInSeconds = 4.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[instantView removeFromSuperview];
});
}
@end
我从 UIViewController 调用它
[EWView show:self.view];
只有在块尚未完成执行时,视图才会继续存在。当块完成时,EWView 将被释放。