admin
2023-04-21 0b3a4aaf99ea251bc8e27b96115288f0988fcffe
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
//
//  MuneBar.m
//  WKMuneController
//
//  Created by macairwkcao on 16/1/26.
//  Copyright © 2016年 CWK. All rights reserved.
//
 
#import "CMuneBar.h"
#import "CMuneItem.h"
#define kRotationAngle M_PI / 20
 
 
@interface CMuneBar()
 
@property(nonatomic,strong)NSArray *items;
 
 
 
@end
 
 
@implementation CMuneBar
 
-(instancetype)initWithItems:(NSArray *)itemsImages size:(CGSize)size type:(MuneBarType)type{
    self = [super init];
    if (self) {
        self.itemsImages = itemsImages;
        self.isShow = NO;
        self.type = type;
        self.frame = CGRectMake(0, 0, size.width, size.height);
        self.layer.cornerRadius = size.width / 2.0;
//        self.layer.masksToBounds = YES;
        self.backgroundColor = [UIColor clearColor];
        [self addSubviews];
    }
    return self;
}
 
-(void)setImage:(UIImage *)image{
    [self.mainButton setImage:image forState:UIControlStateNormal];
}
 
-(void)setHighlightedImage:(UIImage *)highlightedImage{
    [self.mainButton setImage:highlightedImage forState:UIControlStateHighlighted];
}
 
-(void)setTitle:(NSString *)title color:(UIColor *)color{
    [self.mainButton setTitle:title forState:UIControlStateNormal];
    [self.mainButton setTitleColor:color forState:UIControlStateNormal];
}
 
 
#pragma mark - 重写父类方法
/**
 *  重写hitTest:withEvent:方法,检查是否点击item
 */
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *result = [super hitTest:point withEvent:event];
    if (self.isShow) {
        for (CMuneItem *item in self.items) {
            CGPoint buttonPoint = [item convertPoint:point fromView:self];
            if ([item pointInside:buttonPoint withEvent:event]) {
                return item;
            }
        }
    }
    return result;
}
 
-(void)setType:(MuneBarType)type{
    _type = type;
}
 
#pragma mark 私有方法
 
/**
 *  添加子视图
 */
-(void)addSubviews{
    _mainButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_itemsImages[0]]]];
 
//    UIImageView *imageview = [[UIImageView alloc]initWithFrame:_mainButton.frame];
//    [imageview setYthImageWithURL:_itemsImages[0] placeholderImage:[UIImage imageNamed:@"默认片源"]];
    [_mainButton setImage:image forState:UIControlStateNormal];
    
    [_mainButton addTarget:self action:@selector(showItems) forControlEvents:UIControlEventTouchUpInside];
    self.mainButton = _mainButton;
    self.mainButton.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    _mainButton.layer.cornerRadius = self.frame.size.width / 2.0;
    _mainButton.layer.masksToBounds = YES;
 
    /**
     *  菜单选项
     */
    [self items];
  
    [self addSubview:_mainButton];
    
}
 
/**
 *  懒加载菜单选项
 */
-(NSArray *)items{
    if (_items == nil) {
        NSMutableArray *items = [NSMutableArray arrayWithCapacity:self.itemsImages.count];
        for (int i = 0; i < self.itemsImages.count; i++) {
//            UIImageView *imageview = [[UIImageView alloc]initWithFrame:_mainButton.frame];
//            [imageview setYthImageWithURL:_itemsImages[i] placeholderImage:[UIImage imageNamed:@"默认片源"]];
            UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_itemsImages[i]]]];
 
            CMuneItem *item = [CMuneItem muneItemWithSize:CGSizeMake(self.frame.size.width, self.frame.size.height) image:image heightImage:nil target:self action:@selector(tapItem:)];
            item.tag = 100 + i;
            item.center = self.mainButton.center;
//            [self addSubview:imageview];
 
            [self addSubview:item];
            [items addObject:item];
        }
        [self addSubview:_mainButton];
        _items = items;
    }
    return _items;
}
 
/**
 *  展开item,以MuneBarTypeRound方式展开
 *
 *  @param offsetAngle  根据展开的方向不同,设置不同的偏移角度
 */
 
-(void)itemShowRoundTypeWithOffsetAngle:(CGFloat)offsetAngle{
    CGFloat count = self.items.count;
    for (CMuneItem *item in self.items) {
        CGFloat angle = [self caculateRoundAngleWithOffsetAngle:offsetAngle index:count];
        [item itemShowWithType:kMuneItemShowTypeRound angle:angle];
        count -- ;
    }
}
 
