Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>(UPDATE: Please see also <a href="https://stackoverflow.com/a/22262689/77567">my other answer</a> which describes how to set up multiple independent, overlapping holes.)</p> <p>Let's use a plain old <code>UIView</code> with a <code>backgroundColor</code> of translucent black, and give its layer a mask that cuts a hole out of the middle. We'll need an instance variable to reference the hole view:</p> <pre><code>@implementation ViewController { UIView *holeView; } </code></pre> <p>After loading the main view, we want to add the hole view as a subview:</p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; [self addHoleSubview]; } </code></pre> <p>Since we want to move the hole around, it will be convenient to make the hole view be very large, so that it covers the rest of the content regardless of where it's positioned. We'll make it 10000x10000. (This doesn't take up any more memory because iOS doesn't automatically allocate a bitmap for the view.)</p> <pre><code>- (void)addHoleSubview { holeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10000, 10000)]; holeView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.5]; holeView.autoresizingMask = 0; [self.view addSubview:holeView]; [self addMaskToHoleView]; } </code></pre> <p>Now we need to add the mask that cuts a hole out of the hole view. We'll do this by creating a compound path consisting of a huge rectangle with a smaller circle at its center. We'll fill the path with black, leaving the circle unfilled and therefore transparent. The black part has alpha=1.0 and so it makes the hole view's background color show. The transparent part has alpha=0.0, so that part of the hole view is also transparent.</p> <pre><code>- (void)addMaskToHoleView { CGRect bounds = holeView.bounds; CAShapeLayer *maskLayer = [CAShapeLayer layer]; maskLayer.frame = bounds; maskLayer.fillColor = [UIColor blackColor].CGColor; static CGFloat const kRadius = 100; CGRect const circleRect = CGRectMake(CGRectGetMidX(bounds) - kRadius, CGRectGetMidY(bounds) - kRadius, 2 * kRadius, 2 * kRadius); UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:circleRect]; [path appendPath:[UIBezierPath bezierPathWithRect:bounds]]; maskLayer.path = path.CGPath; maskLayer.fillRule = kCAFillRuleEvenOdd; holeView.layer.mask = maskLayer; } </code></pre> <p>Notice that I've put the circle at the center of the 10000x10000 view. This means that we can just set <code>holeView.center</code> to set the center of the circle relative to the other content. So, for example, we can easily animate it up and down over the main view:</p> <pre><code>- (void)viewDidLayoutSubviews { CGRect const bounds = self.view.bounds; holeView.center = CGPointMake(CGRectGetMidX(bounds), 0); // Defer this because `viewDidLayoutSubviews` can happen inside an // autorotation animation block, which overrides the duration I set. dispatch_async(dispatch_get_main_queue(), ^{ [UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations:^{ holeView.center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMaxY(bounds)); } completion:nil]; }); } </code></pre> <p>Here's what it looks like:</p> <p><img src="https://i.stack.imgur.com/ghd9D.gif" alt="hole animation"></p> <p>But it's smoother in real life.</p> <p>You can find a complete working test project <a href="https://github.com/mayoff/stackoverflow-15375777-ios-animated-hole" rel="nofollow noreferrer">in this github repository</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload