#import <UIKit/UIKit.h> // 设置类为可视化设计 IB_DESIGNABLE @interface MyView : UIView // IBInspectable 为可视化设计属性 // 边框宽度 @property (nonatomic,assign)IBInspectable float borderWidth; // 边框颜色 @property (nonatomic,retain)IBInspectable UIColor* borderColor; // 圆角 @property (nonatomic,assign)IBInspectable float cornerRadius; @end
//
// MyView.m
// 01_CirProgress
//
// Created by xgao on 15/10/29.
// Copyright (c) 2015年 xgao. All rights reserved.
//
#import "MyView.h"
@implementation MyView
// 边框宽度
- (void)setBorderWidth:(float)borderWidth{
self.layer.borderWidth = borderWidth;
}
// 边框颜色
- (void)setBorderColor:(UIColor *)borderColor{
self.layer.borderColor = borderColor.CGColor;
}
// 圆角
- (void)setCornerRadius:(float)cornerRadius{
self.layer.cornerRadius = cornerRadius;
}
@end
#import <UIKit/UIKit.h> @interface MyProgress : UIView // 当时进度值 @property (nonatomic,assign) float progressValue; @end
#import "MyProgress.h"
@implementation MyProgress
{
float _proValue;
}
// 重写初始化方法
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
// 设置背影为透明色
self.backgroundColor = [UIColor clearColor];
}
return self;
}
// 重设progressValue属性
- (void)setProgressValue:(float)progressValue
{
_progressValue = progressValue;
// 重新画UI
[self setNeedsDisplay];
}
// 绘图
- (void)drawRect:(CGRect)rect {
// 获取画图的上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
/**** 绘制圆形背景线 ****/
// 圆的半径
float r = rect.size.width / 2.0;
// 全圆
CGFloat endAngle = M_PI * 2;
// 画圆形线
CGContextAddArc(ctx, r, r, r, 0, endAngle, 0);
// 背影颜色
CGContextSetRGBFillColor(ctx, 0.7, 0.7,0.7, 1);
// 完成绘制
CGContextFillPath(ctx);
/**** 绘制扇形区域 ****/
// 计算结束角度
endAngle = M_PI * 2 * _progressValue;
/** 画圆
* 参数1:c 当前上下文
* 参数2:x 圆的X坐标
* 参数3:y 圆的Y坐标
* 参数4:radius 圆的半径
* 参数5:startAngle 开始角度
* 参数6:endAngle 结束角度
* 参数7:clockwise 是否逆时针
*/
CGContextAddArc(ctx, r, r, r, 0, endAngle, 0);
// 连成线,成弧形
CGContextAddLineToPoint(ctx, r, r);
// 其实就是在连接的那条线上加一个点,让线条连接到那一个点,就像拉弓,可加多个点
// CGContextAddLineToPoint(ctx, r + 20, r + 20);
// 填充颜色
CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);
// 完成绘制
CGContextFillPath(ctx);
}
#import "MyProgress.h"
@interface ViewController ()
{
MyProgress* _myProView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 创建自定义控件
_myProView = [[MyProgress alloc]initWithFrame:CGRectMake(100, 50, 180, 180)];
// 默认进度
_myProView.progressValue = 0.2;
[self.view addSubview:_myProView];
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有