iOS 以小时为单位绘制时间圆盘,可点击指向预约时间

功能如下图,中间小红点可自定义是否存在,我在项目中主要用于指定一个时间。比如我指定到10点,我需要让线指向10 ,然后再做其他的事情。所以就研究出了这种时间圆盘,主要运用贝塞尔曲线和画直线来解决这个问题demo地址 http://code.cocoachina.com/view/137396

iOS 以小时为单位绘制时间圆盘,可点击指向预约时间_第1张图片
动态图1

需要的可以直接复制粘贴到项目的一个VC里即可看到该效果,也可以改成类似于这种效果的自己需要的效果。


iOS 以小时为单位绘制时间圆盘,可点击指向预约时间_第2张图片
图二


//

//  YuanHuanVController.m

//  SwiftFirstAPP

//

//  Created by 嗯,大葱 on 2018/7/11.

//  Copyright © 2018年 嗯,大葱. All rights reserved.

//

#import "YuanHuanVController.h"

@interface YuanHuanVController ()

{

    float radius;  //半径

    UIView *baseView;

    UIView *bgV;

    UIImageView *imageView;

    NSString *tempStr;//点击的时间

}

@end

@implementation YuanHuanVController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.

    bgV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 400, 350)];

    bgV.backgroundColor =[UIColor groupTableViewBackgroundColor];



    //可改变整个圆盘的大小

    baseView = [[UIView alloc] initWithFrame:CGRectMake(100, 70, 200, 200)];

    baseView.backgroundColor = [UIColor clearColor];

    [bgV addSubview:baseView];


    [self.view addSubview:bgV];


    //设置圆心,根据需求,可有可无

    UIView *centerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];

    centerView.center = baseView.center;

    centerView.backgroundColor = [UIColor redColor];

    [self.view addSubview:centerView];



    //M_PI的值

    //0 *M_PI 在最右边

    //0.5 *M_PI最下边

    //M_PI 最左边

    //1.5M_PI 最上边


    //下面注释掉的代码是根绝部分规律排列数字按钮,但是钟表类的12 3 6 9都是在水平直角上的,所以我拉出来单独绘制每个数字按钮,过程比较繁琐,但是基本都相等,就是改一点间距大小。

//    CGFloat angle=0;

//

//    while (angle <=1.82) {



    [self createCircleButtonWithAngle:0*M_PI numS:@"3"];

    [self createCircleButtonWithAngle:0.165*M_PI numS:@"4"];


    [self createCircleButtonWithAngle:0.325*M_PI numS:@"5"];



    [self createCircleButtonWithAngle:0.5*M_PI numS:@"6"];

    [self createCircleButtonWithAngle:0.665*M_PI numS:@"7"];

    [self createCircleButtonWithAngle:0.825*M_PI numS:@"8"];



    [self createCircleButtonWithAngle:1*M_PI numS:@"9"];

    [self createCircleButtonWithAngle:1.165*M_PI numS:@"10"];

    [self createCircleButtonWithAngle:1.325*M_PI numS:@"11"];


    [self createCircleButtonWithAngle:1.5*M_PI numS:@"12"];

    [self createCircleButtonWithAngle:1.665*M_PI numS:@"1"];

    [self createCircleButtonWithAngle:1.825*M_PI numS:@"2"];

//        angle = angle + 0.16;

//    }

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

//创建圆上指定角度对应的按钮

- (void)createCircleButtonWithAngle:(CGFloat)angle numS:(NSString *)numStr;

{

    radius = baseView.frame.size.width/2; //半径


    CGFloat x = radius*cos(angle);

    CGFloat y = radius*sin(angle);


    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];

    btn.center = CGPointMake(radius+x, radius+y);

    btn.backgroundColor = [UIColor groupTableViewBackgroundColor];

    [btn setTitle:numStr  forState:(UIControlStateNormal)];

    [btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];

    [btn setTitleColor:[UIColor whiteColor] forState:(UIControlStateSelected)];

    btn.layer.cornerRadius = btn.frame.size.width/2;

    btn.tag = [numStr intValue];

    [btn addTarget:self action:@selector(handleChooseTimeAction:) forControlEvents:(UIControlEventTouchUpInside)];

    [baseView addSubview:btn];


}

//选择的时间

- (void)handleChooseTimeAction:(UIButton *)sender {

    tempStr = sender.titleLabel.text;

    [self linkVlong:sender.frame.origin.x wid:sender.frame.origin.y];

    for (int i = 1; i < 13; i++) {

        UIButton *btn = (UIButton *)[self.view viewWithTag:i];


        if (i == sender.tag) {

            btn.selected = YES;

            [btn setBackgroundColor:[UIColor greenColor]];

        }else {

            [btn setBackgroundColor:[UIColor groupTableViewBackgroundColor]];

            btn.selected = NO;

        }

    }


}

- (void)linkVlong:(CGFloat)xT wid:(CGFloat)yt {


    if (imageView) {

        [imageView removeFromSuperview];

    }

  imageView=[[UIImageView alloc] initWithFrame:baseView.frame];

    [bgV addSubview:imageView];


//    baseView.backgroundColor = [UIColor blueColor];

    UIGraphicsBeginImageContext(imageView.frame.size);

    [imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];

    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);

    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1.0);  //线宽

    CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES);

    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 2.0, 0.0, 1.0, 1.0);  //颜色

    CGContextBeginPath(UIGraphicsGetCurrentContext());

    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), baseView.frame.size.width/2, baseView.frame.size.height/2);  //起点坐标

//    xT+17.5, yt+17.5  每个点的正中间

    if ([tempStr intValue] == 12) {

    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), xT+17.5, 17.5);  //终点坐标

    }else if ([tempStr intValue] == 1){

    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), xT+17.5-10, yt+17.5*2-2);  //终点坐标

    }else if ([tempStr intValue] ==2){

    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), xT, yt+17.5*2-10);  //终点坐标

    }else if ([tempStr intValue] ==3){

    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), xT-2, yt+17.5*2-18);  //终点坐标

    }

    else if ([tempStr intValue] ==4){

        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), xT, yt+17.5-6);  //终点坐标

    }

    else if ([tempStr intValue] ==5){

        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), xT+5, yt+3);  //终点坐标

    }


    else if ([tempStr intValue] ==6){

        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), xT+17.5, yt-1);  //终点坐标

    }


    else if ([tempStr intValue] ==7){

        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), xT+17.5+9, yt+2);  //终点坐标

    }

    else if ([tempStr intValue] ==8){

        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), xT+17.5*2-1, yt+17.5-6);  //终点坐标

    }


    else if ([tempStr intValue] ==9){

        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), xT+17.5*2-1,yt+17.5*2-17);  //终点坐标

    }

    else if ([tempStr intValue] ==10){

        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), xT+17.5*2-1,yt+17.5*2-10);  //终点坐标

    }


    else if ([tempStr intValue] ==11){

        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), xT+17.5*2-6,yt+17.5*2-5);  //终点坐标

    }

    CGContextStrokePath(UIGraphicsGetCurrentContext());

    imageView.image=UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

}

@end



你可能感兴趣的:(iOS 以小时为单位绘制时间圆盘,可点击指向预约时间)