Mac OS自制进度条

来源:互联网 发布:淘宝衣服模特同一个人 编辑:程序博客网 时间:2024/05/01 07:02

系统进度条:使用setDoubleValue:传送进度值

@property (strong)IBOutletNSProgressIndicator*myprogress;

- (void)updateProgress:(double)value{

 

static doubleupdateprogress =0;

 updateprogress=get_wcnt_vlaue();

 [_myprogresssetDoubleValue:updateprogress];

}

自制进度条

总体思路:先新建一个NSView,在这个NSView上画进度条,当然,这个NSView的位置坐标及宽度、高度可以设置为与进度条一样的值。

设置背景框和进度更新框的位置坐标及宽度、高度; 然后设置背景颜色和进度条更新颜色,本例分别为白色和黑色;当进度值更新时,

使用CGRectMake去画更新的进度

注意点:

ios中可以直接在view中设置背景色,可以直接bgimg.backgroundColor,而mac中的背景色是在layer层中,即bgimg.layer.backgroundColor

且要先将layer层加载layer层,否则就是画的东西已经画上了,但是显示不出来:

 bgimg.wantsLayer = YES;//一定要加这一句,加载layer,否则画的东西显示不出来

//

//  ViewController.m

//  

//


#import "ViewController.h"

#import "CustomProgress.h"

@interface ViewController ()

{

    CustomProgress *custompro;

    NSTimer *timer;

    int progress;

}

@property (nonatomic,weak)CALayer *myLayer;

@end

@implementation ViewController



- (void)viewDidLoad {

    [superviewDidLoad];

    

    //self.view.layer.backgroundColor = [NSColor greenColor].CGColor;

   //设置NSView的坐标及宽度、高度。mac OS的左下角位置为原点(0,0)

    custompro = [[CustomProgressalloc]initWithFrame:CGRectMake(36,284,self.view.frame.size.width-72,50)];

     custompro.maxValue =100;//设置进度条最大值

    //设置背景色(R/G/B)

   //custompro.bgimg.layer.backgroundColor =[NSColor colorWithRed:188/255.0 green:188/255.0 blue:188/255.0 alpha:1].CGColor;

   custompro.bgimg.layer.backgroundColor =[NSColor whiteColor].CGColor;//设置背景颜色为白色

   custompro.leftimg.layer.backgroundColor =[NSColor blackColor].CGColor;//设置进度条更新颜色为黑色

    [self.viewaddSubview:custompro];

    //NSLog(@"%@", custompro.leftimg.layer.backgroundColor);

    timer =[NSTimerscheduledTimerWithTimeInterval:0.1

                                            target:self

                                          selector:@selector(timer)

                                          userInfo:nil

                                           repeats:YES];

}

-(void)timer

{

    progress++;

    if (progress<=100) {

        

        [customprosetPresent:progress];//传递进度条进度值

        

    }else

    {

        

        [timer invalidate];//移除定时器

        timer = nil;

        progress = 0;

        

    }

    

    

}

//

//  CustomProgress.h

//  

//

#import <Cocoa/Cocoa.h>


@interface CustomProgress : NSView

@property(nonatomic,retain)NSImageView *bgimg;

@property(nonatomic,retain)NSImageView *leftimg;

//@property(nonatomic, retain)Label *presentlab;

@property(nonatomic)float maxValue;

-(void)setPresent:(int)present;

@end



//

//  CustomProgress.m

//  test

//

//  Created by Develop on 2017/11/6.

//  Copyright © 2017年 Develop. All rights reserved.

//


#import "CustomProgress.h"


@implementation CustomProgress

@synthesize bgimg,leftimg;



- (id)initWithFrame:(CGRect)frame

{

    self = [superinitWithFrame:frame];

    if (self) {

        

        self.layer.backgroundColor= [NSColorclearColor].CGColor;

        bgimg = [[NSImageViewalloc]initWithFrame:CGRectMake(0,0,self.frame.size.width,self.frame.size.height)];

        bgimg.wantsLayer =YES;//一定要加这一句,加载layer,否则画的东西显示不出来

        bgimg.layer.borderColor = [NSColorclearColor].CGColor;//设置边界颜色

        bgimg.layer.borderWidth1;

        bgimg.layer.cornerRadius =5;//设置边角弧度

        [bgimg.layersetMasksToBounds:YES];

        [self addSubview:bgimg];

        

        leftimg = [[NSImageViewalloc]initWithFrame:CGRectMake(0,0,0,self.frame.size.height)];

        leftimg.wantsLayer =YES;

        leftimg.layer.borderColor = [NSColorclearColor].CGColor;

        leftimg.layer.borderWidth1;

        leftimg.layer.cornerRadius =5;

        [leftimg.layersetMasksToBounds:YES];

        [self addSubview:leftimg];

        

        [selfsetNeedsDisplay:YES];

    }

    return self;

}

-(void)setPresent:(int)present

{

    

    leftimg.frame =CGRectMake(0,0,self.frame.size.width/self.maxValue*present,self.frame.size.height);


}

@end

效果图:






原创粉丝点击