iOS-在一个圆环上放置相切的小圆

iOS-在一个圆环上放置相切的小圆

 

//
//  ViewController.m
//  珠宝
//
//  Created by yuency on 17/4/28.
//  Copyright © 2017年 yuency. All rights reserved.
//

#import "ViewController.h"
#import "PearlPoint.h"


//圆形的半径
CGFloat const circleradius = 200;

#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)

//珠子的个数 4个 5,6,7 , 8个有问题
NSInteger const pearlsCount = 4;


@interface ViewController ()

///计算所有珠子的坐标
@property (nonatomic, strong) NSMutableArray *pointArray;

///圆环
@property (nonatomic, strong) UIView *circleView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //创建灰色背景
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, circleradius * 2, circleradius * 2)];
    backView.center = self.view.center;
    backView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:backView];
    
    
    //创建圆环
    self.circleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, circleradius, circleradius)];
    self.circleView.center = CGPointMake(backView.frame.size.width / 2, backView.frame.size.height / 2);
    self.circleView.layer.borderColor = [UIColor yellowColor].CGColor;
    self.circleView.layer.borderWidth = 5;
    self.circleView.layer.cornerRadius = circleradius / 2;
    [backView addSubview:self.circleView];
    
    
    //计算圆环上珠子的坐标
    [self makePointArray];
    
    
    //把珠子添加到圆环上
    for (PearlPoint *point in self.pointArray) {
        UIView *view = [self makePearls];
        view.center = CGPointMake(point.x, point.y);
        [backView addSubview:view];
    }
}



#pragma mark - 制造珠子
- (UIView *)makePearls {
    
    //90°角放多少个珠子
    NSUInteger count = pearlsCount;
    
    //珠子圆心到切线的角度 一定要是浮点型
    CGFloat cuttingAngle = (90 / (count - 1)) / 2.0f;
    
    //减少半径
    CGFloat banjing = circleradius / 2.f;
    
    //计算 x 坐标
    CGFloat xPoint = banjing * sin(DEGREES_TO_RADIANS(cuttingAngle));
    //计算 y 坐标
    CGFloat yPoint = banjing - banjing * cos(DEGREES_TO_RADIANS(cuttingAngle));
    
    //半径
    CGFloat temp = xPoint * xPoint + yPoint * yPoint;
    CGFloat dirous = sqrt(temp);
    
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, dirous * 2, dirous * 2)];
    view.backgroundColor = [UIColor redColor];
    view.layer.cornerRadius = dirous;
    view.alpha = 0.5;
    return view;
}



#pragma mark - 计算珠子坐标
- (void)makePointArray {
    
    self.pointArray = [NSMutableArray array];
    
    //半径
    CGFloat banjing = circleradius / 2;
    
    //根据半径计算珠子应该在的位置, 从上面的最右边的点开始计算. 在长和宽都是 circleradius 中心点 是 (100,100)
    NSUInteger count = pearlsCount;
    CGFloat angle = (90 / (count - 1));
    
    //个数
    int k = 360 / angle;
    
    //角度
    CGFloat drgree = M_PI / ( (count - 1) * 2);
    
    for (int i = 0; i < k; i++) {
        PearlPoint *point = [[PearlPoint alloc] init];
        point.x = banjing * cos(drgree * i) + circleradius;
        point.y = banjing * sin(drgree * i) + circleradius;
        [self.pointArray addObject:point];
    }
}

@end

 

posted @ 2017-04-28 20:56  代码将知道什么是痛苦  阅读(801)  评论(0编辑  收藏  举报