Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 带定时器的按钮_Ios_Object_Uiview - Fatal编程技术网

Ios 带定时器的按钮

Ios 带定时器的按钮,ios,object,uiview,Ios,Object,Uiview,我想创建一个带有计时器的UIButton,如所附图片所示。我该怎么做呢? 将MBProgressHUD添加到UIButton会有帮助吗 (来源:)我可以教你如何画代表计时器的圆圈,我相信你可以从那里画出来。代码如下: TimerButton.h #import <UIKit/UIKit.h> @interface TimerButton : UIView { float currentAngle; float currentTime; float time

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



(来源:)

我可以教你如何画代表计时器的圆圈,我相信你可以从那里画出来。代码如下:

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

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

计时器指示器是否需要像图中所示的那样旋转?如果要使其成为一行,可以将UIProgressView与NSTimer结合使用,每秒钟左右更新一次进度。
#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