2012-08-02 46 views
0

我想創建一個帶有定時器的UIButton,如附圖所示。我該如何解決它? 將MBProgressHUD添加到UIButton幫助?帶定時器的UIButton

waze http://www.efytimes.com/admin/useradmin/rte/my_documents/my_pictures/CA6_Waze3.PNG

+0

是否要求定時器指示符是圖片中顯示的旋轉圓圈? 如果你想使它成爲一條線,你可以使用UIProgressView和NSTimer每秒更新一次進度。 – 2012-08-02 18:22:21

+0

你嘗試過什麼嗎? – NSPunk 2012-08-02 18:45:13

回答

1

我可以告訴你如何繪製表示定時器的圈子,我敢肯定,你可以把它從那裏。下面的代碼:

TimerButton.h

#import <UIKit/UIKit.h> 

@interface TimerButton : UIView 
{ 
    float currentAngle; 
    float currentTime; 
    float timerLimit; 
    NSTimer *timer; 
} 

@property float currentAngle; 

-(void)stopTimer; 
-(void)startTimerWithTimeLimit:(int)tl; 

@end 

TimerButton.m

#import "TimerButton.h" 

@implementation TimerButton 

#define DEGREES_TO_RADIANS(degrees) ((3.14159265359 * degrees)/ 180) 
#define TIMER_STEP .01 

@synthesize currentAngle; 

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

     currentAngle = 0; 

    } 
    return self; 
} 

- (void)drawRect:(CGRect)rect 
{ 
    UIBezierPath* aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(50, 50) 
                 radius:45 
                startAngle:DEGREES_TO_RADIANS(0) 
                 endAngle:DEGREES_TO_RADIANS(currentAngle) 
                 clockwise:YES]; 
    [[UIColor redColor] setStroke]; 
    aPath.lineWidth = 5; 
    [aPath stroke]; 
} 

-(void)startTimerWithTimeLimit:(int)tl 
{ 
    timerLimit = tl; 
    timer = [NSTimer scheduledTimerWithTimeInterval:TIMER_STEP target:self selector:@selector(updateTimerButton:) userInfo:nil repeats:YES]; 
} 

-(void)stopTimer 
{ 
    [timer invalidate]; 
} 

-(void)updateTimerButton:(NSTimer *)timer 
{ 
    currentTime += TIMER_STEP; 
    currentAngle = (currentTime/timerLimit) * 360; 

    if(currentAngle >= 360) [self stopTimer]; 
    [self setNeedsDisplay]; 
} 

@end 

給一個嘗試,讓我知道,如果你需要進一步的解釋。

+0

這工作..謝謝!! – ddd 2012-08-03 00:02:42