12

我正在尝试在 UIVisualEffectView 上创建一种透明的孔视图。我正在关注这里给出的解决方案。我附上了我在这里工作的示例代码。我正在尝试创建一个透明视图,其框架取自模糊视图后面的图像视图。任何人都可以告诉我我在这里做错了什么?

4

2 回答 2

13

您可以使用 aUIBezierPath创建所需的蒙版。

blurView.layer.mask = ({
    CGRect roundedRect = self.bounds;
    roundedRect.origin.x = roundedRect.size.width / 4.0f;
    roundedRect.origin.y = roundedRect.size.height / 4.0f;
    roundedRect.size.width /= 2.0f;
    roundedRect.size.height /= 2.0f;

    CGFloat cornerRadius = roundedRect.size.height / 2.0f;

    UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.bounds];
    UIBezierPath *croppedPath = [UIBezierPath bezierPathWithRoundedRect:roundedRect cornerRadius:cornerRadius];
    [path appendPath:croppedPath];
    [path setUsesEvenOddFillRule:YES];

    CAShapeLayer *mask = [CAShapeLayer layer];
    mask.path = path.CGPath;
    mask.fillRule = kCAFillRuleEvenOdd;
    mask;
});
于 2015-11-10T09:34:20.923 回答
2

Swift 5 版本的懒人解决方案 :)

    var roundedRect = visualEffectsView.bounds
    roundedRect.origin.x = roundedRect.size.width / 4
    roundedRect.origin.y = roundedRect.size.height / 4
    roundedRect.size.width = roundedRect.size.width / 2
    roundedRect.size.height = roundedRect.size.height / 2

    let cornerRadius = roundedRect.size.height / 2

    let path = UIBezierPath(rect: visualEffectsView.bounds)
    let croppedPath = UIBezierPath(roundedRect: roundedRect, cornerRadius: cornerRadius)
    path.append(croppedPath)
    path.usesEvenOddFillRule = true

    let mask = CAShapeLayer()
    mask.path = path.cgPath
    mask.fillRule = .evenOdd
    visualEffectsView.layer.mask = mask
于 2019-05-22T07:21:18.127 回答