ios学习第六天(二)自定义UIView中
2017-04-12 16:40
176 查看
这次用自定义view创造一个不存在的view,看效果图
这个就是了,但是我不知道背景怎么是黑色的,好丑,经过探索后发现在外边设置背景颜色即可,设置成透明色。
来不及解释了,快看代码:
//
// MyCircleView.m
// MyUIView
//
// Created by Moluth on 17/4/12.
// Copyright (c) 2017年 Moluth. All rights reserved.
//
#import "MyCircleView.h"
@implementation MyCircleView
-(void)drawRect:(CGRect)rect{
//绘画区域
CGRect bounds = [self bounds];
// 中心点
CGPoint center;
center.x = bounds.origin.x + bounds.size.width / 2.0;
center.y = bounds.origin.y + bounds.size.height / 2.0;
// 为了不超过图像的边缘,计算最大的半径
float mr = hypot(bounds.size.width, bounds.size.height) / 2.0;
// 获取图形绘制上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//线条宽度是20
CGContextSetLineWidth(context, 15);//线条粗细
//循环绘制圆
for (float r = mr-30; r > 0; r -= 10)
{
// 设置线条颜色
[[[UIColor alloc] initWithRed:(sin(r)+1.0)/2.0 green:(cos(r*127.0+2.0)+1.0)/2.0 blue:(cos(r)+1.0)/2.0 alpha:0.5] setStroke];
CGContextAddArc(context, center.x, center.y,r, 0.0, M_PI * 2.0, YES);//添加弧型路径
CGContextStrokePath(context);//绘制路径
}
}
@end
给大家推荐一篇文章,写的很不错:http://blog.csdn.net/rhljiayou/article/details/9919713
这个就是了,但是我不知道背景怎么是黑色的,好丑,经过探索后发现在外边设置背景颜色即可,设置成透明色。
来不及解释了,快看代码:
//
// MyCircleView.m
// MyUIView
//
// Created by Moluth on 17/4/12.
// Copyright (c) 2017年 Moluth. All rights reserved.
//
#import "MyCircleView.h"
@implementation MyCircleView
-(void)drawRect:(CGRect)rect{
//绘画区域
CGRect bounds = [self bounds];
// 中心点
CGPoint center;
center.x = bounds.origin.x + bounds.size.width / 2.0;
center.y = bounds.origin.y + bounds.size.height / 2.0;
// 为了不超过图像的边缘,计算最大的半径
float mr = hypot(bounds.size.width, bounds.size.height) / 2.0;
// 获取图形绘制上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//线条宽度是20
CGContextSetLineWidth(context, 15);//线条粗细
//循环绘制圆
for (float r = mr-30; r > 0; r -= 10)
{
// 设置线条颜色
[[[UIColor alloc] initWithRed:(sin(r)+1.0)/2.0 green:(cos(r*127.0+2.0)+1.0)/2.0 blue:(cos(r)+1.0)/2.0 alpha:0.5] setStroke];
CGContextAddArc(context, center.x, center.y,r, 0.0, M_PI * 2.0, YES);//添加弧型路径
CGContextStrokePath(context);//绘制路径
}
}
@end
给大家推荐一篇文章,写的很不错:http://blog.csdn.net/rhljiayou/article/details/9919713
相关文章推荐
- ios学习第六天(三)自定义UIView下
- ios学习第六天(一)自定义UIView上
- ios学习第六天(四)美丽的自定义View
- iOS学习爬坑记录8:关于自定义Cell的一点认识
- [ios]iOS学习之UITableView(三):进阶篇索引,标记和自定义的table
- IOS学习——自定义UITableViewCell
- IOS开发学习:MKMapView自定义CalloutView
- ((ios开发学习笔记 十一))自定义TableViewCell 的方式实现自定义TableView(带源码)
- Android 自定义View学习(3)--仿IOS风格滑动按钮
- iOS学习之自定义弹出UIPickerView或UIDatePicker(动画效果)
- ios学习记录 day26 UI 4 代码优化(自定义视图)
- IOS学习笔记(4)——自定义相机的实现
- iOS菜鸟学习——通过nib文件自定义Cell
- IOS UIButton 自定义的补充学习
- UIview 学习与自定义--ios
- iOS学习笔记---c语言第六天
- ((ios开发学习笔记 十))代码实现自定义TableView
- iOS学习之自定义弹出UIPickerView或UIDatePicker(动画效果)
- IOS开发---菜鸟学习之路--(七)-自定义UITableViewCell
- iOS学习之自定义弹出UIPickerView或UIDatePicker(动画效果)