简体   繁体   English

动画UICircularProgressView

[英]Animating UICircularProgressView

i am using UICircularProgressView and have changed it to my liking. 我正在使用UICircularProgressView并将其更改为我喜欢的。 the initialization is clear but i'll post it anyways: 初始化很清楚,但我还是会发布它:

UICircularProgressView *wheelProgress = [[UICircularProgressView alloc]initWithFrame:CGRectMake(someFrame)];

then i am setting the right progress CGFloat between 0-1 然后我将正确的进度CGFloat between 0-1设置为CGFloat between 0-1

CGFloat progress = .3;   // example
[self.wheelProgress setProgress:progress];

This works just fine, and looks good, but i would really like to somehow animate it. 这工作得很好,并且看起来不错,但是我真的想以某种方式对其进行动画处理。 I havent worked with animations so far, so my first approach was something like 到目前为止,我还没有制作动画,所以我的第一种方法是

for(tempProgress =! progress){
  incrementing tempProgress
  setting tempProgres to progressView
}

this of course is massively ugly and blocks everything else going on. 这当然很丑陋,并且阻止了其他所有事情。

what i would like to achieve is a linear animation of the progress from 0 to its final value. 我想实现的是从0到最终值的线性动画。

i have looked at: Animation tutorial 我看过: 动画教程

and came up with something like this: 并想到了这样的事情:

CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration: 1];
wheelProgress.progress = (progress) ? progress : 0.0;
[UIView commitAnimations];

but somehow this does not work.. 但这不起作用

any ideas what i need to fix here? 任何想法我需要在这里解决?

thanks in advance sebastian 预先感谢塞巴斯蒂安

The progress view you are using Is not set up to have the progress as a animatable property. 您正在使用的进度视图未设置为将进度作为可设置动画的属性。 To do so is quite complex and I'm not sure it is what you are looking for. 这样做非常复杂,我不确定这是否是您要寻找的。

If it is, I think you would need to do the following (I haven't done this myself, this is just based on documentation and books that I have read): 如果是这样,我认为您需要执行以下操作(我自己还没有做过,这只是基于我已阅读的文档和书籍):

  1. Rewrite the view to use CALayer s and drawInContext instead of drawRect 重写视图以使用CALayerdrawInContext而不是drawRect
  2. Make progress a property on the layer (use @dynamic instead of @synthesize 使progress成为图层上的属性(使用@dynamic而不是@synthesize
  3. Override actionForKey: on the layer subclass and return a CABasicAnimation for the key @"progress" 覆盖layer子类上的actionForKey:并为键@"progress"返回CABasicAnimation

Examples of this can be found in a few SO question / answers: here and here should get you started or at least used to the search terms that can be used. 可以在一些SO问题中找到此类示例: 在这里这里应该可以帮助您入门,或者至少习惯了可以使用的搜索词。 This looks like a good tutorial for drawing animated slices in a pie chart using layers, which may be closer to what you want than starting with the UICircularProgressView. 看起来像是一个很好的教程,用于使用图层在饼图中绘制动画切片,与从UICircularProgressView开始相比,这可能更接近您想要的内容。

Alternatively, you could implement this instead using an NSTimer which increases the progress value every 1/60th of a second, until it hits your desired value. 或者,您可以改为使用NSTimer来实现此目的,该方法每隔1/60秒增加一次进度值,直到达到所需值为止。

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

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