oc之mac中 - NSProgressIndicator

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

};

 NSProgressIndicatorStyle

2、controlSize 尺寸大小

 

indicator.controlSize = NSControlSizeRegular;

 

typedef NS_ENUM(NSUInteger, NSControlSize) {

    NSControlSizeRegular, //32

    NSControlSizeSmall, //16

    NSControlSizeMini, //10

};

 

controlSize 

3、sizeToFit

 

[indicator sizeToFit];

 

 sizeToFit

--------------------- 

作者:lovechris00 

来源:CSDN 

原文:https://blog.csdn.net/lovechris00/article/details/78271045 

版权声明:本文为博主原创文章,转载请附上博文链接!

 

转载于:https://www.cnblogs.com/sundaymac/p/10338769.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值