IOS drawRect 重绘

本文介绍如何使用iOS的UIKit框架创建自定义UIView来绘制不同类型的圆形与弧形,包括根据三个点绘制的圆、根据中心点绘制的大圆弧以及实心小圆点。

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


#import <UIKit/UIKit.h>

@interface BezierView : UIView
//根据三个点画圆
-(instancetype)initWithFrame:(CGRect)frameRect P1:(CGPoint)P1 P2:(CGPoint)P2 P3:(CGPoint)P3 color:(UIColor*)color;

@end



@interface BezierViewForPointBigArc : UIView
//根据中心点画圆
-(instancetype)initWithFrame:(CGRect)frameRect P:(CGPoint)P R:(float)R color:(UIColor*)color;
-(void)change:(CGPoint)P R:(float)R color:(UIColor*)color;
@end


@interface BezierViewForPointSmallArc : UIView
//根据中心点画实心圆点
-(instancetype)initWithFrame:(CGRect)frameRect P:(CGPoint)P R:(float)R color:(UIColor*)color;
@end

#pragma mark -
#pragma mark -
@interface BezierViewForPointBigArc(){
    //三个分割点
    CGPoint sP;
    float sR;
    
    //颜色
    UIColor *drawColor;
    
}
@end

@implementation BezierViewForPointBigArc


-(instancetype)initWithFrame:(CGRect)frameRect P:(CGPoint)P R:(float)R color:(UIColor*)color
{
    if (self==[super initWithFrame:frameRect]) {
        self.backgroundColor = [UIColor clearColor];
        sP = P;
        sR = R;
        drawColor = color;
        
    }
    return self;
}

-(void)change:(CGPoint)P R:(float)R color:(UIColor*)color
{

    sP = P;
    sR = R;
    drawColor = color;
    [self setNeedsDisplay];

}

- (void)drawRect:(CGRect)rect
{
    UIBezierPath* p = [UIBezierPath bezierPathWithArcCenter:CGPointMake(sP.x, sP.y) radius:sR startAngle:0 endAngle:DEGREES_TO_RADIANS(360) clockwise:YES];
    
    [drawColor setStroke];
    [p stroke];
}

@end




#pragma mark -
#pragma mark -
@interface BezierViewForPointSmallArc(){
    //三个分割点
    CGPoint sP;
    float sR;
    
    //颜色
    UIColor *drawColor;
    
}
@end

@implementation BezierViewForPointSmallArc


-(instancetype)initWithFrame:(CGRect)frameRect P:(CGPoint)P R:(float)R color:(UIColor*)color
{
    if (self==[super initWithFrame:frameRect]) {
        self.backgroundColor = [UIColor clearColor];
        sP = P;
        sR = R;
        drawColor = color;
        
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    [drawColor set];
    CGContextAddArc(context, sP.x, sP.y, sR, 0, 2*M_PI, 1);
    CGContextDrawPath(context, kCGPathFillStroke);
    
    //用UIBezierPath 默认已经获取上下文
    //UIBezierPath* p = [UIBezierPath bezierPathWithArcCenter:CGPointMake(x, y) radius:b_line startAngle:0 endAngle:DEGREES_TO_RADIANS(360) clockwise:YES];
//    
//    [colorAry[i] setStroke];
//    [p stroke];
}


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值