ios学习第六天(二)自定义UIView中

来源:互联网 发布:js history 清除 编辑:程序博客网 时间:2024/06/16 09:53

这次用自定义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





0 0