实战以中心点绘制圆点并显示文字
来源:互联网 发布:淘宝动漫周边 侵权 编辑:程序博客网 时间:2024/05/21 23:32
网上看了些资料,整理并实践了标题所说的问题.分享出来.
先上效果图
代码如下
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
_lineWidth = 20;
_radius = 40.0;//
_myColor = [UIColorcolorWithRed:255/255.0fgreen:0/255.0fblue:0/255.0falpha:1.0f];
}
return self;
}
#pragma mark - drawing methods
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CGContextRef tx = UIGraphicsGetCurrentContext();
//绘制圆
[self drawRadar:tx];
//绘制圆上文字
[self drawMyLabel:tx];
}
-(void) drawRadar:(CGContextRef)tx{
CGContextSaveGState(tx);
[_myColor set];
for (int i = 0; i < 10; i++) {
CGPoint point = [selfpointFromAngle:36*i];
CGContextFillEllipseInRect(tx,CGRectMake(point.x, point.y,_lineWidth,_lineWidth));
}
CGContextRestoreGState(tx);
}
-(void) drawMyLabel:(CGContextRef)tx {
NSDictionary *dic = @{ NSFontAttributeName: [UIFontsystemFontOfSize:9.0],
NSForegroundColorAttributeName: [UIColorwhiteColor]};
int pad = 0;
for (int i=0; i<10; i++) {
NSString* text = [NSStringstringWithFormat:@"%@%.2d",NSLocalizedString(@"A",@"A"),i + 1];
CGPoint curPoint = [selfpointFromAngle:36*i];
CGRect textRect = CGRectMake(curPoint.x, curPoint.y, [selfwidthOfString:textwithFont:[UIFontsystemFontOfSize:9.0]], [selfheightOfString:textwithFont:[UIFontsystemFontOfSize:9.0]]);
float center = ToRad(AngleFromNorth(CGPointMake(self.frame.size.width/2,self.frame.size.height/2), curPoint,NO));
textRect.origin.x = (textRect.origin.x + pad *cos(center)) - textRect.size.width/4 +5.0;
textRect.origin.y = (textRect.origin.y + pad *sin(center))- textRect.size.height/4 +5.0;
[text drawInRect:textRect withAttributes:dic];
}
}
//计算逆时针方向的角度对应的坐标
-(CGPoint)pointFromAngle:(int)angleInt{
//Define the Circle center
CGPoint centerPoint = CGPointMake(self.frame.size.width/2 -_lineWidth/2,self.frame.size.height/2 -_lineWidth/2);
//Define The point position on the circumference
CGPoint result;
result.y = round(centerPoint.y +_radius *sin(ToRad(-angleInt-90)));
result.x = round(centerPoint.x +_radius *cos(ToRad(-angleInt-90)));
return result;
}
static inline float AngleFromNorth(CGPoint p1, CGPoint p2, BOOL flipped) {
CGPoint v = CGPointMake(p2.x-p1.x,p2.y-p1.y);
float vmag = sqrt(SQR(v.x) +SQR(v.y)), result =0;
v.x /= vmag;
v.y /= vmag;
double radians = atan2(v.y,v.x);
result = ToDeg(radians);
return (result >=0 ? result : result +360.0);
}
- (CGFloat) widthOfString:(NSString *)string withFont:(UIFont*)font {
NSDictionary *attributes = [NSDictionarydictionaryWithObjectsAndKeys:font,NSFontAttributeName,nil];
return [[[NSAttributedStringalloc]initWithString:string attributes:attributes]size].width;
}
- (CGFloat) heightOfString:(NSString *)string withFont:(UIFont*)font {
NSDictionary *attributes = [NSDictionarydictionaryWithObjectsAndKeys:font,NSFontAttributeName,nil];
return [[[NSAttributedStringalloc]initWithString:string attributes:attributes]size].height;
}
- 实战以中心点绘制圆点并显示文字
- 鼠标滚轮实现地图放大缩小 并以滚轮点为中心点
- 实战以中心点旋转UIImageView 360度
- android开发,绘制圆形图片并添加文字居中显示
- 基于Python OpenCV加载视频文件,显示,并绘制自定义文字
- 以倒影效果显示文字
- 以投影效果显示文字
- java获取一段文字的url地址并以链接的形式显示在网页上
- js 文字超出长度用省略号代替,鼠标悬停并以悬浮框显示
- GPS以中心点为圆心,指定距离为半径获取随机坐标点
- highcharts中绘制离散的事件点,并在图中显示中文说明内容
- 在窗口显示文字和绘制图形
- ProgressBar颜色设定及绘制文字显示
- 自定义View绘制文字居中显示
- MFC CDC 区域绘制文字居中显示
- MovieClip设置以中心点旋转
- 从网络端leanCloud获取上传的图片和文字并以listview的形式显示在android端
- ArcGis For Silverlight API,地图显示Gis,绘制点,线,绘制图等(三)--绘制点、线、圆,显示提示信息
- ffplay的快捷键以及选项
- 安装包注册表分析工具(批处理)
- 在Android中访问内置SE和基于SE的卡模拟(二)
- 使用maven整合多个web项目,多个war合并
- Android TV 焦点上下左右移动
- 实战以中心点绘制圆点并显示文字
- Bitmap createScaleBitmap()需要注意点
- Android手机通过蓝牙控制小车
- 【python】matplotlib.pyplot介绍
- 一天征服Spark!
- uvm_config_db使用注意
- mysql 转义字符
- ATL 获取PPT控件内容
- CocoaPods安装使用心得,分享给墙内的朋友们