繪制扇形小按鈕

創(chuàng)建繪圖view(RJPieView)

.h文件內(nèi)容

#import <UIKit/UIKit.h>
@class RJPieView;
@protocol RJPieViewDelegate <NSObject>//協(xié)議
- (void)currentClickIndex:(NSInteger)currentIndex;

@end
@interface RJPieView : UIView
@property (nonatomic, weak) id<RJPieViewDelegate> delegate;
@end

.m文件內(nèi)容

#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);
}

區(qū)分點(diǎn)擊區(qū)域

- (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;
    // 求到中心點(diǎn)的距離
    CGFloat distanceCenter = sqrt(pow(mariginX, 2) + pow(mariginY, 2));
    if (distanceCenter <= centerRadius) {
        NSLog(@"在黃色區(qū)域內(nèi)");
        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];

實(shí)現(xiàn)代理方法

- (void)currentClickIndex:(NSInteger)currentIndex {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"當(dāng)前點(diǎn)擊的區(qū)域%ld",currentIndex] message:nil delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
    [alert show];
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. 【從本篇文章中我學(xué)習(xí)到的最重要的概念】: 要想變的一直優(yōu)秀,一個(gè)重要的因素就是持之以恒。 2.【 我在本篇文...
    109強(qiáng)宇閱讀 248評(píng)論 2 0
  • 民間是什么,民間便是你我出生的地方,在藍(lán)天下,大地上,在自己家鄉(xiāng)處。 記得小時(shí)候奶奶家,在老城區(qū)里,那里頭,排...
    文玲子閱讀 453評(píng)論 0 1
  • 做夢(mèng)了,夢(mèng)見尸首 醒來,一切照舊,心想,那是夢(mèng)。要幸福! 可自己依然難受,忘不了的夢(mèng),忘不了的過去。害怕,幸福,不...
    行走的大鳥閱讀 107評(píng)論 0 0
  • 為什么讀? 近期要上線 互聯(lián)網(wǎng)產(chǎn)品眾籌平臺(tái),通過眾籌的模式幫助創(chuàng)業(yè)者、產(chǎn)品經(jīng)理、技術(shù)愛好者將idea變成produ...
    嘉慕閱讀 653評(píng)論 0 0

友情鏈接更多精彩內(nèi)容