簡體   English   中英

iOS:如何使用NSTimer逐步繪制圓圈

[英]iOS: How to draw a circle step by step with NSTimer

我想逐步繪制一個圓圈(只有圓圈的邊框)(就像動畫計時器一樣)。 1旋轉等於1天(24小時)。 我真的不知道該怎么辦。

我做的步驟

1)我已經嘗試過https://github.com/danielamitay/DACircularProgress (這是太寬泛的進展)

2)我試圖繪制一個有很多弧的圓。

你能給我一些代碼嗎? 我真的很困惑。 提前致謝。

編輯

我想使用NSTimer,因為我有一個允許用戶停止繪制圓形的按鈕。 如果用戶再次觸摸按鈕,則必須繼續繪制。

我要做的是創建一個圓形路徑,並將其與CAShapeLayer一起使用,並為strokeEnd設置動畫,類似於我在此答案中所做的操作

它看起來像這樣(但我沒有運行此代碼,因此可能存在拼寫錯誤和其他錯誤):

UIBezierPath *circle = [UIBezierPath bezierPathWithArcCenter:center
                                                      radius:radius
                                                  startAngle:0
                                                    endAngle:2.0*M_PI
                                                   clockwise:YES];

CAShapeLayer *circleLayer = [CAShapeLayer layer];
circleLayer.bounds = CGRectMake(0, 0, 2.0*radius, 2.0*radius);
circleLayer.path   = circle.CGPath;
circleLayer.strokeColor = [UIColor orangeColor].CGColor;
circleLayer.lineWidth   = 3.0; // your line width

CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
drawAnimation.duration = 10.0; // your duration

// Animate from no part of the stroke being drawn to the entire stroke being drawn
drawAnimation.fromValue = @0;
drawAnimation.toValue   = @1;

請注意,路徑和形狀圖層都有一個位置,因此應該相對於形狀圖層框架的原點定義圓形路徑。 首先定義形狀圖層然后在其邊界內創建一個橢圓形可能更清晰:

CAShapeLayer *circleLayer = [CAShapeLayer layer];
circleLayer.bounds = CGRectMake(0, 0, 2.0*radius, 2.0*radius);
circleLayer.position = center; // Set center of the circle

// Create a circle inside of the shape layers bounds
UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:circleLayer.bounds];
circleLayer.path     = circle.CGPath;

// Same appearance configuration as before
circleLayer.strokeColor = [UIColor orangeColor].CGColor;
circleLayer.lineWidth   = 3.0; // your line width

如果DACircleProgress 在此處輸入鏈接描述,否則對您有用,看起來您可以輕松設置線條粗細。

與具有簡單的lineWidth類型屬性相反,該庫的作者似乎根據與圓的半徑的比率來設置厚度。 這作為該類的thicknessRatio屬性存在。 例如,如果你的半徑是40,那么將thicknessRatio設置為0.025應該產生1的線寬。這個庫看起來很簡單並且經過深思熟慮 - 考慮使用它,或者從中學習。

默認設置為0.3,因此半徑為40的圓的進度線厚度為12.這可能就是您所看到的。

祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM