zoukankan      html  css  js  c++  java
  • iOS开发——自定义进度圆环

      1、在DrawCircle.h文件中

      提供了接口,在使用的时候,可以设定圆心、半径、角度、圆环的宽度、圆环的背景底色、圆环的进度条颜色,当然后面三个有自定义的值。

    //

    //  DrawCircle.h

    //  Demo-draw

    //

    //  Created by yyt on 16/5/10.

    //  Copyright © 2016年 yyt. All rights reserved.

    //

    #import <UIKit/UIKit.h>

    @interface DrawCircle : UIView

    @property(nonatomic,assign) CGPoint centerPoint;

    @property(nonatomic,assign) CGFloat radius;

    @property(nonatomic,assign) CGFloat angleValue;  //圆环进度占有的角度,0~360

    @property(nonatomic,assign) CGFloat lineWidth;

    @property(nonatomic,strong) UIColor *bgLineColor;

    @property(nonatomic,strong) UIColor *lineColor;

    @end

    2、在DrawCircle.m文件中

    //

    //  DrawCircle.m

    //  Demo-draw

    //

    //  Created by yyt on 16/5/10.

    //  Copyright © 2016年 yyt. All rights reserved.

    //

    #import "DrawCircle.h"

    @implementation DrawCircle

    - (instancetype)initWithFrame:(CGRect)frame {

        self = [super initWithFrame:frame];

        self.backgroundColor = [UIColor whiteColor];

        self.lineWidth = 10;

        self.bgLineColor = [UIColor lightGrayColor];

        self.lineColor = [UIColor orangeColor];

        return self;

    }

    - (void)drawRect:(CGRect)rect {

        CGContextRef bgContextRef = UIGraphicsGetCurrentContext();

        CGContextAddArc(bgContextRef, _centerPoint.x, _centerPoint.y, _radius, 0, 10, 0);

        CGContextSetLineWidth(bgContextRef, _lineWidth);

        [_bgLineColor setStroke];

        CGContextStrokePath(bgContextRef);

        

        CGContextRef contextRef = UIGraphicsGetCurrentContext();

        CGContextAddArc(contextRef, _centerPoint.x, _centerPoint.y, _radius, M_PI/2, M_PI/2+_angleValue/180*M_PI, 0);

        CGContextSetLineWidth(contextRef, _lineWidth);

        [_lineColor setStroke];

        CGContextStrokePath(contextRef);

    }

    @end

    3、在需要使用圆环进度条的地方ViewController.m文件中

    //

    //  ViewController.m

    //  Demo-draw

    //

    //  Created by yyt on 16/5/10.

    //  Copyright © 2016年 yyt. All rights reserved.

    //

    #import "ViewController.h"

    #import "DrawCircle.h"

    @interface ViewController ()

    @property(nonatomic,strong) DrawCircle *view2;

    @end

    @implementation ViewController

    static int hehe = 30;

    - (void)viewDidLoad {

        [super viewDidLoad];

        

        DrawCircle *view2 = [[DrawCircle alloc] initWithFrame:CGRectMake(100, 200, 100, 100)];

        self.view2 = view2;

        view2.centerPoint = CGPointMake(50, 50);

        view2.radius = 30;

        view2.angleValue = hehe;

        view2.lineWidth = 20;

        view2.lineColor = [UIColor orangeColor];

        [self.view addSubview:view2];

        

      //进度+

        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

        button.frame = CGRectMake(100, 100, 100, 30);

        button.backgroundColor = [UIColor blueColor];

        [button addTarget:self action:@selector(hehe) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:button];

        

      //进度-

        UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];

        button2.frame = CGRectMake(100, 150, 100, 30);

        button2.backgroundColor = [UIColor redColor];

        [button2 addTarget:self action:@selector(hehe2) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:button2];

    }

    - (void)hehe {

        hehe += 30;

        self.view2.angleValue = hehe;

        [self.view2 setNeedsDisplay];

    }

    - (void)hehe2 {

        hehe -= 30;

        self.view2.angleValue = hehe;

        [self.view2 setNeedsDisplay];

    }

    @end

  • 相关阅读:
    Java读书笔记(2)-输入输出
    Java读书笔记(1)-异常处理
    Photoshop自动导出各尺寸Android和Iphone图标,支持新版Android Studio
    【原创】我的研究生活
    [原创]使用Fiddler抓取手机APP流量--360WIFI
    Federa 7 配置yum 源
    开源自己写的刷票器软件(windows和Android)
    更新linux kernel到3.14.10 LTS版后,virt-manager无法识别qemu hypervisor的问题
    Net Core Identity 身份验证:注册、登录和注销 (简单示例)
    Net Core的API文档工具Swagger
  • 原文地址:https://www.cnblogs.com/yyt-hehe-yyt/p/5488455.html
Copyright © 2011-2022 走看看