如何绘制三角形UIButton

Jef*_*eff 2 core-graphics uibutton cgcontext ios

我真的挖了IFTTT右下角按钮,如图所示(右下角).

在此输入图像描述

我试着玩CGContextUIBezierPath匹配效果,但我只是不知道得到这个权利.有没有人对如何实现这一点有一些想法/代码?

谢谢!

Leo*_*Leo 5

首先使用CAShapeLayer创建蒙版

CAShapeLayer * shapeLayer = [CAShapeLayer layer];
//Use these to create path
CGMutablePathRef path = CGPathCreateMutable();
// CGPathMoveToPoint
// CGPathAddLines
shapeLayer.path = path;
Run Code Online (Sandbox Code Playgroud)

然后设置按钮的遮罩

yourbutton.layer.mask = shapeLayer
yourbutton.masksToBounds = YES;
Run Code Online (Sandbox Code Playgroud)

视图示例

  CAShapeLayer * shapeLayer = [CAShapeLayer layer];
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, nil, 0, CGRectGetHeight(self.testview.frame));
CGPathAddLineToPoint(path, nil, CGRectGetWidth(self.testview.frame), 0);
CGPathAddLineToPoint(path, nil, CGRectGetWidth(self.testview.frame), CGRectGetHeight(self.testview.frame));
CGPathCloseSubpath(path);
shapeLayer.path = path;
self.testview.layer.masksToBounds = YES;
self.testview.layer.mask = shapeLayer;
Run Code Online (Sandbox Code Playgroud)

和屏幕截图:

在此输入图像描述

  • 请注意,触摸跟踪仍然会像按钮是正方形一样,这可能无关紧要,但值得注意. (2认同)