一、简介
有时候我们在做程序的时候,项目要求我们给用户提示信息的时候,从顶部自动下拉下来,前些日子我接到过这样的需求,感觉用第三方的集成进来,如果以后修改样式的话或者其它的需求会很麻烦,所以我自己写了一个,代码有点简单,请大家指导一下,有什么意见或者有什么疑问可以直接评论询问我,很乐意和大家一起探讨学习.
二、代码
1.创建一个单例,我是继承NSOject的
+(instancetype)shareInstance{ static ShowChatStateView *stateView = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ stateView = [[ShowChatStateView alloc]init]; }); return stateView;}
2.初始化UIWindow
-(instancetype)init{ self = [super init]; id <UIapplicationDelegate> delegate = [[UIApplication sharedApplication] delegate]; if ([delegate respondsToSelector:@selector(window)]) { self.window = [delegate performSelector:@selector(window)]; } else { self.window = [[UIApplication sharedApplication] keyWindow]; } [self createUI]; return self; }3.创建UILabel,使label唯一
-(void)createUI{ if (self.label == nil) { self.label = [[UILabel alloc]initWithFrame:CGRectMake(0, -64, SCREEN_WIDTH, 64)]; self.label.textAlignment = NSTextAlignmentCenter; self.label.textColor = [UIColor whiteColor]; self.label.backgroundColor = [MyTool colorWithString:@"50D2C2"]; self.label.font = [UIFont systemFontOfSize:Font(14.0f)]; self.label.alpha = 0.f; } if (self.label.superview == nil) { [self.window addSubview:self.label]; }}4.将文字显示在Label上,并且从上面自动下拉,隔段时间(自己设定)在弹回去
-(void)show{ if (!self.label.alpha) { self.label.alpha = 1.f; CGRect rect = self.label.frame; rect.origin.y = 0; [UIView animateWithDuration:0.5 animations:^{ self.label.frame = rect; } completion:^(BOOL finished) { CGRect rect = self.label.frame; rect.origin.y = -64; [UIView animateWithDuration:0.5 delay:1.0 options:UIViewAnimationOptionCurveEaseIn animations:^{ self.label.frame = rect; } completion:^(BOOL finished) { self.label.alpha = 0.f; }]; }]; }}-(void)showLabelWithString:(NSString *)string{ self.label.text = string; [self show];}5.调用方法
[[ShowChatStateView shareInstance] showLabelWithString:@"您已签过到"];
三、总结
我是利用UIWindow来实现下拉,这样有导航栏也不影响view的向下弹出,如果你有更好的方法,欢迎讨论,下面是效果图:
新闻热点
疑难解答