3

私は Cocoa で絵を描くのは初めてで、GarageBand にあるものと同様のスライダーを持つソフトウェアをいくつか作成しています。

GB スライダー http://img33.imageshack.us/img33/2668/schermafbeelding2010061r.png

これらは美しく見え、マウスを上下に動かすことで制御できます。

NSSliders をサブクラス化してカスタマイズして、GarageBand とまったく同じ外観と動作にすることができますか? ありがとう。


3D である必要がないため、回転する必要があるノブの画像が 1 つあります。

4

2 に答える 2

4

最も簡単な方法は、マウス管理と描画の両方を処理する NSView サブクラスを作成することです。

「TLayer」という名前の開始に役立つサンプルコードがあります。これは、XCode 3.1.4 の例の一部です。レイヤーに描画される影のオフセットと半径を制御する円形のカスタム ビューが含まれています。分かりやすく、伸ばしやすいです。

注: Apple の Web サイトでは入手できないようですので、以下にソースを貼り付けました。


ShadowOffsetView.h

#import <AppKit/AppKit.h>

extern NSString *ShadowOffsetChanged;

@interface ShadowOffsetView : NSView
{
    CGSize _offset;
    float _scale;
}

- (float)scale;
- (void)setScale:(float)scale;

- (CGSize)offset;
- (void)setOffset:(CGSize)offset;

@end

ShadowOffsetView.m

#import "ShadowOffsetView.h"

NSString *ShadowOffsetChanged = @"ShadowOffsetChanged";

@interface ShadowOffsetView (Internal)
- (NSCell *)cell;
@end

@implementation ShadowOffsetView

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self == nil)
    return nil;

    _offset = CGSizeZero;

    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (float)scale
{
    return _scale;
}

- (void)setScale:(float)scale
{
    _scale = scale;
}

- (CGSize)offset
{
    return CGSizeMake(_offset.width * _scale, _offset.height * _scale);
}

- (void)setOffset:(CGSize)offset
{
    offset = CGSizeMake(offset.width / _scale, offset.height / _scale);
    if (!CGSizeEqualToSize(_offset, offset)) {
    _offset = offset;
    [self setNeedsDisplay:YES];
    }
}

- (BOOL)isOpaque
{
    return NO;
}

- (void)setOffsetFromPoint:(NSPoint)point
{
    float radius;
    CGSize offset;
    NSRect bounds;
    
    bounds = [self bounds];
    offset.width = (point.x - NSMidX(bounds)) / (NSWidth(bounds) / 2);
    offset.height = (point.y - NSMidY(bounds)) / (NSHeight(bounds) / 2);
    radius = sqrt(offset.width * offset.width + offset.height * offset.height);
    if (radius > 1) {
    offset.width /= radius;
    offset.height /= radius;
    }
    if (!CGSizeEqualToSize(_offset, offset)) {
    _offset = offset;
    [self setNeedsDisplay:YES];
    [(NSNotificationCenter *)[NSNotificationCenter defaultCenter]
        postNotificationName:ShadowOffsetChanged object:self];
    }
}

- (void)mouseDown:(NSEvent *)event
{
    NSPoint point;

    point = [self convertPoint:[event locationInWindow] fromView:nil];
    [self setOffsetFromPoint:point];
}

- (void)mouseDragged:(NSEvent *)event
{
    NSPoint point;

    point = [self convertPoint:[event locationInWindow] fromView:nil];
    [self setOffsetFromPoint:point];
}

- (void)drawRect:(NSRect)rect
{
    NSRect bounds;
    CGContextRef context;
    float x, y, w, h, r;

    bounds = [self bounds];
    x = NSMinX(bounds);
    y = NSMinY(bounds);
    w = NSWidth(bounds);
    h = NSHeight(bounds);
    r = MIN(w / 2, h / 2);
    
    context = [[NSGraphicsContext currentContext] graphicsPort];

    CGContextTranslateCTM(context, x + w/2, y + h/2);

    CGContextAddArc(context, 0, 0, r, 0, 2*M_PI, true);
    CGContextClip(context);

    CGContextSetGrayFillColor(context, 0.910, 1);
    CGContextFillRect(context, CGRectMake(-w/2, -h/2, w, h));

    CGContextAddArc(context, 0, 0, r, 0, 2*M_PI, true);
    CGContextSetGrayStrokeColor(context, 0.616, 1);
    CGContextStrokePath(context);

    CGContextAddArc(context, 0, -2, r, 0, 2*M_PI, true);
    CGContextSetGrayStrokeColor(context, 0.784, 1);
    CGContextStrokePath(context);

    CGContextMoveToPoint(context, 0, 0);
    CGContextAddLineToPoint(context, r * _offset.width, r * _offset.height);

    CGContextSetLineWidth(context, 2);
    CGContextSetGrayStrokeColor(context, 0.33, 1);
    CGContextStrokePath(context);
}

@end
于 2010-06-13T17:10:30.983 に答える
1

実際の描画では、ノブの回転角度ごとに画像を用意し (実装が簡単)、適切な画像を描画する必要があります。

(実際にリアルな 3D の外観を得るには (たとえ可能であったとしても)、プログラムによる描画は時間に見合わないと思います。)

またはコードでノブを描画します。この記事は、私が考えるアイデアを提供するはずです : http://katidev.com/blog/2008/03/07/how-to-create-a-custom-control-with-nsview/円形および回転するノブのような要素の基本的な NSBezerPath 描画)

于 2010-06-13T14:48:31.810 に答える