您的位置:首页 > 产品设计 > UI/UE

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: