前面写了弹出动画两个,今天做商城时又用到了,看着这个用着蛮普遍的,所以记了下来
//
// mallMoreView.h
// XQB
//
// Created by City--Online on 15/7/6.
//
//
#import <UIKit/UIKit.h>
typedef void (^SelectMallMoreMenu)(NSInteger index);
@interface mallMoreView : UIView
//单例
+ (mallMoreView *)sharedManager;
//block传值
@property(nonatomic,strong) SelectMallMoreMenu selectMallMoreMenu;
@property(nonatomic,strong) NSArray *titles;
@property(nonatomic,strong) UITableView *tableView;
//window全屏显示
-(void)showInWindow;
// View中显示
-(void)showInView:(UIView*)view;
//在父视图view的相对位置为Frame
-(void)showInView:(UIView*)view withFrame:(CGRect)frame;
//消失视图
-(void)dismissView;
@end
//
// mallMoreView.m
// XQB
//
// Created by City--Online on 15/7/6.
//
//
#import "mallMoreView.h"
#import "Global.h"
@interface mallMoreView ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,assign) BOOL open;
@end
@implementation mallMoreView
+ (mallMoreView *)sharedManager
{
static mallMoreView *managerInstance = nil;
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
managerInstance = [[self alloc] init];
managerInstance.backgroundColor=[UIColor colorWithWhite:0.1 alpha:0.1];
});
return managerInstance;
}
-(void)showInWindow
{
[self showInView:[UIApplication sharedApplication].keyWindow];
}
-(void)showInView:(UIView*)view
{
[self showInView:view withFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
}
-(void)showInView:(UIView*)view withFrame:(CGRect)frame
{
if (_open) {
[self dismissView];
return;
}
_open=true;
UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissView)];
[self addGestureRecognizer:tapGesture];
self.frame=CGRectMake(0, 0, frame.size.width, frame.size.height);
self.alpha=0.0;
if (_tableView==nil) {
_tableView=[[UITableView alloc]init];
}
_tableView.delegate=self;
_tableView.dataSource=self;
_tableView.backgroundColor=[UIColor colorWithWhite:0.2 alpha:0.5];
_tableView.tableHeaderView=[[UIView alloc]initWithFrame:CGRectZero];
_tableView.tableFooterView=[[UIView alloc]initWithFrame:CGRectZero];
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
//动画效果
[UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.alpha=1.0;
_tableView.hidden=NO;
} completion:nil
];
[view addSubview:self];
//要将TableView添加到view而不是self,否则不能选中TableView
[view addSubview:_tableView];
}
-(void)dismissView
{
_open=false;
[UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.alpha=0.0;
_tableView.hidden=YES;
} completion:^(BOOL finished) {
[self removeFromSuperview];
[_tableView removeFromSuperview];
}];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _titles.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.backgroundColor=[UIColor clearColor];
cell.textLabel.text=[_titles objectAtIndex:indexPath.row];
cell.textLabel.textColor=[UIColor whiteColor];
cell.textLabel.font=[UIFont systemFontOfSize:18];
cell.textLabel.textAlignment=NSTextAlignmentCenter;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (_titles.count<=0) {
return 0;
}
return tableView.frame.size.height/_titles.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_selectMallMoreMenu(indexPath.row);
[self dismissView];
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
#ifdef __IPHONE_8_0
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]){
[cell setPreservesSuperviewLayoutMargins:NO];
}
#endif
}
@end
-(void)popMallMoreView:(id)sender
{
mallMoreView *moreView=[mallMoreView sharedManager];
moreView.tableView=[[UITableView alloc]init];
moreView.tableView.frame=CGRectMake(MAINWIDTH-130, 0, 120, 150);
moreView.titles=@[@"回到首页",@"闪购订单",@"收货地址"];
moreView.selectMallMoreMenu=^(NSInteger index)
{
NSLog(@"%ld",index);
};
[moreView showInView:self.view];
}