简体   繁体   English

iPhone中的弧/圆动画

[英]Arc / Circle animation in iphone

How would I animate a circle on the iPhone so that the arc starts at "0 degrees" and ends at "360 degrees"? 我将如何在iPhone上为圆形设置动画,以使圆弧从“ 0度”开始到“ 360度”结束?

Advance Thanks, Sat 提前谢谢,星期六

You need to read the Quartz 2D Programming Guide's section on arcs . 您需要阅读Quartz 2D编程指南中关于arcs的部分 (I am assuming you are creating an app with the Cocoa Touch API, not a web app.) You also need to know how to set up a custom animation. (我假设您正在使用Cocoa Touch API创建应用程序,而不是Web应用程序。)您还需要知道如何设置自定义动画。 You will have to create a custom UIView or CALayer to do the drawing, and create a property (arc degree) that can be animated with a CAAnimation object. 您将必须创建一个自定义UIView或CALayer来进行绘制,并创建可以使用CAAnimation对象进行动画处理的属性(弧度)。 Alternatively, you can control the animation using an NSTimer instead. 另外,您也可以使用NSTimer来控制动画。 You pretty much have to have a grasp of these classes (and others) to pull this off. 您几乎必须掌握这些类(和其他类)才能实现这一目标。

you should read the documentation that Felixyz provided and if you want an example of how to animate the circle have a look over the MBProgressHUD at this link link text . 您应该阅读Felixyz提供的文档,如果想要有关如何为圆制作动画的示例, MBProgressHUD在此链接链接文本中查看MBProgressHUD The loader has two modes one with a UIViewActivityIndicator and a progress indicator (a filling circle that it is animated from 0 to 360 degress) i think the last mode is what you want. 加载程序有两种模式,一种带有UIViewActivityIndicator和进度指示器(从0到360度UIViewActivityIndicator的填充圆,它是动画的),我认为最后一种模式就是您想要的。

the fallowing code is from copy/paste from that implementation that animates the circle: 轻松的代码来自该实现动画的圆圈的复制/粘贴:

- (void)drawRect:(CGRect)rect {
    CGRect allRect = self.bounds;
    CGRect circleRect = CGRectMake(allRect.origin.x + 2, allRect.origin.y + 2, 
                                   allRect.size.width - 4, allRect.size.height - 4);
    CGContextRef context = UIGraphicsGetCurrentContext();
    // Draw background
    CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0); // white
    CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 0.1); // translucent white
    CGContextSetLineWidth(context, 2.0);
    CGContextFillEllipseInRect(context, circleRect);
    CGContextStrokeEllipseInRect(context, circleRect);
    // Draw progress
    float x = (allRect.size.width / 2);
    float y = (allRect.size.height / 2);
    CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0); // white
    CGContextMoveToPoint(context, x, y);
    CGContextAddArc(context, x, y, (allRect.size.width - 4) / 2, -(PI / 2), 
                                   (self.progress * 2 * PI) - PI / 2, 0);
    CGContextClosePath(context);    CGContextFillPath(context);
}

but read the documentation first! 但请先阅读文档! hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM