-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathPopMenu.m
executable file
·302 lines (250 loc) · 10.7 KB
/
PopMenu.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//
// MenuView.m
// JackFastKit
//
// Created by 曾 宪华 on 14-10-13.
// Copyright (c) 2014年 华捷 iOS软件开发工程师 曾宪华. All rights reserved.
//
#import "PopMenu.h"
#import "MenuButton.h"
#import <POP.h>
#define MenuButtonHeight 135
#define MenuButtonVerticalPadding 10
#define MenuButtonHorizontalMargin 10
#define MenuButtonAnimationTime 0.1
#define MenuButtonAnimationInterval (MenuButtonAnimationTime / 5)
#define kMenuButtonBaseTag 100
@interface PopMenu ()
@property (nonatomic, strong) XHRealTimeBlur *realTimeBlur;
@property (nonatomic, strong, readwrite) NSArray *items;
@property (nonatomic, strong) MenuItem *selectedItem;
@property (nonatomic, assign, readwrite) BOOL isShowed;
@property (nonatomic, assign) CGPoint startPoint;
@property (nonatomic, assign) CGPoint endPoint;
@property (nonatomic, strong) UIView *footerView;
@end
@implementation PopMenu
#pragma mark - Life Cycle
- (id)initWithFrame:(CGRect)frame items:(NSArray *)items {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.items = items;
[self setup];
}
return self;
}
// 设置属性
- (void)setup {
self.backgroundColor = [UIColor clearColor];
self.perRowItemCount = 3;
typeof(self) __weak weakSelf = self;
_realTimeBlur = [[XHRealTimeBlur alloc] initWithFrame:self.bounds];
_realTimeBlur.blurStyle = XHBlurStyleTranslucentWhite;
_realTimeBlur.showDuration = 0.3;
_realTimeBlur.disMissDuration = 0.1;
_realTimeBlur.willShowBlurViewcomplted = ^(void) {
weakSelf.isShowed = YES;
[weakSelf showButtons];
};
_realTimeBlur.willDismissBlurViewCompleted = ^(void) {
// UIView *presentView=[[[UIApplication sharedApplication].keyWindow rootViewController] view];
// if ([[presentView.subviews firstObject] isMemberOfClass:NSClassFromString(@"RDVTabBar")]) {
// [presentView bringSubviewToFront:[presentView.subviews firstObject]];
// }
[weakSelf.realTimeBlurFooter disMiss];
if (weakSelf.selectedItem) {
if (weakSelf.didSelectedItemCompletion) {
weakSelf.didSelectedItemCompletion(weakSelf.selectedItem);
weakSelf.selectedItem = nil;
}
}else
{
weakSelf.didSelectedItemCompletion(nil);
}
[weakSelf hidenButtons];
};
_realTimeBlur.didDismissBlurViewCompleted = ^(BOOL finished) {
weakSelf.isShowed = NO;
[weakSelf removeFromSuperview];
[weakSelf.footerView removeFromSuperview];
};
_realTimeBlur.hasTapGestureEnable = YES;
_realTimeBlurFooter = [[XHRealTimeBlur alloc] initWithFrame:self.bounds];
_realTimeBlurFooter.blurStyle = XHBlurStyleTranslucentWhite;
_realTimeBlurFooter.showDuration = 0.3;
_realTimeBlurFooter.disMissDuration = 0.1;
_footerView=[[UIView alloc] initWithFrame:CGRectMake(0, kScreen_Height-48, kScreen_Width, 48)];
}
#pragma mark - 公开方法
- (void)showMenuAtView:(UIView *)containerView {
CGPoint startPoint = CGPointMake(0, CGRectGetHeight(self.bounds));
CGPoint endPoint = startPoint;
switch (self.menuAnimationType) {
case kPopMenuAnimationTypeNetEase:
startPoint.x = CGRectGetMidX(self.bounds);
endPoint.x = startPoint.x;
break;
default:
break;
}
[self showMenuAtView:containerView startPoint:startPoint endPoint:endPoint];
}
- (void)showMenuAtView:(UIView *)containerView startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint {
if (self.isShowed) {
return;
}
self.startPoint = startPoint;
self.endPoint = endPoint;
self.clipsToBounds=TRUE;
[containerView addSubview:self];
[self.realTimeBlur showBlurViewAtView:self];
}
- (void)showMenuAtView:(UIView *)containerView startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint withTabFooterView:(UIView *)containerFooter {
if (self.isShowed) {
return;
}
self.startPoint = startPoint;
self.endPoint = endPoint;
[containerView addSubview:self];
[self.realTimeBlur showBlurViewAtView:self];
// _realTimeBlurFooter.frame=CGRectMake(0, 0, footerView.size.width, footerView.size.height);
[self.realTimeBlurFooter showBlurViewAtView:_footerView];
[containerFooter addSubview:_footerView];
}
- (void)dismissMenu {
if (!self.isShowed) {
return;
}
[self.realTimeBlur disMiss];
// [_realTimeBlurFooter disMiss];
}
#pragma mark - 私有方法
/**
* 添加菜单按钮
*/
- (void)showButtons {
NSArray *items = [self menuItems];
NSInteger perRowItemCount = self.perRowItemCount;
CGFloat menuButtonWidth = (CGRectGetWidth(self.bounds) - ((perRowItemCount + 1) * MenuButtonHorizontalMargin)) / perRowItemCount;
typeof(self) __weak weakSelf = self;
for (int index = 0; index < items.count; index ++) {
MenuItem *menuItem = items[index];
// 如果没有自定义index,就按照正常流程,从0开始
if (menuItem.index < 0) {
menuItem.index = index;
}
MenuButton *menuButton = (MenuButton *)[self viewWithTag:kMenuButtonBaseTag + index];
CGRect toRect = [self getFrameWithItemCount:items.count
perRowItemCount:perRowItemCount
perColumItemCount:items.count/perRowItemCount+(items.count%perRowItemCount>0?1:0)
itemWidth:menuButtonWidth
itemHeight:MenuButtonHeight
paddingX:MenuButtonVerticalPadding
paddingY:MenuButtonHorizontalMargin
atIndex:index
onPage:0];
CGRect fromRect = toRect;
switch (self.menuAnimationType) {
case kPopMenuAnimationTypeSina:
fromRect.origin.y = self.startPoint.y;
break;
case kPopMenuAnimationTypeNetEase:
fromRect.origin.x = self.startPoint.x - menuButtonWidth / 2.0;
fromRect.origin.y = self.startPoint.y;
break;
default:
break;
}
if (!menuButton) {
menuButton = [[MenuButton alloc] initWithFrame:fromRect menuItem:menuItem];
menuButton.tag = kMenuButtonBaseTag + index;
menuButton.didSelctedItemCompleted = ^(MenuItem *menuItem) {
weakSelf.selectedItem = menuItem;
[weakSelf dismissMenu];
};
[self addSubview:menuButton];
} else {
menuButton.frame = fromRect;
}
double delayInSeconds = (items.count - index) * MenuButtonAnimationInterval;
// NSLog(@"showButtons delayInSeconds:%d - %.3f", index, delayInSeconds);
[self initailzerAnimationWithToPostion:toRect formPostion:fromRect atView:menuButton beginTime:delayInSeconds];
}
}
/**
* 隐藏按钮
*/
- (void)hidenButtons {
NSArray *items = [self menuItems];
for (int index = 0; index < items.count; index ++) {
MenuButton *menuButton = (MenuButton *)[self viewWithTag:kMenuButtonBaseTag + index];
CGRect fromRect = menuButton.frame;
CGRect toRect = fromRect;
switch (self.menuAnimationType) {
case kPopMenuAnimationTypeSina:
toRect.origin.y = self.endPoint.y ;
break;
case kPopMenuAnimationTypeNetEase:
toRect.origin.x = self.endPoint.x - CGRectGetMidX(menuButton.bounds);
toRect.origin.y = self.endPoint.y;
break;
default:
break;
}
double delayInSeconds = index * MenuButtonAnimationInterval;
// NSLog(@"hidenButtons delayInSeconds:%d - %.3f", index, delayInSeconds);
[self initailzerAnimationWithToPostion:toRect formPostion:fromRect atView:menuButton beginTime:delayInSeconds];
}
}
- (NSArray *)menuItems {
return self.items;
}
/**
* 通过目标的参数,获取一个grid布局 网格布局
*
* @param perRowItemCount 每行有多少列
* @param perColumItemCount 每列有多少行
* @param itemWidth gridItem的宽度
* @param itemHeight gridItem的高度
* @param paddingX gridItem之间的X轴间隔
* @param paddingY gridItem之间的Y轴间隔
* @param index 某个gridItem所在的index序号
* @param page 某个gridItem所在的页码
*
* @return 返回一个已经处理好的gridItem frame
*/
- (CGRect)getFrameWithItemCount:(NSInteger)itemCount
perRowItemCount:(NSInteger)perRowItemCount
perColumItemCount:(NSInteger)perColumItemCount
itemWidth:(CGFloat)itemWidth
itemHeight:(NSInteger)itemHeight
paddingX:(CGFloat)paddingX
paddingY:(CGFloat)paddingY
atIndex:(NSInteger)index
onPage:(NSInteger)page {
CGFloat insetY = kScreen_Height * 0.12;
// NSUInteger rowCount = itemCount / perRowItemCount + (itemCount % perColumItemCount > 0 ? 1 : 0);
// CGFloat insetY = (CGRectGetHeight(self.bounds) - (itemHeight + paddingY) * rowCount) / 2.0;
CGFloat originX = (index % perRowItemCount) * (itemWidth + paddingX) + paddingX + (page * CGRectGetWidth(self.bounds));
CGFloat originY = ((index / perRowItemCount) - perColumItemCount * page) * (itemHeight + paddingY) + paddingY;
CGRect itemFrame = CGRectMake(originX, originY + insetY, itemWidth, itemHeight);
return itemFrame;
}
#pragma mark - Animation
- (void)initailzerAnimationWithToPostion:(CGRect)toRect formPostion:(CGRect)fromRect atView:(UIView *)view beginTime:(CFTimeInterval)beginTime {
POPSpringAnimation *springAnimation = [POPSpringAnimation animation];
springAnimation.property = [POPAnimatableProperty propertyWithName:kPOPViewFrame];
springAnimation.removedOnCompletion = YES;
springAnimation.beginTime = beginTime + CACurrentMediaTime();
// CGFloat springBounciness = 6 - beginTime * 2;
CGFloat springBounciness = 6;
springAnimation.springBounciness = springBounciness; // value between 0-20
// CGFloat springSpeed = 6 - beginTime * 2;
CGFloat springSpeed = 2;
springAnimation.springSpeed = springSpeed; // value between 0-20
springAnimation.toValue = [NSValue valueWithCGRect:toRect];
springAnimation.fromValue = [NSValue valueWithCGRect:fromRect];
[view pop_addAnimation:springAnimation forKey:@"POPSpringAnimationKey"];
}
@end