创建绘图view(RJPieView)
.h文件内容
#import <UIKit/UIKit.h>
@class RJPieView;
@protocol RJPieViewDelegate <NSObject>//协议
- (void)currentClickIndex:(NSInteger)currentIndex;
@end
@interface RJPieView : UIView
@property (nonatomic, weak) id<RJPieViewDelegate> delegate;
@end
.m文件内容
#import "RJPieView.h"
//直径
#define radius 100
// 中心圆半近
#define centerRadius 50
@implementation RJPieView
@end
初始化方法
- (instancetype)init {
if (self = [super init]) {
UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(colorViewClick:)];
[self addGestureRecognizer:tapGes];
}
return self;
}
绘图
- (void)drawRect:(CGRect)rect {
CGFloat orignX = self.frame.size.width/2;
CGFloat orignY = self.frame.size.height/2;
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextClearRect(ctx, rect);
float angle_start = 0;
float angle_end = 0;
for (int i = 0; i < 8; i++) {
angle_end = M_PI/4*(i+1);
CGContextMoveToPoint(ctx, orignX, orignY);
int R = (arc4random() % 256) ;
int G = (arc4random() % 256) ;
int B = (arc4random() % 256) ;
if (i != 1 && i != 2) {
UIColor *colorRadi = [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1];
CGContextSetFillColor(ctx, CGColorGetComponents([colorRadi CGColor]));
CGContextAddArc(ctx, orignX, orignY, radius, angle_start, angle_end, 0);
CGContextFillPath(ctx);
}
angle_start = angle_end;
}
CGContextMoveToPoint(ctx, orignX, orignY);
CGContextSetFillColor(ctx, CGColorGetComponents([[UIColor yellowColor] CGColor]));
CGContextAddArc(ctx, orignX, orignY, centerRadius, 0, M_PI*2, 0);
CGContextFillPath(ctx);
}
区分点击区域
- (void)colorViewClick:(UITapGestureRecognizer *)tap {
CGPoint point = [tap locationInView:self];
CGFloat mariginX = point.x-self.frame.size.width/2;
CGFloat mariginY = (-point.y)+self.frame.size.height/2;
if (mariginX == 0 || mariginY == 0) {
return;
}
CGFloat xielu = mariginY/mariginX;
NSInteger currentIndex = -1;
// 求到中心点的距离
CGFloat distanceCenter = sqrt(pow(mariginX, 2) + pow(mariginY, 2));
if (distanceCenter <= centerRadius) {
NSLog(@"在黄色区域内");
currentIndex = 0;
}else {
if (mariginX > 0 && mariginY > 0) {
/// 第一象限
if (xielu > 1) {
/// 在1/4象限
NSLog(@"在1/8象限");
currentIndex = 1;
}else if (xielu < 1) {
NSLog(@"在2/8象限");
currentIndex = 2;
}
}else if (mariginX > 0 && mariginY < 0) {
/// 第二象限
if (xielu > -1) {
/// 在1/4象限
NSLog(@"在3/8象限");
currentIndex = 3;
}else if (xielu < -1) {
NSLog(@"在4/8象限");
currentIndex = -1;
}
}else if (mariginX < 0 && mariginY < 0) {
/// 第三象限
if (xielu > 1) {
/// 在1/4象限
NSLog(@"在5/8象限");
currentIndex = -1;
}else if (xielu < 1) {
NSLog(@"在6/8象限");
currentIndex = 6;
}
}else if (mariginX < 0 && mariginY > 0) {
/// 第三象限
if (xielu > -1) {
/// 在1/4象限
NSLog(@"在7/8象限");
currentIndex = 7;
}else if (xielu < -1) {
NSLog(@"在8/8象限");
currentIndex = 8;
}
}
}
if (currentIndex == -1) {
return;
}
if ([self.delegate respondsToSelector:@selector(currentClickIndex:)]) {
[self.delegate currentClickIndex:currentIndex];
}
}
使用方法
RJPieView *pieView = [[RJPieView alloc] init];
pieView.frame = CGRectMake(10, 100, 300, 300);
pieView.delegate = self;
[self.view addSubview:pieView];
实现代理方法
- (void)currentClickIndex:(NSInteger)currentIndex {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"当前点击的区域%ld",currentIndex] message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
}
网友评论