正确屏蔽 CALayers

发布于 12-23 11:38 字数 885 浏览 6 评论 0原文

我正在使用 TapkuLibrary 的其他优秀 TKCalendarDayEventView 并尝试有选择地将视图中的一个角圆化为 StuDev 在此演示。不幸的是,应用 StuDev 的代码片段会导致 EventView 从其包含的 TKCalendarDayTimelineView 中完全消失。我将此代码片段添加到

+ (id)eventViewWithFrame:(CGRect)frame id:(NSNumber *)id startDate:(NSDate *)startDate endDate:(NSDate *)endDate title:(NSString *)标题位置:(NSString *)位置; 方法

。我已经注释掉了代码中设置边框宽度、颜色或半径的代码。我已确保 TKCalendarDayEventView 没有任何超级层,因为 苹果文档警告不要向具有超级图层的图层添加蒙版:

为新图层设置遮罩时,必须首先将新图层的超级图层设置为 nil,否则行为未定义。

我还尝试过使用 maskLayer 的 backgroundColor 和 fillColor 属性。我在 TKCalendarDayEventView 中没有看到任何可能阻止正确应用此掩码的内容。我可能做错了什么?

I am using the TapkuLibrary's otherwise excellent TKCalendarDayEventView and trying to selectively round one of the corners in the view as StuDev demonstrates here. Unfortunately, applying StuDev's code snippet results in the EventView disappearing entirely from its containing TKCalendarDayTimelineView. I am adding this code snippet underneath the current code in the

+ (id)eventViewWithFrame:(CGRect)frame id:(NSNumber *)id startDate:(NSDate *)startDate endDate:(NSDate *)endDate title:(NSString *)title location:(NSString *)location;

method. I have commented out code that otherwise sets the border width, color, or radius in the code. I have made sure that TKCalendarDayEventView doesn't have any superlayers, since the
apple docs warn against adding masks to layers with superlayers:

When setting the mask to a new layer, the new layer’s superlayer must first be set to nil, otherwise the behavior is undefined.

I have also tried playing around with the backgroundColor and fillColor properties of the maskLayer. I don't see anything in the TKCalendarDayEventView that might stop this mask from being correctly applied. What could I be doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感性不性感2024-12-30 11:38:40

如果您在 eventViewWithFrame:id:startDate:endDate:title:location: 方法中放置断点,您将看到在创建事件视图时将框架设置为 CGRectZero代码>.然后设置圆角蒙版的代码片段使用 CGRectZero 作为蒙版层的框架。

处理这个问题的最简单方法可能是重写 TKCalendarDayEventViewsetFrame: 方法,如下所示:

- (void)setFrame:(CGRect)newFrame
{
    if (!CGRectEqualToRect([super frame], newFrame)) {
        [super setFrame:newFrame];

        // Change the view's mask layer to fit the new frame.
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds 
                                                       byRoundingCorners:UIRectCornerTopLeft
                                                             cornerRadii:CGSizeMake(15.0, 15.0)];
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.frame = self.bounds;
        maskLayer.path = maskPath.CGPath;
        self.layer.mask = maskLayer;
    }
}

这样,每次更改视图的框架时,蒙版都会自动调整。

If you put a breakpoint in your eventViewWithFrame:id:startDate:endDate:title:location: method, you will see that when you create your event view you are setting the frame to CGRectZero. The code snippet that then sets the rounded corner mask is using CGRectZero as the mask layer's frame.

Probably the simplest way to deal with this would be to override TKCalendarDayEventView's setFrame: method like so:

- (void)setFrame:(CGRect)newFrame
{
    if (!CGRectEqualToRect([super frame], newFrame)) {
        [super setFrame:newFrame];

        // Change the view's mask layer to fit the new frame.
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds 
                                                       byRoundingCorners:UIRectCornerTopLeft
                                                             cornerRadii:CGSizeMake(15.0, 15.0)];
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.frame = self.bounds;
        maskLayer.path = maskPath.CGPath;
        self.layer.mask = maskLayer;
    }
}

This way, every time you change the frame of the view the mask automatically adjusts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文