-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathQBPopupMenu.m
More file actions
executable file
·623 lines (477 loc) · 25.4 KB
/
Copy pathQBPopupMenu.m
File metadata and controls
executable file
·623 lines (477 loc) · 25.4 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*
Copyright (c) 2013 Katsuma Tanaka
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#import "QBPopupMenu.h"
#import "QBPopupMenuOverlayView.h"
@interface QBPopupMenu ()
@property (nonatomic, strong) QBPopupMenuOverlayView *overlayView;
@property (nonatomic, strong) UIImage *popupImage;
@property (nonatomic, strong) UIImage *highlightedPopupImage;
- (void)performAction:(id)sender;
- (CGSize)actualSize;
- (UIImage *)croppedImageFromImage:(UIImage *)image rect:(CGRect)rect;
- (UIImage *)popupImageForState:(QBPopupMenuState)state;
- (void)drawRightSeparatorInContext:(CGContextRef)context startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint state:(QBPopupMenuState)state;
- (void)drawLeftSeparatorInContext:(CGContextRef)context startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint state:(QBPopupMenuState)state;
@end
@implementation QBPopupMenu
- (instancetype)init
{
return [self initWithItems:nil];
}
- (instancetype)initWithFrame:(CGRect)frame
{
return [self initWithItems:nil];
}
- (instancetype)initWithItems:(NSArray *)items
{
self = [super initWithFrame:CGRectZero];
if (self) {
// ビューの設定
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];
// 初期設定
self.items = items;
self.cornerRadius = 8;
self.arrowSize = 12;
self.animationEnabled = YES;
self.popupImage = nil;
self.highlightedPopupImage = nil;
}
return self;
}
- (void)setItems:(NSArray *)items
{
_items = [items copy];
if (items) {
// サイズを計算
CGSize actualSize = [self actualSize];
actualSize.height = actualSize.height + self.arrowSize;
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, actualSize.width, actualSize.height);
// 画像を作成
self.popupImage = [self popupImageForState:QBPopupMenuStateNormal];
self.highlightedPopupImage = [self popupImageForState:QBPopupMenuStateHighlighted];
// サブビューを削除
for (UIView *subview in self.subviews) {
[subview removeFromSuperview];
}
// ボタンを配置
CGSize frameSize = CGSizeMake(self.bounds.size.width, self.bounds.size.height - self.arrowSize);
CGFloat middle = round(frameSize.height / 2);
CGFloat itemOffset = 0;
for (NSUInteger i = 0; i < self.items.count; i++) {
QBPopupMenuItem *item = [self.items objectAtIndex:i];
CGSize itemSize = [item actualSize];
CGRect itemFrame = CGRectMake(itemOffset, 0, itemSize.width, actualSize.height);
// 画像を切り出す
UIImage *image = [self croppedImageFromImage:self.popupImage rect:itemFrame];
UIImage *highlightedImage = [self croppedImageFromImage:self.highlightedPopupImage rect:itemFrame];
// ボタンを設置
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag = i;
button.frame = itemFrame;
button.autoresizingMask = UIViewAutoresizingNone;
button.enabled = item.enabled;
[button setImage:image forState:UIControlStateNormal];
[button setImage:highlightedImage forState:UIControlStateHighlighted];
[button setImage:image forState:UIControlStateDisabled];
[button addTarget:self action:@selector(performAction:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
// ボタン上のアイテムを設置
if (item.customView) {
item.customView.frame = CGRectMake(itemOffset, 0, itemSize.width, frameSize.height);
[self addSubview:item.customView];
} else {
if (item.title && item.image) {
// Image
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(itemOffset, 4, itemSize.width, middle - 4)];
imageView.image = item.image;
imageView.clipsToBounds = YES;
imageView.contentMode = UIViewContentModeCenter;
imageView.autoresizingMask = UIViewAutoresizingNone;
[self addSubview:imageView];
// Title
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(itemOffset, middle, itemSize.width, middle)];
titleLabel.text = item.title;
titleLabel.font = [item actualFont];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.textColor = [UIColor whiteColor];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.autoresizingMask = UIViewAutoresizingNone;
[self addSubview:titleLabel];
} else if (item.title) {
// Title
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(itemOffset, 0, itemSize.width, frameSize.height)];
titleLabel.text = item.title;
titleLabel.font = [item actualFont];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.textColor = [UIColor whiteColor];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.autoresizingMask = UIViewAutoresizingNone;
[self addSubview:titleLabel];
} else if (item.image) {
// Image
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(itemOffset, 4, itemSize.width, frameSize.height - 4)];
imageView.image = item.image;
imageView.clipsToBounds = YES;
imageView.contentMode = UIViewContentModeCenter;
imageView.autoresizingMask = UIViewAutoresizingNone;
[self addSubview:imageView];
}
}
itemOffset = itemOffset + itemSize.width;
}
}
}
#pragma mark -
- (void)showInView:(UIView *)view atPoint:(CGPoint)point;
{
// デリゲート
if ([self.delegate respondsToSelector:@selector(popupMenuWillAppear:)]) {
[self.delegate popupMenuWillAppear:self];
}
// 位置を算出
CGRect frame = self.frame;
frame.origin.x = round(point.x - frame.size.width / 2);
frame.origin.y = round(point.y - frame.size.height);
self.frame = frame;
// 背景用ビューをつくる
QBPopupMenuOverlayView *overlayView = [[QBPopupMenuOverlayView alloc] initWithFrame:view.bounds];
overlayView.popupMenu = self;
self.overlayView = overlayView;
// 影を点ける
self.layer.shadowOpacity = 0.5;
self.layer.shadowOffset = CGSizeMake(0, 1.6);
self.layer.shadowRadius = 1.5;
// アニメーションの準備
if (self.animationEnabled) {
self.layer.opacity = 0;
}
// ポップアップを表示
[self.overlayView addSubview:self];
[view addSubview:self.overlayView];
if (self.animationEnabled) {
[UIView animateWithDuration:0.2 animations:^(void) {
self.layer.opacity = 1.0;
} completion:^(BOOL finished) {
// デリゲート
if ([self.delegate respondsToSelector:@selector(popupMenuDidAppear:)]) {
[self.delegate popupMenuDidAppear:self];
}
}];
} else {
// デリゲート
if ([self.delegate respondsToSelector:@selector(popupMenuDidAppear:)]) {
[self.delegate popupMenuDidAppear:self];
}
}
}
- (void)dismiss
{
// デリゲート
if ([self.delegate respondsToSelector:@selector(popupMenuWillDisappear:)]) {
[self.delegate popupMenuWillDisappear:self];
}
// ポップアップを消す
if (self.animationEnabled) {
// アニメーションあり
[UIView animateWithDuration:0.2 animations:^(void) {
self.layer.opacity = 0;
} completion:^(BOOL finished) {
[self removeFromSuperview];
[self.overlayView removeFromSuperview];
// デリゲートメソッド
if ([self.delegate respondsToSelector:@selector(popupMenuDidDisappear:)]) {
[self.delegate popupMenuDidDisappear:self];
}
}];
} else {
// アニメーションなし
[self removeFromSuperview];
[self.overlayView removeFromSuperview];
// デリゲートメソッド
if ([self.delegate respondsToSelector:@selector(popupMenuDidDisappear:)]) {
[self.delegate popupMenuDidDisappear:self];
}
}
}
- (void)performAction:(id)sender
{
UIButton *button = (UIButton *)sender;
QBPopupMenuItem *item = [self.items objectAtIndex:button.tag];
[item performAction];
[self dismiss];
}
- (CGSize)actualSize
{
CGFloat width = 0, height = 0;
for (NSUInteger i = 0; i < self.items.count; i++) {
QBPopupMenuItem *item = [self.items objectAtIndex:i];
CGSize actualItemSize = [item actualSize];
width = width + actualItemSize.width;
if (actualItemSize.height > height) {
height = actualItemSize.height;
}
}
return CGSizeMake(width, height);
}
- (UIImage *)croppedImageFromImage:(UIImage *)image rect:(CGRect)rect
{
CGFloat scale = [[UIScreen mainScreen] scale];
CGRect scaledRect = CGRectMake(rect.origin.x * scale, rect.origin.y * scale, rect.size.width * scale, rect.size.height * 2);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], scaledRect);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef scale:scale orientation:UIImageOrientationUp];
CGImageRelease(imageRef);
return croppedImage;
}
- (UIImage *)popupImageForState:(QBPopupMenuState)state
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat cornerRadius = self.cornerRadius;
CGFloat arrowSize = self.arrowSize;
CGSize frameSize = CGSizeMake(self.bounds.size.width, self.bounds.size.height - arrowSize);
CGPoint point = CGPointMake(round(self.bounds.size.width / 2 - 1), self.bounds.size.height - 1);
CGFloat middle = round(frameSize.height / 2);
CGFloat inset = 1;
CGFloat cornerInset = 1;
// ベース
CGMutablePathRef basePath = CGPathCreateMutable();
CGPathMoveToPoint(basePath, NULL, 0, cornerRadius);
CGPathAddArcToPoint(basePath, NULL, 0, 0, cornerRadius, 0, cornerRadius);
CGPathAddLineToPoint(basePath, NULL, frameSize.width - cornerRadius, 0);
CGPathAddArcToPoint(basePath, NULL, frameSize.width, 0, frameSize.width, cornerRadius, cornerRadius);
CGPathAddLineToPoint(basePath, NULL, frameSize.width, frameSize.height - cornerRadius);
CGPathAddArcToPoint(basePath, NULL, frameSize.width, frameSize.height, frameSize.width - cornerRadius, frameSize.height, cornerRadius);
CGPathAddLineToPoint(basePath, NULL, point.x + arrowSize, frameSize.height);
CGPathAddLineToPoint(basePath, NULL, point.x, point.y);
CGPathAddLineToPoint(basePath, NULL, point.x - arrowSize, frameSize.height);
CGPathAddLineToPoint(basePath, NULL, cornerRadius, frameSize.height);
CGPathAddArcToPoint(basePath, NULL, 0, frameSize.height, 0, frameSize.height - cornerRadius, cornerRadius);
CGPathCloseSubpath(basePath);
CGContextAddPath(context, basePath);
CGContextSetRGBFillColor(context, 0, 0, 0, 1.0);
CGContextFillPath(context);
CGPathRelease(basePath);
// 矢印
if (state == QBPopupMenuStateHighlighted) {
CGMutablePathRef arrowPath = CGPathCreateMutable();
CGPathMoveToPoint(arrowPath, NULL, point.x + arrowSize, frameSize.height - inset);
CGPathAddLineToPoint(arrowPath, NULL, point.x, point.y - inset);
CGPathAddLineToPoint(arrowPath, NULL, point.x - arrowSize, frameSize.height - inset);
CGPathCloseSubpath(arrowPath);
// 矢印のグラデーション
CGContextSaveGState(context);
CGContextAddPath(context, arrowPath);
CGContextClip(context);
CGColorSpaceRef arrowColorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGFloat arrowComponents[8] = {
0.027, 0.169, 0.733, 1,
0.02, 0.114, 0.675, 1
};
size_t arrowCount = sizeof(arrowComponents) / (sizeof(CGFloat) * 4);
CGGradientRef arrowGradientRef = CGGradientCreateWithColorComponents(arrowColorSpaceRef, arrowComponents, NULL, arrowCount);
CGPoint arrowStartPoint = CGPointMake(0, frameSize.height - inset);
CGPoint arrowEndPoint = CGPointMake(0, point.y - inset);
CGContextDrawLinearGradient(context, arrowGradientRef, arrowStartPoint, arrowEndPoint, kCGGradientDrawsAfterEndLocation);
CGGradientRelease(arrowGradientRef);
CGColorSpaceRelease(arrowColorSpaceRef);
CGContextRestoreGState(context);
CGPathRelease(arrowPath);
}
// ベース2(上半分)
CGMutablePathRef basePath2 = CGPathCreateMutable();
CGPathMoveToPoint(basePath2, NULL, inset, cornerRadius);
CGPathAddArcToPoint(basePath2, NULL, inset, inset, cornerRadius, inset, cornerRadius - cornerInset);
CGPathAddLineToPoint(basePath2, NULL, frameSize.width - cornerRadius, inset);
CGPathAddArcToPoint(basePath2, NULL, frameSize.width - inset, inset, frameSize.width - inset, cornerRadius, cornerRadius - cornerInset);
CGPathAddLineToPoint(basePath2, NULL, frameSize.width - inset, middle);
CGPathAddLineToPoint(basePath2, NULL, inset, middle);
CGPathCloseSubpath(basePath2);
CGContextAddPath(context, basePath2);
switch(state) {
case QBPopupMenuStateNormal:
CGContextSetRGBFillColor(context, 0.471, 0.471, 0.471, 1.0);
break;
case QBPopupMenuStateHighlighted:
CGContextSetRGBFillColor(context, 0.384, 0.608, 0.906, 1.0);
break;
}
CGContextFillPath(context);
CGPathRelease(basePath2);
// ベース3(上半分の上)
CGMutablePathRef basePath3 = CGPathCreateMutable();
CGPathMoveToPoint(basePath3, NULL, inset, cornerRadius);
CGPathAddArcToPoint(basePath3, NULL, inset, inset + 1, cornerRadius, inset + 1, cornerRadius - cornerInset);
CGPathAddLineToPoint(basePath3, NULL, frameSize.width - cornerRadius, inset + 1);
CGPathAddArcToPoint(basePath3, NULL, frameSize.width - inset, inset + 1, frameSize.width - inset, cornerRadius, cornerRadius - cornerInset);
CGPathAddLineToPoint(basePath3, NULL, frameSize.width - inset, middle);
CGPathAddLineToPoint(basePath3, NULL, inset, middle);
CGPathCloseSubpath(basePath3);
CGContextAddPath(context, basePath3);
switch(state) {
case QBPopupMenuStateNormal:
CGContextSetRGBFillColor(context, 0.314, 0.314, 0.314, 1.0);
break;
case QBPopupMenuStateHighlighted:
CGContextSetRGBFillColor(context, 0.216, 0.471, 0.871, 1.0);
break;
}
CGContextFillPath(context);
// ベース3のグラデーション
CGContextSaveGState(context);
CGContextAddPath(context, basePath3);
CGContextClip(context);
CGColorSpaceRef colorSpaceRef3 = CGColorSpaceCreateDeviceRGB();
CGFloat components3[8];
switch(state) {
case QBPopupMenuStateNormal:
components3[0] = 0.314; components3[1] = 0.314; components3[2] = 0.314; components3[3] = 1;
components3[4] = 0.165; components3[5] = 0.165; components3[6] = 0.165; components3[7] = 1;
break;
case QBPopupMenuStateHighlighted:
components3[0] = 0.216; components3[1] = 0.471; components3[2] = 0.871; components3[3] = 1;
components3[4] = 0.059; components3[5] = 0.353; components3[6] = 0.839; components3[7] = 1;
break;
}
size_t count3 = sizeof(components3) / (sizeof(CGFloat) * 4);
CGGradientRef gradientRef3 = CGGradientCreateWithColorComponents(colorSpaceRef3, components3, NULL, count3);
CGPoint startPoint3 = CGPointMake(0, inset);
CGPoint endPoint3 = CGPointMake(0, middle);
CGContextDrawLinearGradient(context, gradientRef3, startPoint3, endPoint3, kCGGradientDrawsAfterEndLocation);
CGGradientRelease(gradientRef3);
CGColorSpaceRelease(colorSpaceRef3);
CGContextRestoreGState(context);
CGPathRelease(basePath3);
// ベース4(下半分)
CGMutablePathRef basePath4 = CGPathCreateMutable();
CGPathMoveToPoint(basePath4, NULL, inset, middle);
CGPathAddLineToPoint(basePath4, NULL, frameSize.width - inset, middle);
CGPathAddLineToPoint(basePath4, NULL, frameSize.width - inset, frameSize.height - cornerRadius);
CGPathAddArcToPoint(basePath4, NULL, frameSize.width - inset, frameSize.height - inset, frameSize.width - cornerRadius, frameSize.height - inset, cornerRadius - cornerInset);
CGPathAddLineToPoint(basePath4, NULL, cornerRadius, frameSize.height - inset);
CGPathAddArcToPoint(basePath4, NULL, inset, frameSize.height - inset, inset, frameSize.height - cornerRadius, cornerRadius - cornerInset);
CGPathCloseSubpath(basePath4);
CGContextAddPath(context, basePath4);
switch(state) {
case QBPopupMenuStateNormal:
CGContextSetRGBFillColor(context, 0.102, 0.102, 0.102, 1.0);
break;
case QBPopupMenuStateHighlighted:
CGContextSetRGBFillColor(context, 0.047, 0.306, 0.827, 1.0);
break;
}
CGContextSetRGBFillColor(context, 0, 0, 0, 1.0);
CGContextFillPath(context);
// ベース4のグラデーション
CGContextSaveGState(context);
CGContextAddPath(context, basePath4);
CGContextClip(context);
CGColorSpaceRef colorSpaceRef4 = CGColorSpaceCreateDeviceRGB();
CGFloat components4[8];
switch(state) {
case QBPopupMenuStateNormal:
components4[0] = 0.102; components4[1] = 0.102; components4[2] = 0.102; components4[3] = 1;
components4[4] = 0; components4[5] = 0; components4[6] = 0; components4[7] = 1;
break;
case QBPopupMenuStateHighlighted:
components4[0] = 0.047; components4[1] = 0.306; components4[2] = 0.827; components4[3] = 1;
components4[4] = 0.027; components4[5] = 0.176; components4[6] = 0.737; components4[7] = 1;
break;
}
size_t count4 = sizeof(components4) / (sizeof(CGFloat) * 4);
CGGradientRef gradientRef4 = CGGradientCreateWithColorComponents(colorSpaceRef4, components4, NULL, count4);
CGPoint startPoint4 = CGPointMake(0, middle);
CGPoint endPoint4 = CGPointMake(0, frameSize.height - inset);
CGContextDrawLinearGradient(context, gradientRef4, startPoint4, endPoint4, kCGGradientDrawsAfterEndLocation);
CGGradientRelease(gradientRef4);
CGColorSpaceRelease(colorSpaceRef4);
CGContextRestoreGState(context);
CGPathRelease(basePath4);
// セパレータを描画
CGFloat separatorOffset = 0;
if (self.items.count > 1) {
for (NSUInteger i = 0; i < self.items.count; i++) {
QBPopupMenuItem *item = [self.items objectAtIndex:i];
CGSize actualSize = [item actualSize];
if (i == 0) {
// 右側に描く
separatorOffset = separatorOffset + actualSize.width;
[self drawRightSeparatorInContext:context startPoint:CGPointMake(separatorOffset - 1, inset * 2) endPoint:CGPointMake(separatorOffset - 1, frameSize.height - inset * 2) state:state];
} else if (i == self.items.count - 1) {
// 左側に描く
[self drawLeftSeparatorInContext:context startPoint:CGPointMake(separatorOffset, inset * 2) endPoint:CGPointMake(separatorOffset, frameSize.height - inset * 2) state:state];
separatorOffset = separatorOffset + actualSize.width;
} else {
// 両側に描く
[self drawLeftSeparatorInContext:context startPoint:CGPointMake(separatorOffset, inset * 2) endPoint:CGPointMake(separatorOffset, frameSize.height - inset * 2) state:state];
separatorOffset = separatorOffset + actualSize.width;
[self drawRightSeparatorInContext:context startPoint:CGPointMake(separatorOffset - 1, inset * 2) endPoint:CGPointMake(separatorOffset - 1, frameSize.height - inset * 2) state:state];
}
}
}
// 画像を取り出す
UIImage *popupImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return popupImage;
}
- (void)drawRightSeparatorInContext:(CGContextRef)context startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint state:(QBPopupMenuState)state
{
CGContextSaveGState(context);
CGContextAddRect(context, CGRectMake(startPoint.x, startPoint.y, 1, endPoint.y - startPoint.y));
CGContextClip(context);
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGFloat components[16];
switch(state) {
case QBPopupMenuStateNormal:
components[0] = 0.31; components[1] = 0.31; components[2] = 0.31; components[3] = 1;
components[4] = 0.06; components[5] = 0.06; components[6] = 0.06; components[7] = 1;
components[8] = 0.04; components[9] = 0.04; components[10] = 0.04; components[11] = 1;
components[12] = 0; components[13] = 0; components[14] = 0; components[15] = 1;
break;
case QBPopupMenuStateHighlighted:
components[0] = 0.22; components[1] = 0.47; components[2] = 0.87; components[3] = 1;
components[4] = 0.03; components[5] = 0.18; components[6] = 0.72; components[7] = 1;
components[8] = 0.02; components[9] = 0.15; components[10] = 0.73; components[11] = 1;
components[12] = 0.03; components[13] = 0.17; components[14] = 0.72; components[15] = 1;
break;
}
size_t count = sizeof(components) / (sizeof(CGFloat) * 4);
CGGradientRef gradientRef = CGGradientCreateWithColorComponents(colorSpaceRef, components, NULL, count);
CGContextDrawLinearGradient (context, gradientRef, startPoint, endPoint, 0);
CGContextRestoreGState(context);
CGGradientRelease(gradientRef);
CGColorSpaceRelease(colorSpaceRef);
}
- (void)drawLeftSeparatorInContext:(CGContextRef)context startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint state:(QBPopupMenuState)state
{
CGContextSaveGState(context);
CGContextAddRect(context, CGRectMake(startPoint.x, startPoint.y, 1, endPoint.y - startPoint.y));
CGContextClip(context);
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGFloat components[16];
switch(state) {
case QBPopupMenuStateNormal:
components[0] = 0.31; components[1] = 0.31; components[2] = 0.31; components[3] = 1;
components[4] = 0.31; components[5] = 0.31; components[6] = 0.31; components[7] = 1;
components[8] = 0.24; components[9] = 0.24; components[10] = 0.24; components[11] = 1;
components[12] = 0.05; components[13] = 0.05; components[14] = 0.05; components[15] = 1;
break;
case QBPopupMenuStateHighlighted:
components[0] = 0.22; components[1] = 0.47; components[2] = 0.87; components[3] = 1;
components[4] = 0.12; components[5] = 0.50; components[6] = 0.89; components[7] = 1;
components[8] = 0.09; components[9] = 0.47; components[10] = 0.88; components[11] = 1;
components[12] = 0.03; components[13] = 0.18; components[14] = 0.74; components[15] = 1;
break;
}
size_t count = sizeof(components) / (sizeof(CGFloat) * 4);
CGGradientRef gradientRef = CGGradientCreateWithColorComponents(colorSpaceRef, components, NULL, count);
CGContextDrawLinearGradient (context, gradientRef, startPoint, endPoint, 0);
CGContextRestoreGState(context);
CGGradientRelease(gradientRef);
CGColorSpaceRelease(colorSpaceRef);
}
@end