2015-05-19 181 views
2

我真的挖了IFTTT右下角的按鈕,如圖所示(右下角)。如何繪製一個三角形UIButton

enter image description here

我試着用CGContextUIBezierPath玩弄相匹配的效果,但我根本不知道夠得到這個權利。有沒有人有關於如何做到這一點的想法/代碼?

謝謝!

回答

4

首先使用CAShapeLayer創建蒙

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; 

和屏幕截圖:

enter image description here

+2

請注意,觸摸跟蹤仍然會像按鈕是方形那樣工作,這可能無關緊要,但值得注意的是。 –

0

製作一個方形按鈕,在其中放置一個三角形的圖像。這不會工作嗎?爲了使錯覺更好,請爲onclick()事件和ontouch()繪製單獨的圖像。禁用默認按鈕按下並按鈕單擊動畫使其看起來真實。

+0

就我個人而言,我不是一個iOS程序員,所以我不知道核心API,但是當我想要一個具有不同形狀的按鈕時,這就是我通常所做的。 –