ios - 如何绘制三角形的UIButton

标签 ios uibutton core-graphics cgcontext

我真的很喜欢 IFTTT 右下角按钮,如图(右下)所示。

enter image description here

我尝试使用 CGContextUIBezierPath 来匹配效果,但我根本不知道如何正确实现这一点。有人对如何实现这一点有一些想法/代码吗?

谢谢!

最佳答案

首先使用CAShapeLayer创建一个 mask

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

然后设置按钮的掩码

yourbutton.layer.mask = shapeLayer
yourbutton.masksToBounds = YES;

View 示例

  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;

和屏幕截图:

enter image description here

关于ios - 如何绘制三角形的UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30315337/

相关文章:

ios - 点击时选择/取消选择 UIButton

ios - 在 iOS 中创建六边形 ImageView 形状

iphone - NSPredicate 仅过滤日期字段的 "year"部分

ios - 如何用代码更新 info.plist?

ios - 在容器 View Controller 中调整 View 大小

ios - 隐藏 ViewController 后实例化按钮不起作用

ios - UISegmentedControl 操作中的 UIButton

ios - UIButton 不可点击

objective-c - 如何在 Objective-C 中使用 UIImage 作为颜色的 mask

ios - 如何更快地将 View 渲染成图像?