iOS 扇形点击位置

1.先创建一个view试图

#import <UIKit/UIKit.h>


@interface Draw : UIView


@end



#import "Draw.h"


@implementation Draw


/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

 */

- (void)drawRect:(CGRect)rect {

    // Drawing code

    

    CGContextRef context = UIGraphicsGetCurrentContext();

    

   

    

    //画大圆并填充颜

    UIColor*aColor = [UIColor colorWithRed:1 green:0.0 blue:0 alpha:1];

    CGContextSetFillColorWithColor(context, aColor.CGColor);//填充颜色

    CGContextSetLineWidth(context, 3.0);//线的宽度

    

    

    //画笑脸弧线

    //

    CGContextSetRGBStrokeColor(context, 1, 0, 1, 1);//改变画笔

    

    /*画扇形和椭圆*/

    //画扇形,也就画圆,只不过是设置角度的大小,形成一个扇形

    aColor = [UIColor colorWithRed:0 green:1 blue:1 alpha:1];

    CGContextSetFillColorWithColor(context, aColor.CGColor);//填充颜色

    //10为半径围绕圆心画指定角度扇形

    CGContextMoveToPoint(context, 200, 200);

    CGContextAddArc(context, 200, 200, 1000 * M_PI / 180, 90 * M_PI / 180, 0);

    CGContextClosePath(context);

    CGContextDrawPath(context, kCGPathFillStroke); //绘制路径

    

    

    CGContextMoveToPoint(context, 200, 200);

    CGContextAddArc(context, 200, 200, 10090 * M_PI / 180, 180 * M_PI / 180, 0);

    CGContextClosePath(context);

    CGContextDrawPath(context, kCGPathFillStroke);

    

    

    CGContextMoveToPoint(context, 200, 200);

    CGContextAddArc(context, 200, 200, 100180 * M_PI / 180, 270 * M_PI / 180, 0);

    CGContextClosePath(context);

    CGContextDrawPath(context, kCGPathFillStroke);

    

    

    CGContextMoveToPoint(context, 200, 200);

    CGContextAddArc(context, 200, 200, 100270 * M_PI / 180, 360 * M_PI / 180, 0);

    CGContextClosePath(context);

    CGContextDrawPath(context, kCGPathFillStroke);

    

    

    

    

}


@end


2.在要用到的地方创建一下

Draw *draw1 = [[Draw alloc]initWithFrame:CGRectMake(0, 0, 300, 300)];

    draw1.backgroundColor = [UIColor clearColor];

    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];

    [draw1 addGestureRecognizer:tap1];

    [self.view addSubview:draw1];


- (void)tap:(UITapGestureRecognizer *)tap

{

    CGPoint point = [tap locationInView:self.view];

    

    NSLog(@"。。。触点的位置%@",NSStringFromCGPoint(point));

    

    CGPoint centerPoint = CGPointMake(200, 200);

    

    NSLog(@"。。。中心点的坐标%@",NSStringFromCGPoint(centerPoint));

    

    CGPoint startPoint = CGPointMake(300, 200);

    

   CGFloat distance = [self distanceFromPointX:point distanceToPointY:centerPoint];

    

   CGFloat rads = [self radiansToDegreesFromPointX:point ToPointY:startPoint ToCenter:centerPoint];

    NSLog(@"%.2f",rads);

    if (rads>0&&rads<=1.57) {

        NSLog(@"11");

    };

    if (rads>1.57&&rads<=3.14) {

        NSLog(@"22");

    };

    if (rads>3.14&&rads<=4.71) {

        NSLog(@"33");

    };

     if (rads>4.71) {

     NSLog(@"44");

     };

}


#pragma mark - 计算触点和中心点的弧度

    

-(float)radiansToDegreesFromPointX:(CGPoint)start ToPointY:(CGPoint)end ToCenter:(CGPoint)center{

        

        float rads;

        

        CGFloat a = (end.x - center.x);

        

        CGFloat b = (end.y - center.y);

        

        CGFloat c = (start.x- center.x);

        

        CGFloat d = (start.y- center.y);

        

        rads = acos(((a*c) + (b*d)) / ((sqrt(a*a + b*b)) * (sqrt(c*c + d*d))));

    NSLog(@"--%f,%f",start.x,center.x);

        if (start.y < center.y) {

            

            rads = 2*M_PI - rads;

            

       }

        

        return rads;

        

    }

    

#pragma mark - 计算触点到中心点的距离

    

-(float)distanceFromPointX:(CGPoint)start distanceToPointY:(CGPoint)end{

        

        float distance;

        

        CGFloat xDist = (end.x - start.x);

        

        CGFloat yDist = (end.y - start.y);

        

        distance = sqrt((xDist * xDist) + (yDist * yDist));

        

        return distance;

        

}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值