gpt4 book ai didi

ios - UIBezierPath 一起绘制矩形和圆弧

转载 作者:行者123 更新时间:2023-12-01 18:49:37 28 4
gpt4 key购买 nike

我想画一条这样的路径:enter image description here

这就是我写的:

CGFloat radius = 50;

UIBezierPath *path = [UIBezierPath bezierPath];

[path moveToPoint:CGPointMake(radius, CGRectGetMinY(rect))];

[path addLineToPoint:CGPointMake(CGRectGetMaxX(rect) - radius, CGRectGetMinY(rect))];
[path addLineToPoint:CGPointMake(CGRectGetMaxX(rect) -radius, CGRectGetMaxY(rect))];
[path addLineToPoint:CGPointMake(radius, CGRectGetMaxY(rect))];
[path closePath];
UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(radius, CGRectGetMinY(rect) + radius) radius:radius startAngle:0.5 * M_PI endAngle:1.5 *M_PI clockwise:YES];

UIBezierPath *path2 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(CGRectGetMaxX(rect) - radius, CGRectGetMaxY(rect) - radius) radius:radius startAngle:-0.5 * M_PI endAngle:0.5 *M_PI clockwise:YES];


[path1 appendPath:path];
[path1 appendPath:path2];

所以我得到了这样的结果 result

我怎样才能去除两条多余的线?谢谢

最佳答案

试试这个:

- (void)drawRect:(CGRect)rect{
CGContextRef context = UIGraphicsGetCurrentContext();

const CGFloat radius = 50;
const CGFloat lineWidth = 10;

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(rect, lineWidth / 2, lineWidth / 2)
cornerRadius:radius];

CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
path.lineWidth = lineWidth;
[path stroke];

CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor);
[path fill];
}

enter image description here

您可以像这样实现自定义 View :
@interface RoundedView: UIView

@property (nonatomic, strong) UIColor *strokeColor;
@property (nonatomic, strong) UIColor *fillColor;

@end

@implementation RoundedView

- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
}
return self;
}

-(void)drawRect:(CGRect)rect{
CGContextRef context = UIGraphicsGetCurrentContext();

const CGFloat radius = 50;
const CGFloat lineWidth = 10;

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(rect, lineWidth / 2, lineWidth / 2)
cornerRadius:radius];

CGContextSetStrokeColorWithColor(context, self.strokeColor.CGColor);
path.lineWidth = lineWidth;
[path stroke];

CGContextSetFillColorWithColor(context, self.fillColor.CGColor);
[path fill];
}

@end

然后使用它:
RoundedView *view = [[RoundedView alloc] initWithFrame:CGRectMake(10, 20, 200, 100)];
[self.view addSubview:view];

关于ios - UIBezierPath 一起绘制矩形和圆弧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32281721/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com