iOS仿微信黑色弹出窗

效果图:

PopoverView.h

1
2
3
4
5
6
7
8
9
10
11
12
13
#import <UIKit/UIKit.h>

@interface PopoverView : UIView

-(id)initWithPoint:(CGPoint)point titles:(NSArray *)titles images:(NSArray *)images;
-(void)show;
-(void)dismiss;
-(void)dismiss:(BOOL)animated;

@property (nonatomic, copy) UIColor *borderColor;
@property (nonatomic, copy) void (^selectRowAtIndex)(NSInteger index);

@end

PopoveView.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#import "PopoverView.h"


#define kArrowHeight 10.f
#define kArrowCurvature 6.f
#define SPACE 2.f
#define ROW_HEIGHT 44.f
#define TITLE_FONT [UIFont systemFontOfSize:16]
#define RGB(r, g, b) [UIColor colorWithRed:(r)/255.f green:(g)/255.f blue:(b)/255.f alpha:1.f]

@interface PopoverView ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSArray *imageArray;
@property (nonatomic) CGPoint showPoint;
@property (nonatomic, strong) UIButton *handerView;

@end

@implementation PopoverView

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.borderColor = [UIColor blackColor];
self.backgroundColor = [UIColor clearColor];
}

return self;
}

-(id)initWithPoint:(CGPoint)point titles:(NSArray *)titles images:(NSArray *)images
{
self = [super init];
if (self) {
self.showPoint = point;
self.titleArray = titles;
self.imageArray = images;

self.frame = [self getViewFrame];

[self addSubview:self.tableView];
}
return self;
}

-(CGRect)getViewFrame
{

UIWindow *window = [UIApplication sharedApplication].keyWindow;

CGRect frame = CGRectZero;

frame.size.height = [self.titleArray count] * ROW_HEIGHT + SPACE + kArrowHeight;

for (NSString *title in self.titleArray) {
CGFloat width = [title sizeWithFont:TITLE_FONT constrainedToSize:CGSizeMake(300, 100) lineBreakMode:NSLineBreakByCharWrapping].width;
frame.size.width = MAX(width, frame.size.width);
}

if ([self.titleArray count] == [self.imageArray count]) {
frame.size.width = 10 + 25 + 10 + frame.size.width + 40;
}else{
frame.size.width = 10 + frame.size.width + 40;
}

frame.origin.x = self.showPoint.x - frame.size.width/2;
frame.origin.y = self.showPoint.y;

//左间隔最小5x
if (frame.origin.x < 5) {
frame.origin.x = 5;
}
double length;
if (window.bounds.size.width > 0) {
length = window.bounds.size.width - 5;
}

NSLog(@"%f",length);
//右间隔最小5x
if ((frame.origin.x + frame.size.width) > length) {
frame.origin.x = length - frame.size.width;
}


return frame;
}


-(void)show
{
self.handerView = [UIButton buttonWithType:UIButtonTypeCustom];
[_handerView setFrame:[UIScreen mainScreen].bounds];
[_handerView setBackgroundColor:[UIColor clearColor]];
[_handerView addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
[_handerView addSubview:self];
UIWindow *window = [UIApplication sharedApplication].keyWindow;

[window addSubview:_handerView];

CGPoint arrowPoint = [self convertPoint:self.showPoint fromView:_handerView];
self.layer.anchorPoint = CGPointMake(arrowPoint.x / self.frame.size.width, arrowPoint.y / self.frame.size.height);
self.frame = [self getViewFrame];

self.alpha = 0.f;
self.transform = CGAffineTransformMakeScale(0.1f, 0.1f);
[UIView animateWithDuration:0.2f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.transform = CGAffineTransformMakeScale(1.05f, 1.05f);
self.alpha = 1.f;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.08f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.transform = CGAffineTransformIdentity;
} completion:nil];
}];
}

-(void)dismiss
{
[self dismiss:YES];
}

-(void)dismiss:(BOOL)animate
{
if (!animate) {
[_handerView removeFromSuperview];
return;
}

[UIView animateWithDuration:0.3f animations:^{
self.transform = CGAffineTransformMakeScale(0.1f, 0.1f);
self.alpha = 0.f;
} completion:^(BOOL finished) {
[_handerView removeFromSuperview];
}];

}


