MacOS 开发 - NSProgressIndicator
2017年10月18日
一、引言
一开始觉得这个控件比较简单,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];
1
[self.indicator stopAnimation:nil];
1
四、配合进度的动画
特别注意:
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
};
2、controlSize 尺寸大小
indicator.controlSize = NSControlSizeRegular;
typedef NS_ENUM(NSUInteger, NSControlSize) {
NSControlSizeRegular, //32
NSControlSizeSmall, //16
NSControlSizeMini, //10
};
3、sizeToFit
[indicator sizeToFit];
---------------------
作者:lovechris00
来源:CSDN
原文:https://blog.csdn.net/lovechris00/article/details/78271045
版权声明:本文为博主原创文章,转载请附上博文链接!