Creating animated progress bar in iOS

2020-07-28 07:22发布

问题:

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.

回答1:

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. :]

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);
}

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

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);
}


回答2:

idk about that link, but you can generate them from here http://preloaders.net/ that should give you a good base to make your own