gpt4 book ai didi

ios - 创建带圆角的 View (四分之一圆)

转载 作者:行者123 更新时间:2023-11-29 02:34:46 25 4
gpt4 key购买 nike

我想创建一个如下所示的 View

enter image description here

为了实现这一点,我尝试了以下方法:

UIView *myView = [[UIView alloc] init];
imgView.frame = CGRectMake(50, 150, 200, 200);
[self.view addSubview:imgView];

UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:myView.bounds
byRoundingCorners:UIRectCornerTopRight
cornerRadii:CGSizeMake(50.0, 50.0)];

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = myView.bounds;
maskLayer.path = maskPath.CGPath;
myView.layer.mask = maskLayer;

但这对我来说没有用。有人可以帮我吗?

最佳答案

伪代码:

    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(50, 150, 200, 200)];
myView.backgroundColor = [UIColor blueColor];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:myView.bounds
byRoundingCorners:UIRectCornerAllCorners
cornerRadii:CGSizeMake(CGRectGetWidth(myView.frame), CGRectGetHeight(myView.frame))];

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = myView.bounds;
maskLayer.path = maskPath.CGPath;
myView.layer.mask = maskLayer;


CGRect frame = myView.frame;
CGRect remainder, slice;

CGRectDivide(frame, &slice, &remainder, CGRectGetHeight(frame)/2, CGRectMaxYEdge);

NSLog(@"%@",NSStringFromCGRect(frame));
NSLog(@"%@",NSStringFromCGRect(remainder));
NSLog(@"%@",NSStringFromCGRect(slice));

CGRect remainder2, slice2;
CGRectDivide(remainder, &slice2, &remainder2, CGRectGetWidth(remainder)/2, CGRectMaxXEdge);

NSLog(@"%@",NSStringFromCGRect(remainder));
NSLog(@"%@",NSStringFromCGRect(remainder2));
NSLog(@"%@",NSStringFromCGRect(slice2));

myView.frame = remainder2;
[self.view addSubview:myView];

enter image description here

希望你的回答很清楚..😁

关于ios - 创建带圆角的 View (四分之一圆),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26483786/

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