效果图
代理传递消息
//
// CXYCircleMenuView.h
// MagicLamp
//
// Created by on 2017/10/20.
// Copyright © 2017年 YF Ding. All rights reserved.
//
#import <UIKit/UIKit.h>
@protocol CXYCircleMenuViewDeleagte<NSObject>
-(void)bleBtnClick;
-(void)phoneMusicBtnClick;
-(void)deviceMusicBtnClick;
-(void)lightBtnClick;
-(void)fmBtnClick;
-(void)outVoiceBtnClick;
-(void)voiceKardBtnClick;
@end
@interface CXYCircleMenuView : UIView
@property (nonatomic,assign)id<CXYCircleMenuViewDeleagte>delegate;
@end
实现
//
// CXYCircleMenuView.m
// MagicLamp
//
// Created by on 2017/10/20.
// Copyright © 2017年 YF Ding. All rights reserved.
//
#import "CXYCircleMenuView.h"
#define DIST(pointA,pointB) sqrtf((pointA.x-pointB.x)*(pointA.x-pointB.x)+(pointA.y-pointB.y)*(pointA.y-pointB.y))
@interface CXYCircleMenuView()
@property (nonatomic, strong) UIView *content;
@property (nonatomic, assign) CGPoint startPoint;
@property (nonatomic, strong) NSMutableArray <UIView *>*views;
@end
@implementation CXYCircleMenuView
-(NSMutableArray *)views{
if (!_views) {
_views = [[NSMutableArray alloc] initWithCapacity:0];
}
return _views;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setUpView];
}
return self;
}
-(void)setUpView{
NSArray <NSString *> *imageNameArray = @[
@"music",@"itunes-blue",@"light_bulb",@"radio",
@"volume",@"memorycard",];
NSArray <NSString *> *titleArraay = @[@"手机音乐",@"设备音乐",@"灯光模式",@"收音模式",@"外部声源",@"声卡模式"];
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height/2-self.width/2, self.width, self.width)];
self.content = contentView;
[self addSubview:contentView];
UIButton *centerImageView =[UIButton buttonWithType:UIButtonTypeCustom];
centerImageView.frame = CGRectMake(self.centerX- KSCREEN_WIDTH/6, self.centerY-KSCREEN_WIDTH/6, KSCREEN_WIDTH/3, KSCREEN_WIDTH/3);
centerImageView.backgroundColor = [UIColor clearColor];
[centerImageView setImage:[UIImage imageNamed:@"bluetooth"] forState:UIControlStateNormal];
[centerImageView addTarget:self action:@selector(tabberBtnClick:) forControlEvents:UIControlEventTouchUpInside];
centerImageView.tag = 7;
[self addSubview:centerImageView];
[self setCircleMenuViewWithIconArray:imageNameArray WithTitle:titleArraay];
}
-(void)setCircleMenuViewWithIconArray:(NSArray *)icons WithTitle:(NSArray *)titles{
for (int i = 0; i<icons.count; i++) {
CGFloat x = KSCREEN_WIDTH/2*sin(2*M_PI/icons.count*i);
CGFloat y = KSCREEN_WIDTH/2*cos(2*M_PI/icons.count*i);
UIView *menuView = [[UIView alloc] initWithFrame:CGRectMake(KSCREEN_WIDTH/2 + 3*x/4 - KSCREEN_WIDTH/8, KSCREEN_WIDTH/2 + 3*y/4 - KSCREEN_WIDTH/8, KSCREEN_WIDTH/4, KSCREEN_WIDTH/4)];
menuView.backgroundColor = [UIColor clearColor];
[self.views addObject:menuView];
UIButton *tabbarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[tabbarBtn setImage:[UIImage imageNamed:icons[i]] forState:UIControlStateNormal];
tabbarBtn.frame = CGRectMake(menuView.width/8, 0, 3*menuView.width/4, 3*menuView.width/4);
tabbarBtn.tag = i+1;
[tabbarBtn addTarget:self action:@selector(tabberBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[menuView addSubview:tabbarBtn];
UILabel *labe= [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(tabbarBtn.frame), menuView.width, menuView.width/4)];
labe.textAlignment = NSTextAlignmentCenter;
labe.text = titles[i];
[menuView addSubview:labe];
[self.content addSubview:menuView];
}
}
- (BOOL)touchPointInsideCircle:(CGPoint)center radius:(CGFloat)radius targetPoint:(CGPoint)point {
CGFloat dist = DIST(point, center);
NSLog(@"dist == %f, radius == %f",dist,radius);
return (dist <= radius);
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
self.startPoint = [[touches anyObject] locationInView:self];
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
CGPoint orige = CGPointMake(KSCREEN_WIDTH/2, KSCREEN_HEIGHT/2);
CGPoint currentPoint = [[touches anyObject] locationInView:self];
if ([self touchPointInsideCircle:orige radius:KSCREEN_WIDTH/2 targetPoint:currentPoint]) {
CGFloat startAngle = atan2(self.startPoint.y - orige.y,self.startPoint.x-orige.x);
CGFloat currentAngle = atan2(currentPoint.y - orige.y,currentPoint.x-orige.x);
CGFloat circleAngle = currentAngle-startAngle;
self.content.transform = CGAffineTransformRotate(self.content.transform,circleAngle);
for (int i = 0; i<self.views.count; i++) {
self.views[i].transform = CGAffineTransformRotate(self.views[i].transform,-circleAngle);
}
self.startPoint = currentPoint;
}
}
-(void)tabberBtnClick:(UIButton *)btn{
NSLog(@"%ld",btn.tag);
switch (btn.tag) {
case 1:
if ([self.delegate respondsToSelector:@selector(phoneMusicBtnClick) ]) {
[self.delegate phoneMusicBtnClick];
}
break;
case 2:
if ([self.delegate respondsToSelector:@selector(deviceMusicBtnClick) ]) {
[self.delegate deviceMusicBtnClick];
}
break;
case 3:
if ([self.delegate respondsToSelector:@selector(lightBtnClick) ]) {
[self.delegate lightBtnClick];
}
break;
case 4:
if ([self.delegate respondsToSelector:@selector(fmBtnClick) ]) {
[self.delegate fmBtnClick];
}
break;
case 5:
if ([self.delegate respondsToSelector:@selector(outVoiceBtnClick) ]) {
[self.delegate outVoiceBtnClick];
}
break;
case 6:
if ([self.delegate respondsToSelector:@selector(voiceKardBtnClick) ]) {
[self.delegate voiceKardBtnClick];
}
break;
case 7:
if ([self.delegate respondsToSelector:@selector(bleBtnClick) ]) {
[self.delegate bleBtnClick];
}
break;
default:
break;
}
}
@end