ios - 如何获得Semi Circle进度栏?

标签 ios objective-c progress-bar

谁能帮我?给我一些想法来实现这一目标:)

最佳答案

使UIView类在.h文件中声明

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

希望它能工作

关于ios - 如何获得Semi Circle进度栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22709208/

相关文章:

iphone - replaceItemAtURL :withItemAtURL:backupItemName:options:resultingItemURL:error: broken in iOS 6?

ios - 如何在 iOS Objective C 中将 NSData 转换为 UInt8 数组?

java - ProgressBar propertychangeListener 在这种情况下不会更新

ios - MVC 模型 - Controller 应该直接访问 View 的控件吗?

objective-c - 为什么委托(delegate)中需要一些方法?

c# - ProgressBar 的颜色没有改变 c#

java - Android进度条填充

ios - 如何让 UIAlertView 自动消失?

ios - 如何聚焦 UIView?

ios - 如何通过Carthage获取github上没有的依赖项?