需要样品代码在Cocoa / Quartz 2d Speedometer for Mac App中摆针

时间:2014-09-10 16:21:20

标签: macos cocoa osx-mavericks quartz-2d

我正在构建它以在Mac上运行,而不是iOS - 这是不同的。我几乎和speedo在一起,但是当数据输入时,使针头在秤上上下移动的数学计算使我失望。 我正在测量风速,并希望将其显示为仪表 - 速度计,随着风速的变化,针移动。我有基本原则。我也可以 - 并且将 - 将图像加载到持有者中,但稍后。现在我想让它运转起来......

    - (void)drawRect:(NSRect)rect
{


    NSRect myRect = NSMakeRect ( 21, 21, 323, 325 ); // set the Graphics class square size to match the guage image
    [[NSColor blueColor] set]; // colour it in in blue - just because you can...
    NSRectFill ( myRect );



    [[NSGraphicsContext currentContext]                     // set up the graphics context
     setImageInterpolation: NSImageInterpolationHigh];      // highres image

    //-------------------------------------------

    NSSize viewSize  = [self bounds].size;
    NSSize imageSize = { 320, 322 };                        // the actual image rectangle size. You can scale the image here if you like. x and y remember

    NSPoint viewCenter;
    viewCenter.x = viewSize.width  * 0.50;                  // set the view center, both x & y
    viewCenter.y = viewSize.height * 0.50;

    NSPoint imageOrigin = viewCenter;
    imageOrigin.x -= imageSize.width  * 0.50;               // set the origin of the first point
    imageOrigin.y -= imageSize.height * 0.50;

    NSRect destRect;
    destRect.origin = imageOrigin;                          // set the image origin
    destRect.size = imageSize;                              // and size

    NSString * file = @"/Users/robert/Documents/XCode Projects/xWeather Graphics/Gauge_mph_320x322.png";  // stuff in the image
    NSImage * image = [[NSImage alloc] initWithContentsOfFile:file];

    //-------------------------------------------

    NSSize view2Size  = [self bounds].size;
    NSSize image2Size = { 149, 17 };                          // the orange needle
    NSPoint view2Center;
    view2Center.x = view2Size.width  * 0.50;                  // set the view center, both x & y
    view2Center.y = view2Size.height * 0.50;

    NSPoint image2Origin = view2Center;
    //image2Origin.x -= image2Size.width  * 0.50;               // set the origin of the first point
    image2Origin.x = 47;
    image2Origin.y -= image2Size.height * 0.50;

    NSRect dest2Rect;
    dest2Rect.origin = image2Origin;                          // set the image origin
    dest2Rect.size = image2Size;                              // and size now is needle size


    NSString * file2  = @"/Users/robert/Documents/XCode Projects/xWeather Graphics/orange-needle01.png";
    NSImage * image2 = [[NSImage alloc] initWithContentsOfFile:file2];


                                                            // do image 1
    [image setFlipped:YES];                                 // flip it because everything else is in this exerecise
                                                            // do image 2
    [image2 setFlipped:YES];                                // flip it because everything else is in this exerecise


    [image drawInRect: destRect
             fromRect: NSZeroRect
            operation: NSCompositeSourceOver
             fraction: 1.0];


    [image2 drawInRect: dest2Rect
             fromRect: NSZeroRect
            operation: NSCompositeSourceOver
             fraction: 1.0];





    NSBezierPath * path = [NSBezierPath bezierPathWithRect:destRect]; // draw a red border around the whole thing
    [path setLineWidth:3];
    [[NSColor redColor] set];
    [path stroke];


}

//翻转ocords - (BOOL)isFlipped {return YES; }

@end

结果是here。测量部分是。现在我所要做的就是让指针根据输入移动。

1 个答案:

答案 0 :(得分:0)

Apple有一些示例代码,名为SpeedometerView,它完全符合您的要求。它肯定会采取一些措施来适应你的使用,但它可能是一个不错的起点。