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

本文介绍如何使用自定义View创建动态变化的圆形视图,并通过代码实现不同颜色及透明背景的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这次用自定义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.youkuaiyun.com/rhljiayou/article/details/9919713





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值