-(void)itemShowLineWithOffsetPoint:(CGPoint)point incremenr:(CGSize)increment{
    CGFloat count = self.items.count;
 
    for (CMuneItem *item in self.items) {
        CGPoint targetPoint = [self caculateLinePointWithOffsetPoint:point increment:increment index:count];
        [item itemShowWithTargetPoint:targetPoint type:kMuneItemShowTypeLine];
        count -- ;
    }
}
 
/**
 *  展开角度计算,以MuneBarTypeRound方式展开
 *
 *  @param offsetAngle  根据展开的方向不同,设置不同的偏移角度
 *  @param index
 *  @return return angle 每个item偏移的角度
 */
 
-(CGFloat)caculateRoundAngleWithOffsetAngle:(CGFloat)offsetAngle index:(CGFloat)index{
    CGFloat angle = M_PI / (self.items.count);
    angle = angle * index - angle / 2.0 + offsetAngle;
    return angle;
}
 
-(CGPoint)caculateLinePointWithOffsetPoint:(CGPoint)offsetPoint increment:(CGSize)increment index:(NSInteger)index{
    CGFloat x = offsetPoint.x;
    CGFloat y = offsetPoint.y;
    x += increment.width * index;
    y += increment.height * index;
    CGPoint point = CGPointMake(x, y);
    return point;
}
 
 
#pragma mark - 成员方法
/**
 *  显示菜单
 */
-(void)showItems{
    if (!self.isShow) {
        CGFloat count = self.items.count;
        self.isShow = YES;
        if ([self.delegate respondsToSelector:@selector(muneBarHide)]) {
            [self.delegate muneBarShow];
        }
        switch (self.type) {
            case kMuneBarTypeRadRight:
                for (CMuneItem *item in self.items) {
                    [item itemShowWithType:kMuneItemShowTypeRadRight angle:kRotationAngle * count];
                    count --;
                }
                break;
            case kMuneBarTypeRadLeft:
                for (CMuneItem *item in self.items) {
                    [item itemShowWithType:kMuneItemShowTypeRadLeft angle:kRotationAngle * count];
                    count --;
                }
                break;
            case kMuneBarTypeLineTop:{
                [self itemShowLineWithOffsetPoint:CGPointMake(self.frame.size.width / 2.0, self.frame.size.height / 2.0) incremenr:CGSizeMake(0, -self.frame.size.height - 10)];
            }
                break;
            case kMuneBarTypeLineBottom:{
                [self itemShowLineWithOffsetPoint:CGPointMake(self.frame.size.width / 2.0, self.frame.size.height / 2.0) incremenr:CGSizeMake(0, self.frame.size.height + 10)];
 
            }
                break;
            case kMuneBarTypeLineLeft:{
                [self itemShowLineWithOffsetPoint:CGPointMake(self.frame.size.width / 2.0, self.frame.size.height / 2.0) incremenr:CGSizeMake(- self.frame.size.height - 10, 0)];
            }
                break;
            case kMuneBarTypeLineRight:{
                [self itemShowLineWithOffsetPoint:CGPointMake(self.frame.size.width / 2.0, self.frame.size.height / 2.0) incremenr:CGSizeMake(self.frame.size.height + 10, 0)];
            }
                break;
            case kMuneBarTypeRoundTop:{
                [self itemShowRoundTypeWithOffsetAngle:0];
            }
                break;
                
            case kMuneBarTypeRoundBottom:{
                [self itemShowRoundTypeWithOffsetAngle:- M_PI];
            }
                break;
            case kMuneBarTypeRoundLeft:{
                [self itemShowRoundTypeWithOffsetAngle:M_PI / 2.0];
 
            }
                break;
            case kMuneBarTypeRoundRight:{
                [self itemShowRoundTypeWithOffsetAngle: - M_PI / 2.0];
 
            }
                break;
            default:
                break;
        }
    }else{
        [self hideItems];
    }
}
 
 
 
 
/**
 *  隐藏菜单
 */
-(void)hideItems{
    for (CMuneItem *item in self.items) {
        [item itemHide];
    }
    if ([self.delegate respondsToSelector:@selector(muneBarHide)]) {
        [self.delegate muneBarShow];
    }
    self.isShow = NO;
}
 
#pragma 点击事件
/**
 * 点击item响应
 */
-(void)tapItem:(CMuneItem *)item{
    if ([self.delegate respondsToSelector:@selector(muneBarselected:)]) {
        [self.delegate muneBarselected:item.tag - 100];
    }
}
 
@end