2011-05-31 29 views
8

我想創建一個與UIBackBarButtonItem類似的UIButton(帶有箭頭指向左側的導航堆棧,如果可能的話,我寧願這樣做而不必使用圖像,因爲按鈕會根據手機的方向有不同的尺寸。不使用圖像的自定義UIButton形狀

有沒有辦法來活躍在這個代碼影響?我的想法是使用按鈕的CALayer不知何故。

謝謝!

編輯

我正在嘗試使用@ Deepak的建議,但我遇到了一個問題。我希望按鈕的右側看起來像一個[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:4],左側看起來像一個箭頭。我試圖用addQuadCurveToPoint:controlPoint方法來做到這一點。

我正在使用矩形的拐角作爲控制點,但路徑不像我期望的那樣彎曲。它仍然像我只使用addLineToPoint:方法一樣受到限制。我的代碼如下。

float radius = 4.0; 
UIBezierPath *path = [UIBezierPath bezierPath]; 

CGPoint startPoint = CGPointMake(rect.size.width/5.0, 0); 
CGPoint pointBeforeTopCurve = CGPointMake(rect.size.width - radius, 0); 
CGPoint topRightCorner = CGPointMake(rect.size.width, 0); 
CGPoint pointAfterTopCurve = CGPointMake(rect.size.width, 0.0-radius); 

CGPoint pointBeforeBottomCurve = CGPointMake(rect.size.width, rect.size.height-radius); 
CGPoint bottomRightCorner = CGPointMake(rect.size.width, rect.size.height); 
CGPoint pointAfterBottomCurve = CGPointMake(rect.size.width - radius, rect.size.height); 

CGPoint pointBeforeArrow = CGPointMake(rect.size.width/5.0, rect.size.height); 
CGPoint arrowPoint = CGPointMake(0, rect.size.height/2.0); 


[path moveToPoint:pointBeforeTopCurve]; 
[path addQuadCurveToPoint:pointAfterTopCurve controlPoint:topRightCorner]; 

[path addLineToPoint:pointBeforeBottomCurve]; 
[path addQuadCurveToPoint:pointAfterBottomCurve controlPoint:bottomRightCorner]; 


[path addLineToPoint:pointBeforeArrow]; 
[path addLineToPoint:arrowPoint]; 
[path addLineToPoint:startPoint]; 
+0

選中此http://stackoverflow.com/questions/6184321/where-can-i-find-some-beautiful-iphone-buttons/6184393#6184393 – Jhaliya 2011-05-31 19:07:03

+0

它解決了什麼問題? – 2012-03-19 08:15:19

+0

你是什麼意思? – 2012-03-20 01:10:46

回答

7

您可以用Quartz來做到這一點。您需要子類UIButton並實施其drawRect:方法。你將不得不定義一個路徑並用漸變填充它。

您還必須實施hitTest:withEvent:,因爲它涉及非矩形形狀。

0

這解決了繪圖的困難,我

float radius = 10.0; 
UIBezierPath *path = [UIBezierPath bezierPath]; 

CGPoint startPoint = CGPointMake(rect.size.width/5.0, 0); 
CGPoint pointBeforeTopCurve = CGPointMake(rect.size.width - radius, 0); 
CGPoint topRightCorner = CGPointMake(rect.size.width, 0.0); 
CGPoint pointAfterTopCurve = CGPointMake(rect.size.width, radius); 

CGPoint pointBeforeBottomCurve = CGPointMake(rect.size.width, rect.size.height-radius); 
CGPoint bottomRightCorner = CGPointMake(rect.size.width, rect.size.height); 
CGPoint pointAfterBottomCurve = CGPointMake(rect.size.width - radius, rect.size.height); 

CGPoint pointBeforeArrow = CGPointMake(rect.size.width/5.0, rect.size.height); 
CGPoint arrowPoint = CGPointMake(0.0, rect.size.height/2.0); 


[path moveToPoint:pointBeforeTopCurve]; 
[path addQuadCurveToPoint:pointAfterTopCurve controlPoint:topRightCorner]; 

[path addLineToPoint:pointBeforeBottomCurve]; 
[path addQuadCurveToPoint:pointAfterBottomCurve controlPoint:bottomRightCorner]; 


[path addLineToPoint:pointBeforeArrow]; 
[path addLineToPoint:arrowPoint]; 
[path addLineToPoint:startPoint]; 


[path fill];