我想在我的UIView
的底部边缘添加一个鲨鱼齿/三角形/尖头的图案。像这样:
现在仅用代码就能做到吗?如何?
最佳答案
这应该可以帮助您:
- (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/