美文网首页
Mac开发-NSSlider

Mac开发-NSSlider

作者: 只爱随便看看 | 来源:发表于2019-08-02 10:25 被阅读0次

NSSlider实现七彩色的进度条
自定义NSSliderCell
初始化进度条相关的事件:整体颜色、半径、滑动块的大小

- (void)commonInit
{
    _colorArray = [NSArray arrayWithObjects:[NSColor redColor], [NSColor orangeColor], [NSColor yellowColor], [NSColor greenColor], [NSColor cyanColor], [NSColor blueColor], [NSColor purpleColor], [NSColor whiteColor], [NSColor blackColor], nil];
    _sliderBarRadius = 4;
    _sliderKnobWidth = 10;
    _sliderKnobHeight = 10;
}

左右两边

- (void)drawBarInside:(NSRect)rect flipped:(BOOL)flipped
{
    // Bar radius
    CGFloat barRadius = self.sliderBarRadius;
    // Knob position depending on control min/max value and current control value.
    CGFloat value = ([self doubleValue]  - [self minValue]) / ([self maxValue] - [self minValue]);
    // Final Left Part Width
    CGFloat finalWidth = value * ([[self controlView] frame].size.width - self.sliderKnobWidth);

    // Left Part Rect
    NSRect leftRect = rect;
    leftRect.size.width = finalWidth;
    self.leftBarRect = leftRect;
    
    NSGradient* linearGradient = [[NSGradient alloc] initWithColors:_colorArray];
    NSBezierPath *bg = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:barRadius yRadius:barRadius];
    [linearGradient drawInBezierPath:bg angle:0];
    
    self.knobColor = [linearGradient interpolatedColorAtLocation:value];
}

指示标识

- (void)drawKnob:(NSRect)knobRect
{
    NSRect customKnobRect = NSMakeRect(_leftBarRect.size.width, _leftBarRect.origin.y + _leftBarRect.size.height / 2 - self.sliderKnobHeight / 2, self.sliderKnobWidth, self.sliderKnobHeight);
    
    // Draw Left Part
    NSBezierPath *bg = [NSBezierPath bezierPathWithRoundedRect:customKnobRect xRadius: self.sliderKnobWidth / 2 yRadius: self.sliderKnobHeight / 2];
    [self.knobColor setFill];
    [bg fill];
}

效果图:


效果图.png

相关文章

网友评论

      本文标题:Mac开发-NSSlider

      本文链接:https://www.haomeiwen.com/subject/cjpkdctx.html