在iOS中创建动画进度条 [英] Creating animated progress bar in iOS

查看:125
本文介绍了在iOS中创建动画进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为iPad应用程序制作自定义动画条形图(即,条形高度增加到激活时设置级别)。我对iOS开发很陌生,我只想获得有关如何处理此任务的反馈。

I am trying to make a custom animated bar graph for an iPad application (i.e., bar height increases to set level when activated). I am quite new to iOS development and I just want to get feedback on how to approach this task.

我正试图在这个条目,我想知道我是否从这一点开始是对的。

I am trying to play around with the answer in this entry and I want to know if it's right that I'm starting from this point.

推荐答案

如果你只想要一个实心条,你可以创建一个你需要的尺寸和位置的UIView,设置它背景颜色,并将其添加到您的视图中。这是一个不错的编码,使用UIView绘制实体矩形并不羞耻。 :]

If you just want a solid bar, you can create a UIView of the size and placement that you need, set its background color, and add it to your view. This is decent coding, no shame in using a UIView to draw solid rectangles. :]

对于更复杂的图形,您可能想要创建UIView的自定义子类并覆盖其drawRect消息以执行一些自定义绘制。例如:

For more complicated graphics, you might want to create a custom subclass of UIView and override its drawRect message to do some custom drawing. For example:

- (void)drawRect:(CGRect)rect
{
  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextSetLineWidth(context, 4.0);
  CGContextSetRGBStrokeColor(context, 1.0, 1.0, 0, 1.0); // opaque yellow
  CGContextMoveToPoint(context, x1, y1); // for suitable definition of x1,y1, etc
  CGContextAddLineToPoint(context, x2, y2);
  CGContextStrokePath(context);
}

或其他任何CGContext *您可能想要做的图纸(例如馅饼)图表,折线图等。)

or whatever other CGContext* sort of drawing you might want to do (e.g. pie charts, line charts, etc).

要通过添加带背景颜色的UIView来创建一个条形图动画,每当动画开始:

To animate a bar that you create by adding a UIView with a background color, stick the following whenever the animation starts:

timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];
self.startTime = [NSDate date];

然后添加以下消息(注意:栏会向上增长)。

and then add the following message (note: the bar will grow upwards).

- (void) onTimer:(NSTimer*)firedTimer
{
  float time = [self.startTime timeIntervalSinceNow] * -1;
  if (time>kMaxTime)
  {
    [timer invalidate];
    timer = nil;
    time = kMaxTime;
  }
  int size = time * kPixelsPerSecond;
  myBar.frame = CGRectMake(x, y - size, width, size);
}

这篇关于在iOS中创建动画进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