其实要实现这样的效果很简单:
可能不少同窗试过,建立一个透明而且背景为黑色的backgroundView,而后覆盖到self.view上,而后再建立一个提示视图promptView,放到backgroundView上,可是你会发现由于backgroundView设置了alpha值,因此promptView也有他父视图的alpha值,实现的效果就不是咱们这样的。其实正确的思路是:
1. 建立一个backgroundView,设置alpha为0.1,背景色为黑色,而后添加到self.view(你须要放置的位置);
2. 建立一个promptView,里面你有想要作的任何操做,好比有文子,有加载动画等等你想要的视图,而后添加到self.view上,注意,这里的promptView和backgroundView是兄弟关系,不是父子关系;
这样就完成了一个相似于弹出窗口。动画
咱们能够把这个功能性的视图封装成一个view叫PromptView,
步骤1:在他的initWithFrame方法中,写ui
- (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { _label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)]; _label.center = self.center; _label.backgroundColor = [UIColor whiteColor]; _label.text = @"这是提示信息"; _label.textAlignment = 1; self.backgroundColor = [UIColor clearColor]; _backgroundView = [[UIView alloc] initWithFrame:self.bounds]; _backgroundView.backgroundColor =[UIColor blackColor]; _backgroundView.alpha = 0.1; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [_backgroundView removeFromSuperview]; [_label removeFromSuperview]; [self removeFromSuperview]; }); } return self; }
步骤2:写一个弹出方法-(void)showPromptViewOnView:(UIView *)viewspa
-(void)showPromptViewOnView:(UIView *)view
{
[view addSubview:_backgroundView];
[view addSubview:_label];
[UIView animateWithDuration:0.2 animations:^{
_label.frame =CGRectMake(100, _backgroundView.center.y, 200, 40);
}];
}
而后在你想要让他出现的地方建立而且调用便可code
PromptView *view = [[PromptView alloc] initWithFrame:self.view.bounds];
[view showPromptViewOnView:self.view];