淺談MacOS開(kāi)發(fā)-NSSlider,它和iOS的UISlider相比,差別真的是太大了。
首先我們簡(jiǎn)單了解下 iOS UISlider 的API文檔截圖??聪挛臋n說(shuō)明,屬性很多,還有部分沒(méi)有截出來(lái)??梢哉f(shuō),關(guān)于UISlider的99%的設(shè)計(jì)功能均能實(shí)現(xiàn)。

其次我們?cè)賮?lái)了解下 MacOS NSSlider 的API文檔截圖??聪挛臋n說(shuō)明。寶寶想哭??。

我們的功能是:開(kāi)發(fā)類似愛(ài)奇藝的音量控制功能??词呛?jiǎn)單的功能,浪費(fèi)我3個(gè)小時(shí)。系統(tǒng)的NSSlider不能滿足開(kāi)發(fā)需求。只能自己封裝了。下面是原始代碼:
第一步:創(chuàng)建自己的封裝類:RSSliderCells
.h文件:
#import@interface RSSliderCells : NSSliderCell
@end
第二部:重寫drawBarInside方法:
.m文件:
#import "RSSliderCells.h"
@implementation RSSliderCells
- (void)drawBarInside:(NSRect)rect flipped:(BOOL)flipped {
? ? [super drawBarInside:rect flipped:flipped];
? ? rect.size.height = 110;
? ? // Bar radius
? ? CGFloat barRadius = 2.5;
? ? CGFloat value = ([self doubleValue]? - [self minValue]) / ([self maxValue] - [self minValue]);
? ? CGFloat finalWidth = value * ([[self controlView] frame].size.height - 4);
? ? NSRect leftRect = rect;
? ? leftRect.size.height = finalWidth;
? ? leftRect.origin.y = 110 -? (finalWidth);
? ? NSBezierPath* bg = [NSBezierPath bezierPathWithRoundedRect: rect xRadius: barRadius yRadius: barRadius];
? ? [NSColor.lightGrayColor setFill];
? ? [bg fill];
? ? NSBezierPath *bezierPath = [NSBezierPath bezierPathWithRoundedRect:leftRect xRadius:barRadius yRadius:barRadius];
? ? // 設(shè)置線的填充色
? ? [NSColor.greenColor setFill];
? ? [bezierPath fill];
}
@end
最后實(shí)現(xiàn)的功能截圖如下:
