2015-04-08 80 views
1

如何在自定義形狀的iOS中創建2個按鈕。我需要繪製兩個按鈕,例如矩形的對角線。左側是一個按鈕,右側是另一個按鈕。我試過使用Bezier路徑,但是如何使它們適應所有設備?具有自定義形狀的iOS UIButton

這裏是我的代碼,我試過一個按鈕

UIBezierPath* bezierPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 138, 118)]; 
[UIColor.blackColor setStroke]; 
bezierPath.lineWidth = 20; 
[bezierPath stroke]; 


CAShapeLayer *shapeLayer = [CAShapeLayer layer]; 
shapeLayer.frame = self.Btn.bounds; 
shapeLayer.path = bezierPath.CGPath; 
shapeLayer.fillColor = [UIColor clearColor].CGColor; 
shapeLayer.strokeColor = [UIColor blackColor].CGColor; 
shapeLayer.lineWidth = 120; 
self.Btn.layer.mask = shapeLayer; 
+2

你能舉一個你想要的圖片的例子嗎? – Esqarrouth

+0

看看[OBShapedButton](https://github.com/ole/OBShapedButton)。有了這個,你可以重疊2個'UIButton's給他們一個單一按鈕的外觀,並且只有當你點擊圖像上的區域時,任何按鈕纔會響應。但是,您需要爲您的按鈕定製圖像。 – n00bProgrammer

回答

1

着,只是你一個按鈕,

子類,並在則hitTest以CGPoint確定你應該設置突出了形狀/選擇/ etc