围绕圆形创建视图

- (void)loadUI{
    
    
    CGFloat centerx = self.view.center.x;
    CGFloat centery = self.view.center.y;
    /**
     *  画圆
     */
    UIView *centerView = [[UIView alloc]init];
    
    centerView.backgroundColor = [UIColor redColor];
    centerView.frame = CGRectMake(0.0, 0.0,50,50);
    centerView.center = CGPointMake(centerx,centery);
    
    centerView.layer.cornerRadius = 25;
    
    [self.view addSubview:centerView];
    
    for (int i = 0 ; i < 2; i ++ ) {
        
        CAShapeLayer *solidLine =  [CAShapeLayer layer];
        
        CGMutablePathRef solidPath =  CGPathCreateMutable();
        //线宽
        solidLine.lineWidth = 1.0f ;
        //颜色
        solidLine.strokeColor = [UIColor blackColor].CGColor;
        //线性
        solidLine.fillColor = [UIColor clearColor].CGColor;
        //
        CGFloat line_X = self.view.center.x - RADIUS - i * 3;
        CGFloat line_y = self.view.center.y - RADIUS - i * 3;
        
        CGPathAddEllipseInRect(solidPath, nil, CGRectMake(line_X,  line_y, RADIUS * 2 + i * 6 ,  RADIUS  * 2 + i * 6));
        solidLine.path = solidPath;
        CGPathRelease(solidPath);
        
        [self.view.layer addSublayer:solidLine];

    }
    
    for (NSInteger i = 0 ;i < PHOTONUM ;i++) {
        
        CGFloat tmpy =  centery + RADIUS*cos(2.0*M_PI *i/PHOTONUM);
        CGFloat tmpx =  centerx - RADIUS*sin(2.0*M_PI *i/PHOTONUM);
        
        UIView *view = [[UIView alloc] init];
        
        view.backgroundColor = [UIColor redColor];
        view.frame = CGRectMake(0.0, 0.0,50,50);
        view.center = CGPointMake(tmpx,tmpy);
        
        view.layer.cornerRadius = 25;
        
        [self.view addSubview:view];
        
        
        CGFloat imgx =  centerx + (RADIUS + 2)*cos(2.0 * M_PI *i/PHOTONUM);
        CGFloat imgy =  centery - (RADIUS + 2)*sin(2.0 * M_PI *i/PHOTONUM);
        
        UIImageView  *imgView = [[UIImageView alloc] init];
        
        imgView.frame  = CGRectMake(0.0 , 0.0 , 15 , 15);
        imgView.center = CGPointMake(imgx,imgy);
        imgView.image  = [UIImage imageNamed:@"icon"];
        
        
        //尖角有问题,明个修复
        [self makeRotation:imgView speedX:imgy speedY:imgx];
        [self.view addSubview:imgView];
        
    }
}

-(UIImageView *) makeRotation:(UIImageView *)image speedX:(float)X speedY:(float)Y
{
    if (Y<0) {
        image.transform = CGAffineTransformMakeRotation(atan(X/(-Y)));
    }else if(Y>0){
        image.transform = CGAffineTransformMakeRotation(atanf(X/(-Y))-PI);
    }
    return image;
}

效果:

围绕圆形创建视图_第1张图片
IMG_4733.jpg

你可能感兴趣的:(围绕圆形创建视图)