实现很简单,废话不多说:
.h 文件中声明
/**
* tableview 要添加提示的列表tableview
* tishi 自定义要提示的文字内容
* time 提示条停留的时间
* height 提示条的高度
* textColor 提示文字的颜色
* backgroundColor 提示框背景颜色
* font 提示文字的大小
*/
+ (void)addHeadTipWithTableView:(UITableView *)tableView withTishi:(NSString *)tishi withTime:(NSInteger)time tishiHeight:(CGFloat)height textColor:(UIColor *)textColor backgroundColor:(UIColor *)backgroundColor font:(UIFont *)font;
.m 文件中实现
- (instancetype)init{
self = [super init];
if (self) {
}
return self;
}
+ (void)addHeadTipWithTableView:(UITableView *)tableView withTishi:(NSString *)tishi withTime:(NSInteger)time tishiHeight:(CGFloat)height textColor:(UIColor *)textColor backgroundColor:(UIColor *)backgroundColor font:(UIFont *)font{
UILabel *headLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 0)];
[UIView animateWithDuration:0.5 animations:^{
headLabel.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, height);
tableView.tableHeaderView = headLabel;
}];
headLabel.textColor = textColor;
headLabel.backgroundColor = backgroundColor;
headLabel.font = font;
headLabel.textAlignment = NSTextAlignmentCenter;
headLabel.text = tishi;
dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time/*延迟执行时间*/ * NSEC_PER_SEC));
dispatch_after(delayTime, dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.5 animations:^{
headLabel.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 0);
tableView.tableHeaderView = headLabel;
}completion:^(BOOL finished) {
tableView.tableHeaderView = nil;
}];
});
}
截图:
![](https://img.haomeiwen.com/i2530231/a01c8e94555214af.png)
网友评论