0

添付の写真のようにタイマー付きのUIButtonを作成したいのですが。どうすればいいですか?MBProgressHUDをUIButtonに追加すると役立ちますか?

Waze
(出典:efytimes.com

4

1 に答える 1

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

それを試してみて、さらに説明が必要な場合はお知らせください。

于 2012-08-02T19:03:55.550 に答える