当前位置: 首页 > 工具软件 > PopView > 使用案例 >

popView

章昊
2023-12-01

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



}






相关阅读

相关文章

相关问答