需求 - 24 - “罗盘”圆形

#define ITMEMARGIN 40.0f
#define BigRadius  120.0f


@interface CalculationCircularFirstViewController ()

@end

@implementation CalculationCircularFirstViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    UIButton * b1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [b1 setFrame:CGRectMake(0, 0, 40, 40)];
    [b1 setBackgroundColor:[UIColor redColor]];
    [b1.layer setMasksToBounds:YES];
    [b1.layer setCornerRadius:20];
    [b1 setTitle:@"1" forState:UIControlStateNormal];
    [self.view addSubview:b1];
    
    UIButton * b2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [b2 setFrame:CGRectMake(0, 0, 40, 40)];
    [b2 setBackgroundColor:[UIColor greenColor]];
    [b2.layer setMasksToBounds:YES];
    [b2.layer setCornerRadius:20];
    [b2 setTitle:@"2" forState:UIControlStateNormal];
    [self.view addSubview:b2];
    
    UIButton * b3 = [UIButton buttonWithType:UIButtonTypeCustom];
    [b3 setFrame:CGRectMake(0, 0, 40, 40)];
    [b3 setBackgroundColor:[UIColor blueColor]];
    [b3.layer setMasksToBounds:YES];
    [b3.layer setCornerRadius:20];
    [b3 setTitle:@"3" forState:UIControlStateNormal];
    [self.view addSubview:b3];
    
    UIButton * b4 = [UIButton buttonWithType:UIButtonTypeCustom];
    [b4 setFrame:CGRectMake(0, 0, 40, 40)];
    [b4 setBackgroundColor:[UIColor yellowColor]];
    [b4.layer setMasksToBounds:YES];
    [b4.layer setCornerRadius:20];
    [b4 setTitle:@"4" forState:UIControlStateNormal];
    [self.view addSubview:b4];
    
    UIButton * b5 = [UIButton buttonWithType:UIButtonTypeCustom];
    [b5 setFrame:CGRectMake(0, 0, 40, 40)];
    [b5 setBackgroundColor:[UIColor lightGrayColor]];
    [b5.layer setMasksToBounds:YES];
    [b5.layer setCornerRadius:20];
    [b5 setTitle:@"5" forState:UIControlStateNormal];
    [self.view addSubview:b5];
    
    UIButton * b6 = [UIButton buttonWithType:UIButtonTypeCustom];
    [b6 setFrame:CGRectMake(0, 0, 40, 40)];
    [b6 setBackgroundColor:[UIColor purpleColor]];
    [b6.layer setMasksToBounds:YES];
    [b6.layer setCornerRadius:20];
    [b6 setTitle:@"6" forState:UIControlStateNormal];
    [self.view addSubview:b6];
    
    UIButton * b7 = [UIButton buttonWithType:UIButtonTypeCustom];
    [b7 setFrame:CGRectMake(0, 0, 40, 40)];
    [b7 setBackgroundColor:[UIColor blackColor]];
    [b7.layer setMasksToBounds:YES];
    [b7.layer setCornerRadius:20];
    [b7 setTitle:@"7" forState:UIControlStateNormal];
    [self.view addSubview:b7];
    
    /*
     every 0 point :{182.17167854309082, 170.34563064575195}
     every 1 point :{198.25180530548096, 103.36706042289734}
     every 2 point :{156.31924629211426, 48.719482421875}
     every 3 point :{87.461061477661133, 46.916365623474121}
     every 4 point :{42.725930213928223, 99.29448127746582}
     every 5 point :{55.278630256652832, 167.02280521392822}
     every 6 point :{115.81312298774719, 199.89036083221436}
     */
    
    CGPoint point1 = [self itmePointIndex:1 count:7];
    CGPoint point2 = [self itmePointIndex:2 count:7];
    CGPoint point3 = [self itmePointIndex:3 count:7];
    CGPoint point4 = [self itmePointIndex:4 count:7];
    CGPoint point5 = [self itmePointIndex:5 count:7];
    CGPoint point6 = [self itmePointIndex:6 count:7];
    CGPoint point7 = [self itmePointIndex:7 count:7];
    
    [b1 setCenter:point1];
    [b2 setCenter:point2];
    [b3 setCenter:point3];
    [b4 setCenter:point4];
    [b5 setCenter:point5];
    [b6 setCenter:point6];
    [b7 setCenter:point7];
}


- (CGPoint)itmePointIndex:(NSInteger)index count:(NSInteger)count
{
    /* 角度是关键 */
    /* π/180 是1度角对应的弧度数,只要乘以度数,就可以得到弧度 */
    CGFloat degree = (360 / count) * (M_PI / 180);
    
    CGFloat radius = BigRadius - ITMEMARGIN;
    
    CGFloat x,y = 0.0f;
    y = cosf(degree * index);
    x = sinf(degree * index);
    
    NSLog(@"index x y:  :%ld %f %f", index, x, y);
    
    CGPoint point = CGPointMake((x * radius)+radius ,
                                    (y * radius)+radius );

    return point;
}

@end



效果如下:



可以把正弦余弦输出来看看:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值