Cut Out Shape with Animation

后端 未结 5 1372
盖世英雄少女心
盖世英雄少女心 2020-11-30 19:11

I want to do something similar to the following:

How to mask an image in IOS sdk?

I want to cover the entire screen with translucent black. Then, I want to c

5条回答
  •  时光取名叫无心
    2020-11-30 19:43

    (UPDATE: Please see also my other answer which describes how to set up multiple independent, overlapping holes.)

    Let's use a plain old UIView with a backgroundColor 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:

    @implementation ViewController {
        UIView *holeView;
    }
    

    After loading the main view, we want to add the hole view as a subview:

    - (void)viewDidLoad {
        [super viewDidLoad];
        [self addHoleSubview];
    }
    

    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.)

    - (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];
    }
    

    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.

    - (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;
    }
    

    Notice that I've put the circle at the center of the 10000x10000 view. This means that we can just set holeView.center 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:

    - (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];
        });
    }
    

    Here's what it looks like:

    hole animation

    But it's smoother in real life.

    You can find a complete working test project in this github repository.

提交回复
热议问题