2014-03-28 22 views
1

Seek Bar like this如何才能實現半圓進度條?

任何人都可以幫助我嗎?給我一些想法,以實現這一目標:)

+1

您是否嘗試過這個https://github.com/martinezdelariva/MRCircularProgressView? –

+1

我沒有試過。但它會給出一些想法。謝謝@ ali59a。 – iTag

+0

如何將它修改爲「半圈進度」@ ali59a – iTag

回答

4

做出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]; 
} 

在控制器的.h import CornerRadious並聲明

NSTimer *m_timer; 
CornerRadious *cr; 

在ViewDidLoadMethod中的.m類中

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

ASLO使加在這些方法

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

希望它的工作

+0

Thanks @Maximus。 – iTag

+0

歡迎.. @iTag – morroko

+0

用計時器動畫。你肯定知道如何打破我的心 –