【问题标题】:How can I Achieve Semi Circle progress bar?如何实现半圆进度条?
【发布时间】:2014-03-28 09:48:15
【问题描述】:

任何人都可以帮助我吗?给我一些想法来实现这一点:)

【问题讨论】:

标签: ios objective-c progress-bar


【解决方案1】:

在 .h 文件中声明一个 UIView 类

 CGFloat startAngle;
    CGFloat endAngle;
    @property(assign) int  percent;

替换.m类中的initWithFrame和drawRect方法

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];

        // Determine our start and stop angles for the arc (in radians)
        startAngle = M_PI * 1;
        endAngle = startAngle + (M_PI * 2);    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    NSString* textContent = [NSString stringWithFormat:@"%d", percent];

    UIBezierPath* bezierPath = [UIBezierPath bezierPath];

    // Create our arc, with the correct angles
    [bezierPath addArcWithCenter:CGPointMake(rect.size.width / 2, rect.size.height / 2)
                          radius:130
                      startAngle:startAngle
                        endAngle:(endAngle - startAngle) * (percent / 100.0) + startAngle
                       clockwise:YES];

    // Set the display for the path, and stroke it
    bezierPath.lineWidth = 20;
    [[UIColor redColor] setStroke];
    [bezierPath stroke];

    // Text Drawing
    CGRect textRect = CGRectMake((rect.size.width / 2.0) - 71/2.0, (rect.size.height / 2.0) - 45/2.0, 71, 45);
    [[UIColor blackColor] setFill];
    [textContent drawInRect: textRect withFont: [UIFont fontWithName: @"Helvetica-Bold" size: 42.5] lineBreakMode: NSLineBreakByWordWrapping alignment: NSTextAlignmentCenter];
}

在你的 Controller .h 中导入 CornerRadious 并声明

NSTimer *m_timer;
 CornerRadious *cr;

在 ViewDidLoadMethod 中的 .m 类中

cr = [[CornerRadious alloc] initWithFrame:self.view.bounds];
    cr.percent = 50;
    [self.view addSubview:cr];

也可以在这些方法中添加

- (void)viewDidAppear:(BOOL)animated
{
    // Kick off a timer to count it down
    m_timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(decrementSpin) userInfo:nil repeats:YES];
}

- (void)decrementSpin
{
    // If we can decrement our percentage, do so, and redraw the view
    if (cr.percent > 0) {
        cr.percent = cr.percent - 1;
        [cr setNeedsDisplay];
    }
    else {
        [m_timer invalidate];
        m_timer = nil;
    }
}

希望有效果

【讨论】:

  • 使用计时器制作动画。你肯定知道如何伤我的心?
  • @morroko 我怎样才能把上半部分做成半方形!!!!请帮忙
  • 你想填充半个正方形(从上部)吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
  • 2023-02-23
  • 2017-09-17
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多