Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7599383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:38:52+00:00 2026-05-30T22:38:52+00:00

I’m creating a spotlight that moves over content in my app, like so: In

  • 0

I’m creating a spotlight that moves over content in my app, like so:

Spotlight A Spotlight B

In the sample app (shown above), the background layer is blue, and I have a layer over it that darkens all of it, except a circle that shows it normally. I’ve got this working (you can see how in the code below). In my real app, there is actual content in other CALayers, rather than just blue.

Here’s my problem: it doesn’t animate. I’m using CGContext drawing to create the circle (which is an empty spot in an otherwise black layer). When you click the button in my sample app, I draw the circle at a different size in a different location.

I would like that to smoothly translate and scale, instead of jumping, as it currently does. It may require a different method of creating the spotlight effect, or there might be a way I don’t know of to implicitly animate the -drawLayer:inContext: call.

It’s easy to create the sample app:

  1. Make a new Cocoa app (using ARC)
  2. Add the Quartz framework
  3. Drop a custom view and a button onto the XIB
  4. Link the custom view to a new class (SpotlightView), with code provided below
  5. Delete SpotlightView.h, since I included its contents in SpotlightView.m
  6. Set the button’s outlet to the -moveSpotlight: action

Update (the mask property)

I like David Rönnqvist’s suggestion in comments to use the mask property of the darkened layer to cut out a hole, which I could then move independently. The problem is that for some reason, the mask property works the opposite of how I expect a mask to work. When I specify a circular mask, all that shows up is the circle. I expected the mask to work in the opposite manner, masking out the area with 0 alpha.

Masking feels like the right way to go about this, but if I have to fill in the entire layer and cut out a hole, then I may as well do it the way I originally posted. Does anyone know how to invert the -[CALayer mask] property, so that the area drawn in gets cut out from the layer’s image?

/Update

Here’s the code for SpotlightView:

//
//  SpotlightView.m
//

#import <Quartz/Quartz.h>

@interface SpotlightView : NSView
- (IBAction)moveSpotlight:(id)sender;
@end

@interface SpotlightView ()
@property (strong) CALayer *spotlightLayer;
@property (assign) CGRect   highlightRect;
@end


@implementation SpotlightView

@synthesize spotlightLayer;
@synthesize highlightRect;

- (id)initWithFrame:(NSRect)frame {
    if ((self = [super initWithFrame:frame])) {
        self.wantsLayer = YES;

        self.highlightRect = CGRectNull;

        self.spotlightLayer = [CALayer layer];
        self.spotlightLayer.frame = CGRectInset(self.layer.bounds, -50, -50);
        self.spotlightLayer.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;

        self.spotlightLayer.opacity = 0.60;
        self.spotlightLayer.delegate = self;

        CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
        [blurFilter setValue:[NSNumber numberWithFloat:5.0]
                      forKey:@"inputRadius"];
        self.spotlightLayer.filters = [NSArray arrayWithObject:blurFilter];

        [self.layer addSublayer:self.spotlightLayer];
    }

    return self;
}

- (void)drawRect:(NSRect)dirtyRect {}

- (void)moveSpotlight:(id)sender {
    [self.spotlightLayer setNeedsDisplay];
}

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
    if (layer == self.spotlightLayer) {
        CGContextSaveGState(ctx);

        CGColorRef blackColor = CGColorCreateGenericGray(0.0, 1.0);

        CGContextSetFillColorWithColor(ctx, blackColor);
        CGColorRelease(blackColor);

        CGContextClearRect(ctx, layer.bounds);
        CGContextFillRect(ctx, layer.bounds);

        // Causes the toggling
        if (CGRectIsNull(self.highlightRect) || self.highlightRect.origin.x != 25) {
            self.highlightRect = CGRectMake(25, 25, 100, 100);
        } else {
            self.highlightRect = CGRectMake(NSMaxX(self.layer.bounds) - 50,
                                            NSMaxY(self.layer.bounds) - 50,
                                            25, 25);
        }

        CGRect drawnRect = [layer convertRect:self.highlightRect
                                    fromLayer:self.layer];
        CGMutablePathRef highlightPath = CGPathCreateMutable();
        CGPathAddEllipseInRect(highlightPath, NULL, drawnRect);
        CGContextAddPath(ctx, highlightPath);

        CGContextSetBlendMode(ctx, kCGBlendModeClear);
        CGContextFillPath(ctx);

        CGPathRelease(highlightPath);
        CGContextRestoreGState(ctx);
    }
    else {
        CGColorRef blueColor = CGColorCreateGenericRGB(0, 0, 1.0, 1.0);
        CGContextSetFillColorWithColor(ctx, blueColor);
        CGContextFillRect(ctx, layer.bounds);
        CGColorRelease(blueColor);
    }
}

