【问题标题】:iPhone: Drawing a curved line until it becomes a circle animationiPhone:画一条曲线直到变成圆形动画
【发布时间】:2011-11-04 04:46:27
【问题描述】:

我想画一条曲线,直到它完全旋转并连接成一个完整的圆,只是圆的轮廓,没有填充。这必须在几秒钟内进行动画处理。

谁能指出我正确的方向?我已经向 similar question 询问过这个问题,但我的措辞不正确,所以每个人都很难理解我的意思,因此它在问题的海洋中迷失了。

非常感谢您的帮助

[编辑]

我目前正在对 UIView 进行子类化并覆盖 drawRect。我找到了绘制实心圆的代码,但我只需要笔画。

- (void)drawRect:(CGRect)rect {
// Drawing code

CGRect allRect = self.bounds;
CGRect circleRect = CGRectInset(allRect, 2.0f, 2.0f);

CGContextRef context = UIGraphicsGetCurrentContext();

// Draw background
CGContextSetRGBStrokeColor(context, self.strokeValueRed, self.strokeValueGreen, self.strokeValueBlue, self.strokeValueAlpha); // white
CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 0.1f); // translucent white
CGContextSetLineWidth(context, self.lineWidth);
CGContextFillEllipseInRect(context, circleRect);
CGContextStrokeEllipseInRect(context, circleRect);

// Draw progress
CGPoint center = CGPointMake(allRect.size.width / 2, allRect.size.height / 2);
CGFloat radius = (allRect.size.width - 4) / 2;
CGFloat startAngle = - ((float)M_PI / 2); // 90 degrees
CGFloat endAngle = (self.progress * 2 * (float)M_PI) + startAngle;
CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f); // white
CGContextMoveToPoint(context, center.x, center.y);
CGContextAddArc(context, center.x, center.y, radius, startAngle, endAngle, 0);
CGContextClosePath(context);
CGContextFillPath(context);
}

[编辑#2]

我更改了代码以删除所有填充引用,但现在它没有绘制任何东西:(有什么想法吗?

- (void)drawRect:(CGRect)rect
{
// Drawing code

CGRect allRect = self.bounds;
CGContextRef context = UIGraphicsGetCurrentContext();

// Draw background
CGContextSetRGBStrokeColor(context, self.strokeValueRed, self.strokeValueGreen, self.strokeValueBlue, self.strokeValueAlpha); // white
CGContextSetLineWidth(context, 5);

// Draw progress
CGPoint center = CGPointMake(allRect.size.width / 2, allRect.size.height / 2);
CGFloat radius = (allRect.size.width - 4) / 2;
CGFloat startAngle = - ((float)M_PI / 2); // 90 degrees
CGFloat endAngle = (self.progress * 2 * (float)M_PI) + startAngle;
CGContextAddArc(context, center.x, center.y, radius, startAngle, endAngle, 0);
CGContextStrokePath(context);
}

[编辑 #3] 解决方案

是的,我觉得自己像个正确的白痴!问题是笔画颜色值没有初始化,这意味着正在绘制线条但显然看不到它!

【问题讨论】:

    标签: iphone core-animation core-graphics


    【解决方案1】:

    这与我对您的另一个问题 here 给出的答案相同。

    您真正应该做的是为 CAShapeLayer 的笔划设置动画,其中路径是一个圆。这将使用 Core Animation 加速,并且比在 -drawRect: 中绘制圆的一部分更容易。

    下面的代码将在屏幕中心创建一个圆形图层,并按顺时针方向对其笔划进行动画处理,使其看起来好像正在绘制。你当然可以使用任何你喜欢的形状。 (您可以阅读 Ole Begemanns 博客上的 this article 了解更多关于如何为形状图层的笔划设置动画的信息。)

    注意: stoke 属性与图层上的边框属性不同。要更改笔画的宽度,您应该使用“lineWidth”而不是“borderWitdh”等。

    // Set up the shape of the circle
    int radius = 100;
    CAShapeLayer *circle = [CAShapeLayer layer];
    // Make a circular shape
    circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius) 
                                             cornerRadius:radius].CGPath;
    // Center the shape in self.view
    circle.position = CGPointMake(CGRectGetMidX(self.view.frame)-radius, 
                                  CGRectGetMidY(self.view.frame)-radius);
    
    // Configure the apperence of the circle
    circle.fillColor = [UIColor clearColor].CGColor;
    circle.strokeColor = [UIColor blackColor].CGColor;
    circle.lineWidth = 5;
    
    // Add to parent layer
    [self.view.layer addSublayer:circle];
    
    // Configure animation
    CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    drawAnimation.duration            = 10.0; // "animate over 10 seconds or so.."
    drawAnimation.repeatCount         = 1.0;  // Animate only once..
    
    // Animate from no part of the stroke being drawn to the entire stroke being drawn
    drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    drawAnimation.toValue   = [NSNumber numberWithFloat:1.0f];
    
    // Experiment with timing to get the appearence to look the way you want
    drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    
    // Add the animation to the circle
    [circle addAnimation:drawAnimation forKey:@"drawCircleAnimation"];
    

    【讨论】:

    • 嗨,大卫,感谢您的帮助。我将您的解决方案设置为我上一个问题的答案。很高兴看到有人理解我的坏问题描述:)
    • removedOnCompletion 应该等于 YES 这里。 strokeEnd 属性默认已经是 1.0,一旦完成就不需要在屏幕上保留动画,它只会浪费电池/性能。否则最好的解决方案!
    • @Drmorgan 是的,没错。这是一个旧答案,尚未更新以反映最佳实践。它链接的答案不久前更新了,现在这个也更新了。
    【解决方案2】:

    取出所有说填充的东西。

    把末尾的CGContextFillPath改成CGContextStrokePath。

    取出接近尾声的 CGContextMoveToPoint 和 CGContextClosePath。这些只是勾勒出楔形的直边。

    您可能需要更改 CGContextMoveToPoint 以移动到圆弧的起点而不是中心,而不是将其移出。

    【讨论】:

      【解决方案3】:

      一个非常直接的方法是继承 UIView 并绘制一个可变长度的弧。然后您可以使用计时器定期更新旋转角度

      【讨论】:

      • 嘿@Eytan 我做得很好,但我只是不知道如何在不绘制实心圆的情况下单独绘制笔触。我已经修改了上面的帖子以显示我的代码。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多