Cocoa ScreenSaverView

1.1k views Asked by At

I am trying to play with ScreenSaverView in mac OS X.

I followed this tutorial http://cocoadevcentral.com/articles/000088.php and it worked (can't say flawlessly ,but worked). I also saw in part 2 of this tutorial they are playing with openGL stuff etc.

I have 2 general questions:

1)Can I use SprteKit within screensaver view?

2)What is wrong with my code here -> it compiles well, however I don't see anything except a black screen (I want to see my *.png).

- (instancetype)initWithFrame:(NSRect)frame isPreview:(BOOL)isPreview
{
    self = [super initWithFrame:frame isPreview:isPreview];
    if (self)
    {
        _imageView = [[NSImageView alloc]initWithFrame:[self bounds]];
        [_imageView setImage:[NSImage imageNamed:@"SettingsButton.png"]];
        [self addSubview:_imageView];
        [self setAnimationTimeInterval:1.0];
    }
    return self;
}

EDIT: Attempt to use a draw rect:

- (void)drawRect:(NSRect)rect0
{
    [super drawRect:rect0];

    NSImage *anotherImage = [NSImage imageNamed:@"SettingsButton.png"];
    [anotherImage drawAtPoint:NSMakePoint(10,100) fromRect:NSMakeRect(0,0,[anotherImage size].width,[anotherImage size].height) operation:NSCompositeCopy fraction:1.0];


    // Calculate a random color
    CGFloat red = SSRandomFloatBetween( 0.0, 255.0 ) / 255.0;
    CGFloat green = SSRandomFloatBetween( 0.0, 255.0 ) / 255.0;
    CGFloat blue = SSRandomFloatBetween( 0.0, 255.0 ) / 255.0;

    NSColor* color = [NSColor colorWithCalibratedRed:red
                                  green:green
                                   blue:blue
                                  alpha:1];


    NSColor * color2 = [NSColor colorWithPatternImage:anotherImage];
    [color2 set];

    NSBezierPath * path = [NSBezierPath bezierPathWithRect:rect0];
   [path fill];
}

When I set a color it works , I can see a screen filled by Random colors (fine as meant)

When I use color2 which is a pattern from the image nothing is works :-( -> I tried different images same nothing there...

I checked in build phase that I do copy the images as a bundle resources

What could be the problem ?

EDIT: Ok so after my attempt in drawRect I suspected that imageNamed method is causing a troubles and rewrite my origin attempt to:

- (instancetype)initWithFrame:(NSRect)frame isPreview:(BOOL)isPreview
{
    self = [super initWithFrame:frame isPreview:isPreview];
    if (self)
    {
        _imageView = [[NSImageView alloc]initWithFrame:[self bounds]];
        NSBundle * tempBundle = [NSBundle bundleForClass:[self class]];

    // Load your image, "top.tiff". Don't forget to release it when you are done (not shown).
        NSImage * theImage = [[NSImage alloc] initWithContentsOfFile:
                          [tempBundle pathForImageResource:@"personal1.jpg"]];
        [_imageView setImage:theImage];
        [self addSubview:_imageView];
        [self setAnimationTimeInterval:1.0];

    }
    return self;
}

And Whoaallaaa it worked !!(Kinda) I see my picture :-) !!!

As for part1 of the Question -> Yes it is possible Just tried and it works ! with a little down side I can exit ScreenSaver only with cmd key xD

Managed to solve this issue,by subclassing SKView and delivering event to the next responder.

However it gave me a huge idea -> actually it just opens an opportunity to make a simple SK game as part of the screensaver could have been a cute feature.

Sadly I can't submit it to app store :)

1

There are 1 answers

12
Droppy On BEST ANSWER

It's not clear to me if a SpriteKit view (SKView) can even be used in a normal OSX/iOS app. I have searched in the past and found nothing.

If SpriteKit is anything like other game frameworks (from which it borrows much of its structure) then it will use a traditional game loop; i.e. clear screen, draw everything, wait a bit and then repeat.

Cocoa apps use an runloop that reacts to events and take steps to only redraw what needs to be redrawn.

So I would say "No" to your first question.

As far as your second, code-related, question is concerned, then I cannot see much wrong with it, however I am not familiar with that init method; what class does it subclass?