@end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:38:53+00:00Added an answer on May 30, 2026 at 10:38 pm

    I finally got it. What prodded me to the answer was hearing of the CAShapeLayer class. At first, I thought it would be a simpler way to draw the layer’s contents, rather than drawing and clearing the contents of a standard CALayer. But I read the documentation of the path property of CAShapeLayer, which stated it could be animated, but not implicitly.

    While a layer mask might have been more intuitive and elegant, it doesn’t seem to be possible to use the mask to hide a portion of the owner’s layer, rather than showing a portion, and so I couldn’t use it. I’m happy with this solution, as it’s pretty clear what’s going on. I wish it used implicit animation, but the animation code is only a few lines.

    Below, I’ve modified the sample code from the question to add smooth animation. (I removed the CIFilter code, because it was extraneous. The solution does still work with filters.)

    //
    //  SpotlightView.m
    //
    
    #import <Quartz/Quartz.h>
    
    @interface SpotlightView : NSView
    - (IBAction)moveSpotlight:(id)sender;
    @end
    
    @interface SpotlightView ()
    @property (strong) CAShapeLayer *spotlightLayer;
    @property (assign) CGRect   highlightRect;
    @end
    
    
    @implementation SpotlightView
    
    @synthesize spotlightLayer;
    @synthesize highlightRect;
    
    - (id)initWithFrame:(NSRect)frame {
        if ((self = [super initWithFrame:frame])) {
            self.wantsLayer = YES;
    
            self.highlightRect = CGRectNull;
    
            self.spotlightLayer = [CAShapeLayer layer];
            self.spotlightLayer.frame = self.layer.bounds;
            self.spotlightLayer.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
            self.spotlightLayer.fillRule = kCAFillRuleEvenOdd;
    
            CGColorRef blackoutColor = CGColorCreateGenericGray(0.0, 0.60);
            self.spotlightLayer.fillColor = blackoutColor;
            CGColorRelease(blackoutColor);
    
            [self.layer addSublayer:self.spotlightLayer];
        }
    
        return self;
    }
    
    - (void)drawRect:(NSRect)dirtyRect {}
    
    - (CGPathRef)newSpotlightPathInRect:(CGRect)containerRect
                          withHighlight:(CGRect)spotlightRect {
        CGMutablePathRef shape = CGPathCreateMutable();
    
        CGPathAddRect(shape, NULL, containerRect);
    
        if (!CGRectIsNull(spotlightRect)) {
            CGPathAddEllipseInRect(shape, NULL, spotlightRect);
        }
    
        return shape;
    }
    
    - (void)moveSpotlight {
        CGPathRef toShape = [self newSpotlightPathInRect:self.spotlightLayer.bounds
                                           withHighlight:self.highlightRect];
    
        CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
        pathAnimation.fromValue = (__bridge id)self.spotlightLayer.path;
        pathAnimation.toValue   = (__bridge id)toShape;
    
        [self.spotlightLayer addAnimation:pathAnimation forKey:@"path"];
        self.spotlightLayer.path = toShape;
    
        CGPathRelease(toShape);
    }
    
    - (void)moveSpotlight:(id)sender {
        if (CGRectIsNull(self.highlightRect) || self.highlightRect.origin.x != 25) {
            self.highlightRect = CGRectMake(25, 25, 100, 100);
        } else {
            self.highlightRect = CGRectMake(NSMaxX(self.layer.bounds) - 50,
                                            NSMaxY(self.layer.bounds) - 50,
                                            25, 25);
        }
    
        [self moveSpotlight];
    }
    
    - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
        CGColorRef blueColor = CGColorCreateGenericRGB(0, 0, 1.0, 1.0);
        CGContextSetFillColorWithColor(ctx, blueColor);
        CGContextFillRect(ctx, layer.bounds);
        CGColorRelease(blueColor);
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.