popView

#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 {



}






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值