MacOS 开发 - NSProgressIndicator


一、引言

一开始觉得这个控件比较简单,iOS 上也很少用到原生的indicator (一般用 MBProgressHUD),没什么好写的。
后来发现这块资料很少,也有人问道,索性还是简单写写。


二、创建

	NSProgressIndicator *indicator = [[NSProgressIndicator alloc]initWithFrame:CGRectMake(150, 100, 40, 40)];
    indicator.style = NSProgressIndicatorSpinningStyle;
    
    //这种方式只是给背景rect添加了背景色。
    indicator.wantsLayer = YES;
    indicator.layer.backgroundColor = [NSColor cyanColor].CGColor;
  
    indicator.controlSize = NSControlSizeRegular;
    [indicator sizeToFit];
    self.indicator = indicator;
    
    [self.window.contentView addSubview:indicator];

三、startAnimation & stopAnimation 动起来

通过按钮控制 self.indicator

[self.indicator startAnimation:nil];

[self.indicator stopAnimation:nil];

四、配合进度的动画

特别注意:
1、需要设置 indeterminate 为NO。
indeterminate 是 不确定的、模糊的意思,模糊状态下,不会显示具体的进度。
2、最大值默认为 100,doubleValue 值范围是 [0,100]。不是 [0,1] 哦。
这里写一个示例:

@property (weak) IBOutlet NSWindow *window;

@property (nonatomic,strong) NSProgressIndicator *indicator;
@property (nonatomic,strong) NSTimer *showTimer;
@property (nonatomic,assign) double count;

@end

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    
    self.count = 0;
    [self.showTimer fire];
    
    
    NSProgressIndicator *indicator = [[NSProgressIndicator alloc]initWithFrame:CGRectMake(16, 40, 288, 100)];
    indicator.style = NSProgressIndicatorStyleBar;
    indicator.controlSize = NSControlSizeRegular;
    
    
    indicator.indeterminate = NO;  //设置是精准的进度条还是模糊的指示器,NO-精度
    indicator.minValue = 0.0;
    indicator.maxValue = 100.0;
    indicator.doubleValue = 33.0;
    
    self.indicator = indicator;
    [self.window.contentView addSubview:indicator];
    
}

- (void)countTime{
    
    self.count += 1.0;
    
    if (self.count == 100) {
        self.count = 0;
    }
    
    [self.indicator setDoubleValue:self.count];
    NSLog(@"d : %f",self.indicator.doubleValue);
    
}

-(NSTimer *)showTimer{
    if(!_showTimer){
        //时间间隔
        NSTimeInterval timeInterval =0.2 ;
        _showTimer = [NSTimer scheduledTimerWithTimeInterval:timeInterval
                                                      target:self
                                                    selector:@selector(countTime)
                                                    userInfo:nil
                                                     repeats:YES];
    }
    return _showTimer;
}


五、NSProgressIndicatorStyle 显示样式

1、style

indicator.style = NSProgressIndicatorSpinningStyle;

typedef NS_ENUM(NSUInteger, NSProgressIndicatorStyle) {
    NSProgressIndicatorStyleBar = 0,
    NSProgressIndicatorStyleSpinning = 1
};

NSProgressIndicatorStyle


2、controlSize 尺寸大小

indicator.controlSize = NSControlSizeRegular;

typedef NS_ENUM(NSUInteger, NSControlSize) {
    NSControlSizeRegular, //32
    NSControlSizeSmall, //16
    NSControlSizeMini, //10
};

controlSize


3、sizeToFit

[indicator sizeToFit];

sizeToFit


4、修改颜色、自定义及黑暗模式适配

如果设置为 spinningstyle,10.14 以后的黑暗模式下,可能会无法显示。
这时可能需要自定义才能解决。
一个很不错的自定义 Indicator :
https://github.com/kelan/YRKSpinningProgressIndicator

在这里插入图片描述


参考

https://stackoverflow.com/questions/456445/how-can-i-display-the-spinning-nsprogressindicator-in-a-different-color


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI工程仔

请我喝杯伯爵奶茶~!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值