0

我想创建一个带有计时器的 UIButton,如附图所示。我该怎么做?将 MBProgressHUD 添加到 UIButton 有帮助吗?

位智
(来源:efytimes.com

4

1 回答 1

1

我可以向你展示如何绘制代表计时器的圆圈,我相信你可以从那里得到它。这是代码:

定时器按钮.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

定时器按钮.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

试一试,如果您需要进一步解释,请告诉我。

于 2012-08-02T19:03:55.550 回答