gpt4 book ai didi

ios - UIView 底边模式

转载 作者:行者123 更新时间:2023-11-28 19:31:18 26 4
gpt4 key购买 nike

我想在我的 UIView 的底部边缘添加一个鲨鱼齿/三角形/尖头图案。像这样:

enter image description here

现在只用代码就可以做到这一点吗?以及如何?

最佳答案

这应该让你上路:

- (void)viewDidLoad {
[super viewDidLoad];

UIBezierPath *pth = [UIBezierPath bezierPath];

CGFloat w = 240.0;
CGFloat h = 200.0;

CGPoint p = CGPointZero;

// start at top-left
[pth moveToPoint:p];

// line to top-right
p.x = w;
[pth addLineToPoint:p];

// line to bottom-right
p.y = h;
[pth addLineToPoint:p];

// line to 40 left, 40 up
p.x -= 40;
p.y -= 40;
[pth addLineToPoint:p];

// line to 40 left, 40 down (back to bottom)
p.x -= 40;
p.y += 40;
[pth addLineToPoint:p];

// line to 40 left, 40 up
p.x -= 40;
p.y -= 40;
[pth addLineToPoint:p];

// line to 40 left, 40 down (back to bottom)
p.x -= 40;
p.y += 40;
[pth addLineToPoint:p];

// line to 40 left, 40 up
p.x -= 40;
p.y -= 40;
[pth addLineToPoint:p];

// line to 40 left, 40 down (back to bottom)
p.x -= 40;
p.y += 40;
[pth addLineToPoint:p];

// line to starting point - top-left
[pth closePath];

// 240 x 200 rectangle at 40,40
CGRect r = CGRectMake(40, 40, w, h);

// create a UIView
UIView *v = [[UIView alloc] initWithFrame:r];
v.backgroundColor = [UIColor redColor];

// create a CAShapeLayer
CAShapeLayer *maskShape = [CAShapeLayer layer];

// add the path to the CAShapeLayer
maskShape.path = pth.CGPath;

// set the view's layer mask to the CAShapeLayer
v.layer.mask = maskShape;

// add the masked subview to the view
[self.view addSubview:v];

}

关于ios - UIView 底边模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44332011/

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