2013-03-17 69 views
2

我想顯示一個複雜的UI元素作爲有點兒與扇區甜甜圈,因此每個扇區可以被表示爲單個的UIView。 有什麼辦法可以將矩形UIView轉換成適合圓環輪廓的圓弧?或者那是不可能的? 我是一個完全的iOS noob,如果你指出我正確的文檔,我會非常感激。顯示的UIView爲弧形

+0

在矩形UIView中繪製弧線會好嗎?這會更容易。 – 2013-03-17 20:09:00

+0

不好。我想實現一個新的UI元素,它可以類似於UITableViewController,而是一個矩形單元格,應該有一種方法可以將任何矩形的UIView後裔插入到弧形單元格中。帶觸摸事件的 ,旋轉中心 – 2013-03-17 21:28:52

+0

對不起...沒有真的跟在後面? – 2013-03-17 22:26:52

回答

0

UIView的總是長方形的,除非你申請的仿射變換到它(但我仍然不認爲你可以得到一個圓弧狀)。我認爲你最好的選擇是通過繼承UIView並實現drawRect或使用CAShapeLayer來繪製UIView中的弧。

+0

是的,我想是這樣UIViewAnimation轉換,以適應矩形的UIView到的所有者繪製弧形扇區甜甜圈視圖,是真的嗎? – 2013-03-17 21:40:19

0

不知道更多關於什麼你試圖做的,我要出去肢體和建議你使用CGDrawing方法繪製你的甜甜圈段在一個矩形視圖。

要做到這一點,你會使用,特別是以下幾點:

CGPathAddArc() 
CGPathAddLineToPoint() 
CGPathMoveToPoint() 

該代碼繪製充滿弧線:

-(void)drawFilledArc:(CGContextRef)context innerRadius:(CGFloat)rIn outterRadius (CGFloat)rOut 
    startAngle:(double)startAng endAngle:(double)endAng drawColor:(UIColor *)color 
{ 
    float x1, y1; 
    CGFloat centerX = originX; 
    CGFloat centerY = originY; 

    CGContextSaveGState(context); 
    CGMutablePathRef path = CGPathCreateMutable(); 

    CGContextSetLineWidth(context, lineWidth); 

    getChartWheelCoord(centerX, centerY, startAng, rIn, &x1, &y1); 
    CGPathMoveToPoint(path, NULL, x1, y1); 

    getChartWheelCoord(centerX, centerY, startAng, rOut, &x1, &y1); 
    CGPathAddLineToPoint(path, NULL, x1, y1); 

    CGPathAddArc(path, NULL, centerX, centerY, rOut, radians(-startAng), radians(-endAng), YES); 

    getChartWheelCoord(centerX, centerY, endAng, rIn, &x1, &y1); 
    CGPathAddLineToPoint(path, NULL, x1, y1); 

    CGPathAddArc(path, NULL, centerX, centerY, rIn, radians(-endAng), radians(-startAng), NO); 
    CGPathCloseSubpath(path); 

    CGContextSetFillColorWithColor(context, color.CGColor); 
    CGContextAddPath(context, path); 
    CGContextDrawPath(context, kCGPathFillStroke); 

    CGPathRelease(path); 
    CGContextRestoreGState(context); 
} 
+0

並在第一評論 – 2013-03-17 21:37:15

0

那麼你可以達到你在以下方面想要的東西。

1如果您的UI元素不需要負責點擊或觸摸事件,那麼您可以繪製它 使用核心圖形。

2如果您的用戶界面元素必須響應用戶交互事件,那麼您可以創建一個UIView 並添加儘可能多的自定義按鈕作爲UIview的子視圖以及弧形或扇形圖像(圖像對於自定義按鈕)。

+0

一些澄清我該怎麼辦,第二個,但與任何子視圖(UIView的子孫)? – 2013-03-17 21:34:49

+0

能否發米d圖與細節了可能要給你一個示例程序 – 2013-03-18 07:06:40

+0

好吧,我想是這樣的:http://f1.s.qip.ru/1q1ISx8F.png(不是設計師,對不起油漆:) – 2013-03-19 13:47:05