#pragma mark - UITableView

-(UITableView *)tableView
{
if (_tableView != nil) {
return _tableView;
}

CGRect rect = self.frame;
rect.origin.x = SPACE;
rect.origin.y = kArrowHeight + SPACE;
rect.size.width -= SPACE * 2;
rect.size.height -= (SPACE - kArrowHeight);

self.tableView = [[UITableView alloc] initWithFrame:rect style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.alwaysBounceHorizontal = NO;
_tableView.alwaysBounceVertical = NO;
_tableView.showsHorizontalScrollIndicator = NO;
_tableView.showsVerticalScrollIndicator = NO;
_tableView.scrollEnabled = NO;
_tableView.backgroundColor = [UIColor clearColor];
// _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

return _tableView;
}

#pragma mark - UITableView DataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_titleArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.backgroundView = [[UIView alloc] init];
cell.backgroundView.backgroundColor = [UIColor blackColor];//RGB(245, 245, 245);

if ([_imageArray count] == [_titleArray count]) {
cell.imageView.image = [UIImage imageNamed:[_imageArray objectAtIndex:indexPath.row]];
}
cell.textLabel.font = [UIFont systemFontOfSize:16];
cell.textLabel.text = [_titleArray objectAtIndex:indexPath.row];
cell.textLabel.textColor = [UIColor whiteColor];

if ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0) {
cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
}

return cell;
}

#pragma mark - UITableView Delegate

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
if (self.selectRowAtIndex) {
self.selectRowAtIndex(indexPath.row);
}
[self dismiss:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ROW_HEIGHT;
}



// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
[self.borderColor set]; //设置线条颜色

CGRect frame = CGRectMake(0, 10, self.bounds.size.width, self.bounds.size.height - kArrowHeight);

float xMin = CGRectGetMinX(frame);
float yMin = CGRectGetMinY(frame);

float xMax = CGRectGetMaxX(frame);
float yMax = CGRectGetMaxY(frame);

CGPoint arrowPoint = [self convertPoint:self.showPoint fromView:_handerView];

UIBezierPath *popoverPath = [UIBezierPath bezierPath];
[popoverPath moveToPoint:CGPointMake(xMin, yMin)];//左上角

/********************向上的箭头**********************/
[popoverPath addLineToPoint:CGPointMake(arrowPoint.x - kArrowHeight, yMin)];//left side
[popoverPath addCurveToPoint:arrowPoint
controlPoint1:CGPointMake(arrowPoint.x - kArrowHeight + kArrowCurvature, yMin)
controlPoint2:arrowPoint];//actual arrow point

[popoverPath addCurveToPoint:CGPointMake(arrowPoint.x + kArrowHeight, yMin)
controlPoint1:arrowPoint
controlPoint2:CGPointMake(arrowPoint.x + kArrowHeight - kArrowCurvature, yMin)];//right side
/********************向上的箭头**********************/


[popoverPath addLineToPoint:CGPointMake(xMax, yMin)];//右上角

[popoverPath addLineToPoint:CGPointMake(xMax, yMax)];//右下角

[popoverPath addLineToPoint:CGPointMake(xMin, yMax)];//左下角

//填充颜色
[[UIColor blackColor] setFill];
[popoverPath fill];

[popoverPath closePath];
[popoverPath stroke];
}


@end

import “PopoverView.h” 就可以用了
调用:

1
2
3
4
5
6
7
8
9
10
- (IBAction)popOverView:(UIBarButtonItem *)sender {
CGPoint point = CGPointMake(sender.frame.origin.x + sender.frame.size.width/2, sender.frame.origin.y + sender.frame.size.height);
//CGPoint point = CGPointMake(self.view.bounds.size.width -30, self.view.frame.origin.y + 60);
NSArray *titles = @[@"item1", @"选项2", @"选项3"];
PopoverView *pop = [[PopoverView alloc] initWithPoint:point titles:titles images:nil];
pop.selectRowAtIndex = ^(NSInteger index){
NSLog(@"select index:%ld", (long)index);
};
[pop show];
}

可惜不是我写的,看了网上大神后自己改的……
那个神奇的keywindow是什么至今不知道