ios 在UITableViewController上添加悬浮视图
做一个类似微信的APP,点击”+”按钮时,要弹出一个悬浮视图(灰色),悬浮视图中为UIButton(绿色),点击UIButton时有点击事件且悬浮视图收起,点击其他空白处时悬浮视图也会收起.
1.创建一个继承UIWindow的类;
#import <UIKit/UIKit.h>
@interface GESuspendingWindow : UIWindow
@end
2.在创建的类中重写initWithFrame方法初始化背景颜色为无色,创建所需要的悬浮视图,设为灰色并添加到self中,在点击事件中添加通知;
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
//self.subView为悬浮视图
self.subView = [[UIView alloc]initWithFrame:CGRectMake(self.bounds.size.width-10-150, 64, 150 , 150)];
self.subView.backgroundColor = [UIColor lightGrayColor];
//添加buttons
[self addButtons];
[self addSubview:self.subView];
}
return self;
}
- (void)addButtons{
for (int i = 0 ; i < 5; i++) {
NSString* text = [self.textArray objectAtIndex:i];
NSString* image = [self.imageArray objectAtIndex:i];
UIButton* button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(0, i*30, self.bounds.size.width, 30);
button.tag = i;
[button setTintColor:[UIColor greenColor]];
[button setTitle:text forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[button setImageEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
[button setTitleEdgeInsets:UIEdgeInsetsMake(0, 30, 0, 0)];
[button addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
[self.subView addSubview:button];
}
}
- (void)clicked:(UIButton*)button{
NSString* info = nil;
switch (button.tag) {
case 0:
info = @"groupChat";
break;
case 1:
info = @"addFriend";
break;
case 2:
info = @"scan";
break;
case 3:
info = @"collectMoney";
break;
case 4:
info = @"helpAndFeedback";
break;
default:
break;
}
[[NSNotificationCenter defaultCenter]postNotificationName:@"suspendingViewClicked" object:nil userInfo:@{@"name":info}];
}
3.在创建的类中重写touchesBegan方法,在方法中实现通知;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[[NSNotificationCenter defaultCenter]postNotificationName:@"suspendingViewClicked" object:nil userInfo:@{@"name":@"removeSuspendingWindow"}];
}
4.在控制器中接收通知,在接收到通知的方法中实现[self.app.window makeKeyAndVisible],在”+”点击事件中创建继承UIWindow类的实例,并实现makeKeyAndVisible;
#import "GEWeixinTableViewController.h"
#import "AppDelegate.h"
#import "GESuspendingWindow.h"
@interface GEWeixinTableViewController ()
@property(nonatomic,strong)GESuspendingWindow* suwpendingWindon;
@property(nonatomic,weak)AppDelegate *app;
@end
- (void)awakeFromNib{
[super awakeFromNib];
//配置文字
self.title = @"微信";
//配置图片
self.tabBarItem.image = [UIImage imageNamed:@"line_bell"];
self.tabBarItem.selectedImage = [UIImage imageNamed:@"full_bell"];
//左按钮
UIBarButtonItem* addBar = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addSuspendingView)];
self.navigationItem.rightBarButtonItem = addBar;
self.app = [UIApplication sharedApplication].delegate;
//为显示数据的表格注册单元格
[self.searchDisplayController.searchResultsTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}
//悬浮视图点击事件
- (void)clickedAction:(NSNotification*)noti{
NSString* info = [noti.userInfo valueForKey:@"name"];
if ([info isEqualToString:@"groupChat"]) {
NSLog(@"......%@",info);
}else if ([info isEqualToString:@"addFriend"]) {
NSLog(@"......%@",info);
}else if ([info isEqualToString:@"scan"]) {
NSLog(@"......%@",info);
}else if ([info isEqualToString:@"collectMoney"]) {
NSLog(@"......%@",info);
}else if ([info isEqualToString:@"helpAndFeedback"]) {
NSLog(@"......%@",info);
}else if ([info isEqualToString:@"removeSuspendingWindow"]) {
NSLog(@"......%@",info);
}
[self.app.window makeKeyAndVisible];
self.suwpendingWindon = nil;
}
//添加悬浮视图
- (void)addSuspendingView{
self.suwpendingWindon = [[GESuspendingWindow alloc]initWithFrame:self.view.frame];
[self.suwpendingWindon makeKeyAndVisible];
}