产品要求这样的界面,效果的时候,采用UIBezierPath 绘图上下文的方式进行
@interface LCPopView :UIView<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSArray *titleArray;//列表信息
@property (nonatomic,assign) CGPoint showPoint; //指针指向
@property (nonatomic,strong) UIButton *handerView;
-(void)show;
-(void)dismiss;
-(void)dismiss:(BOOL)animated;
-(instancetype)initWithFrame:(CGRect)frame WithShowPoint:(CGPoint) point;
@property (nonatomic,copy) void (^selectRowAtIndex)(NSInteger index);
@end
//
// LCPopView.m
// LCPopover
//
// Created by 王伟志 on 16/2/23.
// Copyright (c) 2016年 王伟志. All rights reserved.
//
#import "LCPopView.h"
#define TITLE_FONT [UIFont systemFontOfSize:16]
#define kArrowHeight 10.f
#define SPACE 2.f
#define TOP_SPACE 16.f
#define ROW_HEIGHT 40.f
#define kArrowCurvature 6.f
#define RGB(r, g, b) [UIColor colorWithRed:(r)/255.f green:(g)/255.f blue:(b)/255.f alpha:1.f]
@interface LCPopView ()
@end
@implementation LCPopView
-(UITableView *)tableView
{
if (_tableView !=nil) {
return _tableView;
}
CGRect rect = self.frame;
rect.origin.x =SPACE;
rect.origin.y =SPACE;
rect.size.width =0;
rect.size.height =0;
self.tableView = [[UITableViewalloc] initWithFrame:rectstyle:UITableViewStylePlain];
_tableView.delegate =self;
_tableView.dataSource =self;
_tableView.alwaysBounceHorizontal =NO;
_tableView.alwaysBounceVertical =NO;
_tableView.showsHorizontalScrollIndicator =NO;
_tableView.showsVerticalScrollIndicator =NO;
_tableView.scrollEnabled =NO;
_tableView.backgroundColor = [UIColorclearColor];
return_tableView;
}
-(instancetype)initWithFrame:(CGRect)frame
{
self = [superinitWithFrame:frame];
if (self) {
self.backgroundColor = [UIColorclearColor];
}
return self;
}
-(instancetype)initWithFrame:(CGRect)frame WithShowPoint:(CGPoint)point
{
self = [superinitWithFrame:frame];
if (self) {
self.backgroundColor = [UIColorclearColor];
self.showPoint = point;
}
return self;
}
-(void)drawRect:(CGRect)rect
{
//设置弹框大小.tableView也设置
CGRect frame =CGRectZero;
frame.size.height = [self.titleArraycount] * ROW_HEIGHT +SPACE * 2 +kArrowHeight;
for (NSString *titlein self.titleArray)
{
//计算字体的宽度
CGFloat width = [titlesizeWithAttributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:19]}].width;
NSLog(@"%f",width);
frame.size.width =MAX(width, frame.size.width) +9;
}
frame =CGRectMake(_showPoint.x - frame.size.width,0, frame.size.width, frame.size.height);
self.tableView.frame =CGRectMake(_showPoint.x - frame.size.width +kArrowHeight + SPACE,self.tableView.frame.origin.y +TOP_SPACE, frame.size.width, frame.size.height);
[selfdrawRoundedRectPath:frame];
[selfaddSubview:self.tableView];
}
- (void)drawRoundedRectPath:(CGRect)frame {
float yMin = CGRectGetMinY(frame) +TOP_SPACE;
CGPoint arrowPoint = CGPointMake(self.showPoint.x,0);
//绘制弹框
UIBezierPath * path = [UIBezierPathbezierPathWithRoundedRect:CGRectMake(frame.origin.x +TOP_SPACE, frame.origin.y +TOP_SPACE, frame.size.width, frame.size.height)cornerRadius:10];
//贝塞尔曲线
[pathmoveToPoint:CGPointMake(self.showPoint.x -kArrowHeight, yMin)];//左上角
[pathaddCurveToPoint:arrowPoint
controlPoint1:CGPointMake(arrowPoint.x -kArrowHeight + kArrowCurvature,TOP_SPACE)
controlPoint2:arrowPoint];//actual arrow point
[pathaddCurveToPoint:CGPointMake(arrowPoint.x +kArrowHeight, TOP_SPACE)
controlPoint1:arrowPoint
controlPoint2:CGPointMake(arrowPoint.x +kArrowHeight - kArrowCurvature,TOP_SPACE)];
// 设置填充颜色
UIColor *fillColor = [UIColorwhiteColor];
[fillColorset];
[pathfill];
// 设置画笔颜色
UIColor *strokeColor = [UIColorwhiteColor];
[strokeColorset];
//根据我们设置的各个点连线
[pathstroke];
}
#pragma MARK - 整个屏幕添加动画
-(void)show
{
self.handerView = [UIButtonbuttonWithType:UIButtonTypeCustom];
[_handerViewsetFrame:[UIScreenmainScreen].bounds];
[_handerViewsetBackgroundColor:[UIColorclearColor]];
[_handerViewaddTarget:selfaction:@selector(dismiss)forControlEvents:UIControlEventTouchUpInside];
[_handerView addSubview:self];
UIWindow *window = [UIApplicationsharedApplication].keyWindow;
[windowaddSubview:_handerView];
self.alpha =0.f;
self.transform =CGAffineTransformMakeScale(0.1f,0.1f);
[UIViewanimateWithDuration:0.2fdelay:0.foptions:UIViewAnimationOptionCurveEaseInOutanimations:^{
self.transform =CGAffineTransformMakeScale(1.05f,1.05f);
self.alpha =1.f;
}completion:^(BOOL finished) {
[UIViewanimateWithDuration:0.08fdelay:0.foptions:UIViewAnimationOptionCurveEaseInOutanimations:^{
self.transform =CGAffineTransformIdentity;
}completion:nil];
}];
}
-(void)dismiss
{
[selfdismiss:YES];
}
-(void)dismiss:(BOOL)animate
{
if (!animate) {
[_handerViewremoveFromSuperview];
return;
}
[UIViewanimateWithDuration:0.3fanimations:^{
self.transform =CGAffineTransformMakeScale(0.1f,0.1f);
self.alpha =0.f;
}completion:^(BOOL finished) {
[_handerViewremoveFromSuperview];
}];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[selfdismiss];
}
#pragma mark - UITableView DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_titleArraycount];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier =@"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier];
}
cell.backgroundColor = [UIColorclearColor];
// cell.backgroundView = [[UIView alloc] init];
// cell.backgroundView.backgroundColor = RGB(255, 255, 255);
cell.textLabel.font = [UIFontfontWithName:@"FZLTXHJW--GB1-0"size:16];
cell.textLabel.text = [_titleArrayobjectAtIndex:indexPath.row];
if ([[UIDevicecurrentDevice].systemVersionfloatValue] >= 7.0) {
cell.separatorInset =UIEdgeInsetsMake(0,0, 0, 0);
}
return cell;
}
#pragma mark - UITableView Delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPathanimated:NO];
if (self.selectRowAtIndex) {
self.selectRowAtIndex(indexPath.row);
}
[selfdismiss:YES];
}
@end