掩蔽的CALayer - iPhone [英] Masking a CALayer - iPhone

查看:122
本文介绍了掩蔽的CALayer - iPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的开/关切换开​​关为iPhone(类似于标准交换机)的自定义。我在那里我设置滑块的面具点,但调用 [[MyView的层] setMask:maskLayer] 设置相对于它的掩蔽层,不相对于掩模层的容器层掩模层的位置。对于这种特殊的情况下,所需要的掩模层的位置的含有层因为肘节滑块将掩模下方移动和掩模应保持静止的相对于它被设置

I'm creating a custom on / off toggle switch for the iPhone (similar to the standard switch) and i'm at the point where I'm setting the mask of the slider, but calling [[myView layer] setMask:maskLayer] sets the position of the mask layer relative to the layer it's masking, not relative to the container layer of the mask layer. For this particular scenario, the position of the mask layer needs to be set relative to it's containing layer because the toggle slider will move underneath the mask and the mask should stay stationary.

而不必动画遮罩和切换滑块组件来达到预期的效果,谁知道如何使这项工作?下面是我到目前为止有:

Without having to animate the mask AND the toggle slider component to achieve the desired effect, anyone know how to make this work? Here's what I've got so far:

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
CGMutablePathRef path = CGPathCreateMutable();

CGPathAddEllipseInRect(path, nil, CGRectMake(0, 0, 13, 13));
CGPathAddEllipseInRect(path, nil, CGRectMake(30, 0, 13, 13));
CGPathAddRect(path, nil, CGRectMake(6, 0, 32, 13));

[maskLayer setPath:path];
[maskLayer setFillColor:[[UIColor greenColor] CGColor]];

[[self layer] addSublayer:maskLayer];

[maskLayer setPosition:CGPointMake(2, 2)];

[self addSubview:toggleView];

[[toggleView layer] setMask:maskLayer];


推荐答案

一个掩模只是一种特殊的子层,所以掩模的superlayer永远是它的掩蔽层。有没有办法解决它。此外,你违反了这个警告的文件中:

A mask is just a special kind of sublayer, so the superlayer of the mask will always be the layer it's masking. There is no way around it. Also, you are violating this warning in the documentation:

在设定面具一个新图层,新图层的superlayer首先必须设置为零,否则该行为是不确定的。

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

您应该删除 [自层] addSublayer:maskLayer];

要解决你的问题,我会创造另一层或视图包含的一切,现在是你的toggleView,然后将该层添加到您的toggleLayer作为一个子层。这样,你可以独立定位新的子层的面膜,如果你移动父层,既面具和子层将一起移动(这是,如果我理解正确,他们应该做的)。

To solve your problem, I would create another layer or view that contains everything that is now on your toggleView and then add that layer to your toggleLayer as a sublayer. That way, you can position the new sublayer independently of the mask and if you move the parent layer, both the mask and the sublayer will move together (which is what they should do if I understand you correctly).

这篇关于掩蔽的CALayer - iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