GSProgressView

本人极不推荐使用drawRect的方式来绘制下载进度条,无论机器的性能怎么高,使用drawRect用于绘制图形都是低效的。

A cute little circular progress view for iOS

一款轻巧的显示圆形进度的的view,用于iOS开发

Installation - 安装

Drag GSProgressView.h and GSProgressView.m into your project.

将GSProgressView.h与GSProgressView.m拖到你的工程当中。

Serving suggestion - 建议的使用方式

GSProgressView *pv = [[GSProgressView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
pv.color = [UIColor redColor];
pv.progress = 0.6;
[myView addSubview:pv];
GSProgressView.h + GSProgressView.m
//
// GSProgressView.h
//
// Created by Simon Whitaker on 14/11/2012.
// Copyright (c) 2012 Goo Software Ltd. All rights reserved.
// #import <UIKit/UIKit.h> @interface GSProgressView : UIView @property (nonatomic) CGFloat progress;
@property (strong, nonatomic) UIColor *color UI_APPEARANCE_SELECTOR;
@property (strong, nonatomic) UIColor *tickColor UI_APPEARANCE_SELECTOR; @end
//
// GSProgressView.m
//
// Created by Simon Whitaker on 14/11/2012.
// Copyright (c) 2012 Goo Software Ltd. All rights reserved.
// #import "GSProgressView.h"
#import <QuartzCore/QuartzCore.h> @implementation GSProgressView - (void)commonInit {
self.backgroundColor = [UIColor clearColor];
} - (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self commonInit];
}
return self;
} - (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self commonInit];
}
return self;
} - (void)setProgress:(CGFloat)progress {
if (progress > 1.0) progress = 1.0; if (progress != _progress) {
_progress = progress;
[self setNeedsDisplay];
}
} - (void)drawRect:(CGRect)rect {
if ([self color] == nil)
[self setColor:[UIColor blackColor]]; CGPoint center = CGPointMake(rect.size.width/, rect.size.height/);
CGFloat radius = MIN(rect.size.width, rect.size.height)/; // Start a path
UIBezierPath *path = [UIBezierPath bezierPath]; // Move to centre and draw an arc. [path moveToPoint:center];
[path addArcWithCenter:center
radius:radius
startAngle: - M_PI_2 // zero degrees is east, not north, so subtract pi/2
endAngle: * M_PI * [self progress] - M_PI_2 // ditto
clockwise:YES];
[path closePath]; // If progress is 1.0, show a tick mark in the centre of the circle
if ([self progress] == 1.0) {
/*
First draw a tick that looks like this: A---F
| |
| E-------D
| |
B-----------C (Remember: (0,0) is top left)
*/
UIBezierPath *tickPath = [UIBezierPath bezierPath];
CGFloat tickWidth = radius/;
[tickPath moveToPoint:CGPointMake(, )]; // A
[tickPath addLineToPoint:CGPointMake(, tickWidth * )]; // B
[tickPath addLineToPoint:CGPointMake(tickWidth * , tickWidth * )]; // C
[tickPath addLineToPoint:CGPointMake(tickWidth * , tickWidth)]; // D
[tickPath addLineToPoint:CGPointMake(tickWidth, tickWidth)]; // E
[tickPath addLineToPoint:CGPointMake(tickWidth, )]; // F
[tickPath closePath]; // Now rotate it through -45 degrees...
[tickPath applyTransform:CGAffineTransformMakeRotation(-M_PI_4)]; // ...and move it into the right place.
[tickPath applyTransform:CGAffineTransformMakeTranslation(radius * 0.43, radius)]; // Account for non-square views
CGFloat xOffset = rect.size.width/ - radius;
CGFloat yOffset = rect.size.height/ - radius;
[tickPath applyTransform:CGAffineTransformMakeTranslation(xOffset, yOffset)]; // Add fill color if it's set
if (self.tickColor) {
[[self tickColor] setFill];
[tickPath fill];
} // Add the tick path to the existing circle path
[path appendPath:tickPath];
};
path.usesEvenOddFillRule = YES; [[self color] setFill];
[path fill];
} #pragma mark - Accessibility - (BOOL)isAccessibilityElement {
return YES;
} - (NSString *)accessibilityLabel {
return NSLocalizedString(@"Progress", @"Accessibility label for GSProgressView");
} - (NSString *)accessibilityValue {
// Report progress as a percentage, same as UISlider, UIProgressView
return [NSString stringWithFormat:@"%d%%", (int)round([self progress] * 100.0)];
} - (UIAccessibilityTraits)accessibilityTraits {
return UIAccessibilityTraitUpdatesFrequently;
} @end

最新文章

  1. 【UOJ #246】【UER #7】套路
  2. Private-code MaxCounter
  3. js控制键盘只能输入数字和退格键,delete键
  4. js 所有事件列表
  5. log4j里面的info,debug,error级别有什么区别
  6. POJ 3026 : Borg Maze(BFS + Prim)
  7. IOS调用相机相册
  8. PHP — php精粹-编写高效的php代码 --- API
  9. 帝国cms7.0修改默认搜索模版中的分页[!--show.page--]
  10. WPS怎样设置多级标题(如四级标题)
  11. HTML5 简单实现刮刮乐效果
  12. java学习笔记day06---匿名内部类
  13. xshell使用命令总结
  14. STL algorithm算法minmax,minmax_element(36)
  15. 元素NULL判断
  16. android开发文章收藏
  17. Charles 连接手机抓包出现Unknown,一直无法抓包的问题解决
  18. 【转】百度站长平台MIP引入工具使用心得
  19. vue的父子组件间的相互传参props及props数据的多种验证机制
  20. CHENGDU1-Python编程语言和PEP8规范

热门文章

  1. 【Maven学习】maven-assembly-plugin的使用
  2. Hadoop Hive概念学习系列之hive三种方式区别和搭建、HiveServer2环境搭建、HWI环境搭建和beeline环境搭建(五)
  3. WPF INotifyPropertyChanged
  4. Tomcat的配置文件Server.xml解析
  5. 深入js之基本语法
  6. throws 与 throw
  7. 每天一道leetcode203-移除链表的元素
  8. Swift函数_默认参数
  9. 面向对象(基础oop)之继承总结
  10. 对JDK、JRE和JVM的一些浅薄理解