-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMenuAction.m
More file actions
90 lines (72 loc) · 3.24 KB
/
Copy pathMenuAction.m
File metadata and controls
90 lines (72 loc) · 3.24 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
//
// MenuAction.m
// LinkageMenu
//
// Created by 魏小庄 on 2018/5/18.
// Copyright © 2018年 魏小庄. All rights reserved.
//
#import "MenuAction.h"
@interface MenuAction ()
@property (nonatomic, assign, readwrite) MenuActionStyle actionStyle;
@property (nonatomic, copy, readwrite) NSString *title;
/** 私有属性用于自定义视图 主动移除筛选列表 */
@property (nonatomic, copy) void (^didDismissDropMenu)(void);
@end
@implementation MenuAction
+ (instancetype)actionWithTitle:(NSString *)title style:(MenuActionStyle)style {
MenuAction *action = [[MenuAction alloc]init];
action.actionStyle = style;
action.title = title;
action.adjustsImageWhenDisabled = NO;
action.adjustsImageWhenHighlighted = NO;
[action setTitle:title forState:UIControlStateNormal];
action.titleLabel.font = [UIFont systemFontOfSize:15];
action.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
[action setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[action setImage:[self drawTriangleWithFront:YES] forState:UIControlStateNormal];
[action setImage:[self drawTriangleWithFront:NO] forState:UIControlStateSelected];
return action;
}
+ (UIImage *)drawTriangleWithFront:(BOOL)front {
CGFloat w = 12;
CGFloat h = 8;
UIView *triangleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, w, h)];
CAShapeLayer *triangleLayer = [[CAShapeLayer alloc]init];
UIBezierPath *path = [UIBezierPath bezierPath];
if (front) {
[path moveToPoint:CGPointMake(0, 0)];
[path addLineToPoint:CGPointMake(w, 0)];
[path addLineToPoint:CGPointMake(w * 0.5, h)];
triangleLayer.path = path.CGPath;
[triangleView.layer addSublayer:triangleLayer];
[triangleLayer setFillColor:[UIColor blackColor].CGColor];
}else {
[path moveToPoint:CGPointMake(w * 0.5, 0)];
[path addLineToPoint:CGPointMake(0, h)];
[path addLineToPoint:CGPointMake(w, h)];
triangleLayer.path = path.CGPath;
[triangleView.layer addSublayer:triangleLayer];
[triangleLayer setFillColor:[UIColor colorWithRed:242/255.0 green:136/255.0 blue:0/255.0 alpha:1/1.0].CGColor];
}
triangleView.backgroundColor = [UIColor whiteColor];
UIGraphicsBeginImageContextWithOptions(triangleView.frame.size, YES, [UIScreen mainScreen].scale); //图形上下文设置
[triangleView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();//赋值
UIGraphicsEndImageContext();//结束
return image;
}
-(void)adjustFrame {
[self setTitleEdgeInsets:UIEdgeInsetsMake(0, -self.imageView.bounds.size.width + 2, 0, self.imageView.bounds.size.width + 10)];
[self setImageEdgeInsets:UIEdgeInsetsMake(0, self.titleLabel.bounds.size.width + 10, 0, -self.titleLabel.bounds.size.width + 2)];
}
- (void)adjustTitle:(NSString *)title textColor:(UIColor *)color {
if (![title isKindOfClass:[NSString class]]) return;
[self setTitle:title forState:UIControlStateNormal];
[self setTitleColor:color forState:UIControlStateNormal];
[self adjustFrame];
// 移除筛选列表
if(self.didDismissDropMenu){
self.didDismissDropMenu();
}
}
@end