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

  • 相关阅读:
    超值干货:微服务架构下如何解耦,对于已经紧耦合下如何重构?
    程序员收藏不看系列:近三万字总结Spring注解开发!
    干货收藏:6 款能挣钱的 Spring Boot 开源后台管理系统
    美团二面:你向 Mysql 数据库插入 100w 条数据用了多久?
    5分钟快速掌握阿里内部MySQL性能优化的核心技术!
    优秀!一鼓作气学会“一致性哈希”,就靠这 18 张图了
    分库分表神器 Sharding-JDBC,几千万的数据你不搞一下?
    熬夜肝出5大点,18张图带你彻底弄懂MySQL事务日志
    jdk8新特性Stream
    java多线程
  • 原文地址:https://www.cnblogs.com/yyt-hehe-yyt/p/5488455.html
Copyright © 2011-2022 走看看