如何绘制一个三角形的UIButton [英] How to draw a triangle shaped UIButton
本文介绍了如何绘制一个三角形的UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我真的挖了IFTTT右下角的按钮,如图(右下图)所示。
I really dig the IFTTT bottom right corner button, as shown in the image (bottom right).
我尝试过使用 CGContext
和 UIBezierPath
来匹配效果,但是我只是不知道该怎么做。有人对如何实现此目标有任何想法/代码吗?
I tried playing around with CGContext
and UIBezierPath
to match the effect, but i simply don't know enough to get this right. Does anyone have some thoughts/code on how to make this happen?
谢谢!
推荐答案
首先使用CAShapeLayer创建遮罩
First use a CAShapeLayer to create a 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;
视图示例
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;
和屏幕截图:
这篇关于如何绘制一个三角形的UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文