popView
2016-09-14 14:13
274 查看
#import <UIKit/UIKit.h>
@protocol XPPopViewDelegate <NSObject>
- (void)returnSelectedIndexPath:(NSInteger )index;
@end
@interface XPPopView :
UIView
@property (nonatomic,strong)
NSArray *dateSource;
@property (nonatomic,assign)
id<XPPopViewDelegate> aDelegate;
- (instancetype) initWithisLeft:(BOOL)isLeft;
- (void)hideView ;
- (void)showView ;
@end
#import "XPPopView.h"
@interface
XPPopView () <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)
UITableView *tableView;
@property (nonatomic,strong)
UIImageView *view;
@property (nonatomic,assign)
BOOL isLeft;
@end
@implementation XPPopView
- (instancetype) initWithisLeft:(BOOL)isLeft {
if (self = [superinit] ) {
_isLeft = isLeft;
self.layer.shadowColor = [UIColorblackColor].CGColor;
self.layer.shadowOffset =CGSizeMake(-3,3);
self.layer.shadowOpacity =0.5;
self.layer.shadowRadius =4;
[self addSubview:self.view];
[self addSubview:self.tableView];
}
return
self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return_dateSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"CellID"];
cell.backgroundColor = [UIColorblackColor];
cell.textLabel.textColor = [UIColorwhiteColor];
cell.textLabel.font = [UIFontsystemFontOfSize:15];
cell.textLabel.textAlignment =NSTextAlignmentCenter;
cell.textLabel.text =_dateSource[indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPathanimated:YES];
if (_aDelegate) {
[_aDelegate
returnSelectedIndexPath:indexPath.row];
}
[self hideView];
}
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableViewalloc]
initWithFrame:CGRectMake(0,5,
CGRectGetWidth(self.bounds),CGRectGetHeight(self.bounds)
-5)
style:UITableViewStylePlain];
_tableView.delegate =self;
_tableView.dataSource =self;
_tableView.rowHeight =40;
_tableView.bounces =NO;
_tableView.separatorStyle =UITableViewCellSelectionStyleNone;
_tableView.layer.cornerRadius =10;
_tableView.layer.masksToBounds =YES;
_tableView.backgroundColor = [UIColorblackColor];
[_tableViewregisterClass:[UITableViewCellclass]
forCellReuseIdentifier:@"CellID"];
}
return_tableView;
}
- (UIImageView *)view {
if (!_view) {
_view = [[UIImageViewalloc]
init];
}
return _view;
}
- (void)hideView {
__weak typeof (self) weakSelf =self;
[UIViewanimateWithDuration:0.3animations:^{
weakSelf.layer.opacity =0;
}];
[UIViewcommitAnimations];
}
- (void)showView {
if (self.layer.opacity ==1) {
[self hideView];
return;
}
__weak typeof (self) weakSelf =self;
[UIViewanimateWithDuration:0.3animations:^{
weakSelf.layer.opacity =1;
}];
[UIViewcommitAnimations];
}
- (void)setDateSource:(NSArray *)dateSource {
_dateSource = [NSArrayarrayWithArray:dateSource];
NSInteger length =
0;
for (NSString *strin dateSource) {
(str.length > length) ? (length = str.length) : length;
}
if (!_isLeft) {
self.frame =CGRectMake(SCREENWIDTH -30 * length -
10,0,
30 * length,_dateSource.count *40 +
5);
}
else {
self.frame =CGRectMake(5,0,
30 * length,_dateSource.count *40 +
5);
}
[selfsetTopView];
_tableView.frame =CGRectMake(0,5,
CGRectGetWidth(self.bounds),CGRectGetHeight(self.bounds)
-5);
[_tableView
reloadData];
self.layer.opacity =0;
}
- (void)setTopView {
UIBezierPath *path = [UIBezierPathbezierPath];
if (!_isLeft) {
_view.frame =CGRectMake(CGRectGetWidth(self.frame)
-20, 0,
10, 5);
[path moveToPoint:CGPointMake(5,0)];
[path addLineToPoint:CGPointMake(10,5)];
[path addLineToPoint:CGPointMake(0,5)];
}
else {
_view.frame =CGRectMake(15,0,
10, 5);
[path moveToPoint:CGPointMake(5,0)];
[path addLineToPoint:CGPointMake(10,5)];
[path addLineToPoint:CGPointMake(0,5)];
}
[path closePath];
_view.backgroundColor = [UIColorblackColor];
CAShapeLayer *maskLayer = [CAShapeLayerlayer];
maskLayer.path = [path
CGPath];
_view.layer.mask = maskLayer;
}
@end
使用
#import "XPPopView.h"
- (XPPopView *)popView {
if (!_popView) {
_popView = [[XPPopViewalloc]
initWithisLeft:NO];
_popView.aDelegate =self;
[self.popViewsetDateSource:@[@"发表",@"我的发表",@"筛选"]];
}
return_popView;
}
协议<XPPopViewDelegate>
- (void)returnSelectedIndexPath:(NSInteger )index {
}
@protocol XPPopViewDelegate <NSObject>
- (void)returnSelectedIndexPath:(NSInteger )index;
@end
@interface XPPopView :
UIView
@property (nonatomic,strong)
NSArray *dateSource;
@property (nonatomic,assign)
id<XPPopViewDelegate> aDelegate;
- (instancetype) initWithisLeft:(BOOL)isLeft;
- (void)hideView ;
- (void)showView ;
@end
#import "XPPopView.h"
@interface
XPPopView () <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)
UITableView *tableView;
@property (nonatomic,strong)
UIImageView *view;
@property (nonatomic,assign)
BOOL isLeft;
@end
@implementation XPPopView
- (instancetype) initWithisLeft:(BOOL)isLeft {
if (self = [superinit] ) {
_isLeft = isLeft;
self.layer.shadowColor = [UIColorblackColor].CGColor;
self.layer.shadowOffset =CGSizeMake(-3,3);
self.layer.shadowOpacity =0.5;
self.layer.shadowRadius =4;
[self addSubview:self.view];
[self addSubview:self.tableView];
}
return
self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return_dateSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"CellID"];
cell.backgroundColor = [UIColorblackColor];
cell.textLabel.textColor = [UIColorwhiteColor];
cell.textLabel.font = [UIFontsystemFontOfSize:15];
cell.textLabel.textAlignment =NSTextAlignmentCenter;
cell.textLabel.text =_dateSource[indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPathanimated:YES];
if (_aDelegate) {
[_aDelegate
returnSelectedIndexPath:indexPath.row];
}
[self hideView];
}
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableViewalloc]
initWithFrame:CGRectMake(0,5,
CGRectGetWidth(self.bounds),CGRectGetHeight(self.bounds)
-5)
style:UITableViewStylePlain];
_tableView.delegate =self;
_tableView.dataSource =self;
_tableView.rowHeight =40;
_tableView.bounces =NO;
_tableView.separatorStyle =UITableViewCellSelectionStyleNone;
_tableView.layer.cornerRadius =10;
_tableView.layer.masksToBounds =YES;
_tableView.backgroundColor = [UIColorblackColor];
[_tableViewregisterClass:[UITableViewCellclass]
forCellReuseIdentifier:@"CellID"];
}
return_tableView;
}
- (UIImageView *)view {
if (!_view) {
_view = [[UIImageViewalloc]
init];
}
return _view;
}
- (void)hideView {
__weak typeof (self) weakSelf =self;
[UIViewanimateWithDuration:0.3animations:^{
weakSelf.layer.opacity =0;
}];
[UIViewcommitAnimations];
}
- (void)showView {
if (self.layer.opacity ==1) {
[self hideView];
return;
}
__weak typeof (self) weakSelf =self;
[UIViewanimateWithDuration:0.3animations:^{
weakSelf.layer.opacity =1;
}];
[UIViewcommitAnimations];
}
- (void)setDateSource:(NSArray *)dateSource {
_dateSource = [NSArrayarrayWithArray:dateSource];
NSInteger length =
0;
for (NSString *strin dateSource) {
(str.length > length) ? (length = str.length) : length;
}
if (!_isLeft) {
self.frame =CGRectMake(SCREENWIDTH -30 * length -
10,0,
30 * length,_dateSource.count *40 +
5);
}
else {
self.frame =CGRectMake(5,0,
30 * length,_dateSource.count *40 +
5);
}
[selfsetTopView];
_tableView.frame =CGRectMake(0,5,
CGRectGetWidth(self.bounds),CGRectGetHeight(self.bounds)
-5);
[_tableView
reloadData];
self.layer.opacity =0;
}
- (void)setTopView {
UIBezierPath *path = [UIBezierPathbezierPath];
if (!_isLeft) {
_view.frame =CGRectMake(CGRectGetWidth(self.frame)
-20, 0,
10, 5);
[path moveToPoint:CGPointMake(5,0)];
[path addLineToPoint:CGPointMake(10,5)];
[path addLineToPoint:CGPointMake(0,5)];
}
else {
_view.frame =CGRectMake(15,0,
10, 5);
[path moveToPoint:CGPointMake(5,0)];
[path addLineToPoint:CGPointMake(10,5)];
[path addLineToPoint:CGPointMake(0,5)];
}
[path closePath];
_view.backgroundColor = [UIColorblackColor];
CAShapeLayer *maskLayer = [CAShapeLayerlayer];
maskLayer.path = [path
CGPath];
_view.layer.mask = maskLayer;
}
@end
使用
#import "XPPopView.h"
- (XPPopView *)popView {
if (!_popView) {
_popView = [[XPPopViewalloc]
initWithisLeft:NO];
_popView.aDelegate =self;
[self.popViewsetDateSource:@[@"发表",@"我的发表",@"筛选"]];
}
return_popView;
}
协议<XPPopViewDelegate>
- (void)returnSelectedIndexPath:(NSInteger )index {
}
相关文章推荐
- CentOS6.4-RMAN定时任务备份 on 11GR2
- Linux命令中使用正则表达式
- Linux 文件系统结构介绍
- OpenGL ES教程
- Linux vi中查找字符内容的方法
- AUTOSSH,ssh反向代理
- openlayers 3(一)
- Dockerfile中CMD命令和ENTRYPOINT 命令的说明
- OpenStack vxlan隧道问题定位及解决
- CentOS 7 安装 MySQL
- -Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable and mvn script match.
- nginx内置属性
- nginx proxy_set_header设置、自定义header
- 三种使用tomcat方式
- salt安装与简单使用---基于centos6.5
- Linux启动提示“unexpected inconsistency;RUN fsck MANUALLY”
- Tomcat安装与配置
- Linux内存压力测试-memtester工具简析
- Maven实现直接部署Web项目到Tomcat7
- Win10 NotePad++ “ShellExecute failed (2): Is this command correct?”