ios - 带定时器的 UIButton

标签 ios object uiview

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

waze
(来源:efytimes.com)

最佳答案

我可以向您展示如何绘制代表计时器的圆圈,我相信您可以从那里开始。代码如下:

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

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

关于ios - 带定时器的 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11783439/

相关文章:

ios - 如何在 swift 2.0 中使用 facebook 登录

ios - 将音频修剪为视频长度

iphone - 在 iOS 上,如果一个 super View 的 userInteractionEnabled 是 NO,那么所有的 subview 也会被禁用吗?

ios - 以编程方式调用 UIView 的点击手势

iphone - 如何在 iPhone UIView 中使用负号框架大小?

ios - iOS 中的 LaunchScreen.storyboard 和 Main.storyboard 有什么不同?

javascript - 如何在javascript中将对象属性字符串转换为整数

php - 取消设置变量属性上的数组

c++ - 在类中的函数外部归档 vector

iOS 重新加载两个 tableView 部分