UIRectCornerBottomLeft/Right 的 CAShapeLayer 使我的视图消失

CAShapeLayer with UIRectCornerBottomLeft/Right makes my view disappear

好的。我已经完成了 Google 搜索、Stack 搜索、Wenderlich 搜索(那是模因吗?)。我已经在溢出时尝试了这里的所有东西,但没有任何效果。

我需要一个 CALayer 专家。

我有一个 table视图,我将其显示为应用程序顶部的下拉菜单。在我尝试将底角 left/right 圆化之前,它显示得很好而且漂亮。我尝试这样做的每一次尝试都导致我的 table 完全消失。这些方法仍然有效,我完成了 up/down 方法。然而 table 就这样消失了。

具有 table视图的代码:

- (void)setupTopMenu {
    CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;

    self.topMenuTable = [[UITableView alloc] initWithFrame:CGRectMake((screenWidth / 2) - ((screenWidth - topMenuSideBuffer) / 2), self.view.frame.origin.y + topBuffer, (screenWidth - topMenuSideBuffer), 0) style:UITableViewStylePlain];
    self.topMenuTable.backgroundColor = DARK_BLUE_NAVBAR_COLOR;
    self.topMenuTable.dataSource = self;
    self.topMenuTable.delegate = self;
    self.topMenuTable.scrollEnabled = YES;
    self.topMenuTable.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.topMenuTable.separatorColor = [UIColor blackColor];
    self.topMenuTable.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.topMenuTable.layer.borderColor = [UIColor blackColor].CGColor;
    self.topMenuTable.layer.borderWidth = 0.4f;

    UIBezierPath *maskPath;
    maskPath = [UIBezierPath bezierPathWithRoundedRect:self.topMenuTable.bounds byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(5.0, 5.0)];

    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.view.bounds;
    maskLayer.path = maskPath.CGPath;
    self.topMenuTable.layer.mask = maskLayer;

    [self.view addSubview:self.topMenuTable];
}

只有一个视图的代码(我认为它可能与 tableView 有关,但当我使用常规 UIView 时它的行为相同)

- (void)setupTopMenu {
    CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;

    self.topMenuView = [[UIView alloc] initWithFrame:CGRectMake((screenWidth / 2) - ((screenWidth - topMenuSideBuffer) / 2), self.view.frame.origin.y + topBuffer, (screenWidth - topMenuSideBuffer), 0)];
    self.topMenuView.backgroundColor = [UIColor redColor];
    self.topMenuView.layer.borderColor = [UIColor blackColor].CGColor;
    self.topMenuView.layer.borderWidth = 0.4f;

    UIBezierPath *maskPath;
    maskPath = [UIBezierPath bezierPathWithRoundedRect:self.topMenuView.bounds byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(5.0, 5.0)];

    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.view.bounds;
    maskLayer.path = maskPath.CGPath;
    self.topMenuView.layer.mask = maskLayer;

    [self.view addSubview:self.topMenuView];
}

我用来显示视图的代码:

- (void)showTopMenu {

    if (!isTopMenuDown) {
        [UIView animateWithDuration:0.45 animations:^ {
            //self.topMenuTable.frame = CGRectMake((self.screenWidth / 2) - (self.topMenuWidth / 2), self.view.bounds.origin.y + topBuffer, self.topMenuWidth, self.topMenuHeight);
            self.topMenuView.frame = CGRectMake((self.screenWidth / 2) - (self.topMenuWidth / 2), self.view.bounds.origin.y + topBuffer, self.topMenuWidth, self.topMenuHeight);
        } completion:^(BOOL finished) {

        }];
    } else {
        [UIView animateWithDuration:0.35 animations:^ {
            // self.topMenuTable.frame = CGRectMake((self.screenWidth / 2) - (self.topMenuWidth /2), self.view.bounds.origin.y + topBuffer, self.topMenuWidth, 0);
            self.topMenuView.frame = CGRectMake((self.screenWidth / 2) - (self.topMenuWidth /2), self.view.bounds.origin.y + topBuffer, self.topMenuWidth, 0);
        } completion:^ (BOOL finished) {

        }];
    }

    isTopMenuDown =! isTopMenuDown;
}

注意:我也尝试过像 Round some corners of UIView and round the view’s layer’s border too

那样将蒙版添加到图层的想法

同样的结果。

如果我去掉图层代码,视图会完美显示,但使用图层代码会使它们消失。

我显示我看不到的视图的方式是否导致它消失了?

感谢您的帮助。

I thought maybe it had something to do with the tableView but it behaves the same when I use a regular UIView

确实如此 - 因为它与 table 视图无关。跟你不懂什么是口罩有关

遮罩层根据遮罩层的 透明度 遮挡部分图层。您没有采取任何措施来区分蒙版的不透明部分和透明部分。你的整个面具是透明的。因此,层的所有 - 即所有视图 - 都被遮挡(不可见)。

我觉得你描述的是这样的(我夸大了角落的圆角,只是为了显示目的):

那是一个长方形,上面有一个面具。掩码由 filled 圆角矩形路径组成。这样,路径的interior是不透明的,出现了;外表是透明的,不会出现

仅供参考,这是我创建和添加蒙版的方式(我使用了图像和 contents,但如果您愿意,您当然可以自由使用形状图层):

UIView* viewToMask = self.view.subviews[0];
UIGraphicsBeginImageContextWithOptions(viewToMask.bounds.size, NO, 0);
UIBezierPath* round = [UIBezierPath bezierPathWithRoundedRect:viewToMask.bounds byRoundingCorners: (UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(50,50)];
[round fill];
UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CALayer* mask = [CALayer new];
mask.frame = viewToMask.bounds;
mask.contents = (id)result.CGImage;
viewToMask.layer.mask = mask;