日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

自定义PopView

發布時間:2023/11/30 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自定义PopView 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

改代碼是參考一個Demo直接改的,代碼中有一些漏洞,如果發現其他的問題,可以下方直接留言

.h文件

#import <UIKit/UIKit.h> typedef void(^PopoverBlock)(NSInteger index); @interface CustomPopView : UIView //@property(nonatomic,copy)void(^block)(int index); //-(void)setDataArr:(NSArray *)titleArr withView:(id *)view withBlock:(void(^)(NSString *a))block; @property (nonatomic, copy) NSArray *menuTitles; @property(nonatomic,copy)void(^PopoverHiddenBlock)(BOOL isHidden ); - (void)showFromView:(id)aView selected:(PopoverBlock)selected; @end@interface PopoverArrow : UIView@end

?.m文件

#import "CustomPopView.h" // 字體大小 #define kPopoverFontSize 14.f// 十六進制顏色 #define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]#define SCREEN_W [UIScreen mainScreen].bounds.size.width #define SCREEN_H [UIScreen mainScreen].bounds.size.height// 箭頭高度 #define kArrowH 8 // 箭頭寬度 #define kArrowW 15 //每行的高度 #define CELL_HEIGHT 38 // #define Identifier @"cell"// 邊框顏色 #define kBorderColor UIColorFromRGB(0xE1E2E3) @interface CustomPopView () <UITableViewDelegate, UITableViewDataSource> {PopoverBlock _selectedBlock;UIView *_backgroundView;PopoverArrow *_arrowView; }@property (nonatomic, retain) UITableView *tableView;@end@implementation CustomPopView - (instancetype)initWithFrame:(CGRect)frame {if (!(self = [super initWithFrame:frame])) return nil;self.backgroundColor = [UIColor clearColor];// 箭頭_arrowView = [PopoverArrow new];[self addSubview:_arrowView];// tableView放在箭頭底下, 用于箭頭擋住tableView邊框[self insertSubview:self.tableView belowSubview:_arrowView];return self; }- (void)layoutSubviews {[super layoutSubviews];// 設置tableView默認的分割線起終點位置if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {[self.tableView setSeparatorInset:UIEdgeInsetsZero];}if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {[self.tableView setLayoutMargins:UIEdgeInsetsZero];}self.tableView.layer.cornerRadius = 5.f;self.tableView.layer.borderColor = kBorderColor.CGColor;self.tableView.layer.borderWidth = 1.f; }#pragma mark -- getter- (UITableView *)tableView {if (_tableView) return _tableView;_tableView = [UITableView new];_tableView.delegate = self;_tableView.dataSource = self;_tableView.rowHeight = CELL_HEIGHT;_tableView.backgroundColor = [UIColor whiteColor];_tableView.showsVerticalScrollIndicator = NO;[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:Identifier];_tableView.tableFooterView = UIView.new;return _tableView; }#pragma mark -- delegate- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return self.menuTitles.count; }- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];cell.textLabel.font = [UIFont systemFontOfSize:kPopoverFontSize];cell.textLabel.text = [self.menuTitles objectAtIndex:indexPath.row];return cell; }- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {[cell setSeparatorInset:UIEdgeInsetsZero];}if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {[cell setLayoutMargins:UIEdgeInsetsZero];} }- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {[UIView animateWithDuration:0.25 animations:^{self.alpha = 0;} completion:^(BOOL finished) {[_backgroundView removeFromSuperview];_backgroundView = nil;if (_selectedBlock) {_selectedBlock(indexPath.row);}[self removeFromSuperview];}]; }#pragma mark -- private // 點擊透明層隱藏 - (void)hide {[UIView animateWithDuration:0.25 animations:^{self.alpha = 0;} completion:^(BOOL finished) {[_backgroundView removeFromSuperview];_backgroundView = nil;if(self.PopoverHiddenBlock){self.PopoverHiddenBlock(YES);}[self removeFromSuperview];}]; }#pragma mark -- public/*!* @author lifution** @brief 顯示彈窗** @param aView 箭頭指向的控件* @param selected 選擇完成回調*/ - (void)showFromView:(id)aView selected:(PopoverBlock)selected {if (selected) _selectedBlock = selected;//aView只能傳兩種參數,一種是UIView 另一種UIBarButtonItemif(!([aView isKindOfClass:[UIView class]] || [aView isKindOfClass:[UIBarButtonItem class]])){return;}UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;// 背景遮擋_backgroundView = UIView.new;_backgroundView.frame = keyWindow.bounds;_backgroundView.backgroundColor = [UIColor blackColor];_backgroundView.alpha = 0.2;_backgroundView.userInteractionEnabled = YES;[_backgroundView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)]];[keyWindow addSubview:_backgroundView];// 刷新數據更新contentSize[self.tableView reloadData];// 獲取觸發彈窗的按鈕在window中的坐標CGRect triggerRect ;if([aView isKindOfClass:[UIView class]]){UIView *view = (UIView *)aView;triggerRect = [view convertRect:view.bounds toView:keyWindow];}else{UIView *bgView = [aView valueForKey:@"_view"];triggerRect = [bgView convertRect:bgView.bounds toView: keyWindow];}// 箭頭指向的中心點CGFloat arrowCenterX = CGRectGetMaxX(triggerRect)-CGRectGetWidth(triggerRect)/2;// 取得標題中的最大寬度CGFloat maxWidth = 0;for (id obj in self.menuTitles) {if ([obj isKindOfClass:[NSString class]]) {CGSize titleSize = [obj sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kPopoverFontSize]}];if (titleSize.width > maxWidth) {maxWidth = titleSize.width;}}}CGFloat curWidth = ((maxWidth+80)>SCREEN_W-30)?SCREEN_W-30:(maxWidth+80);CGFloat curHeight = CELL_HEIGHT*self.menuTitles.count+kArrowH;CGFloat curX = arrowCenterX-curWidth/2;CGFloat curY = CGRectGetMaxY(triggerRect)+10;// 如果箭頭指向點距離屏幕右邊減去5px不足curWidth的一半的話就重新設置curXif ((SCREEN_W-arrowCenterX-5)<curWidth/2) {curX = curX-(curWidth/2-(SCREEN_W-arrowCenterX-5));}// 如果箭頭指向點距離屏幕左邊加上5px不足curWidth的一半的話就重新設置curXif (arrowCenterX+5<curWidth/2) {curX = curX+(curWidth/2-arrowCenterX)+5;}//如果高度大于10行,則最高按10計算if(curHeight>CELL_HEIGHT*10+kArrowH){curHeight = CELL_HEIGHT*10+kArrowH;}self.frame = CGRectMake(curX, curY - 18, curWidth, curHeight);_arrowView.frame = CGRectMake(arrowCenterX-curX-kArrowW/2, 0, kArrowW, kArrowH+1);// 箭頭高度 +1 遮擋住tableView的邊框self.tableView.frame = CGRectMake(0, kArrowH, curWidth,curHeight - kArrowH );[keyWindow addSubview:self];self.alpha = 0;[UIView animateWithDuration:0.3 animations:^{self.alpha = 1;}]; }@end// 箭頭 @implementation PopoverArrow- (instancetype)initWithFrame:(CGRect)frame {if (!(self = [super initWithFrame:frame])) return nil;self.backgroundColor = [UIColor clearColor];return self; }// Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect {[super drawRect:rect];// Drawing codeCGSize curSize = rect.size;CGContextRef context = UIGraphicsGetCurrentContext();CGContextSetLineWidth(context, 1);CGContextSetStrokeColorWithColor(context, kBorderColor.CGColor);CGContextSetFillColorWithColor(context, UIColor.whiteColor.CGColor);CGContextBeginPath(context);CGContextMoveToPoint(context, 0, curSize.height);CGContextAddLineToPoint(context, curSize.width/2, 0);CGContextAddLineToPoint(context, curSize.width, curSize.height);CGContextDrawPath(context, kCGPathFillStroke); }@end

?使用:

view = [CustomPopView new]; view.menuTitles = @[@"1",@"2",@"3"]; UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addBtnClick:)]; self.navigationItem.rightBarButtonItem = item;-(void)addBtnClick:(UIBarButtonItem *)item{[view showFromView:item selected:^(NSInteger index) {}]; }

?

轉載于:https://www.cnblogs.com/hualuoshuijia/p/9983821.html

總結

以上是生活随笔為你收集整理的自定义PopView的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。