Clipping subview drawing to arbitrary path or image (iPhone)
-
Hi. We have a requirement to clip a set of subviews' drawing to an arbitrary path or clip image specified by a containing (parent) view. Is there any way to accomplish this?
UIView, not NSView.
TIA,
Rick -
On Jan 15, 2010, at 2:31 PM, Rick Mann wrote:
> Hi. We have a requirement to clip a set of subviews' drawing to an arbitrary path or clip image specified by a containing (parent) view. Is there any way to accomplish this?
Do the subviews move? If not, it would seem that the best way to deal with that would be to pass the same clip to each of the subviews and have them apply that clip before they draw (with proper transforms of course).
If not, this could be done (with performance penalty) using the layer's mask property. Simplest way I can think is to make a new layer, assign the contents of the parent layer to that new layer's contents, then assign that layer as the mask of all subviews. This may require a bit of futzing around to get right however.
--
David Duncan
Apple DTS Animation and Printing -
On Jan 15, 2010, at 15:53:29, David Duncan wrote:
> On Jan 15, 2010, at 2:31 PM, Rick Mann wrote:
>
>> Hi. We have a requirement to clip a set of subviews' drawing to an arbitrary path or clip image specified by a containing (parent) view. Is there any way to accomplish this?
>
>
> Do the subviews move? If not, it would seem that the best way to deal with that would be to pass the same clip to each of the subviews and have them apply that clip before they draw (with proper transforms of course).
>
> If not, this could be done (with performance penalty) using the layer's mask property. Simplest way I can think is to make a new layer, assign the contents of the parent layer to that new layer's contents, then assign that layer as the mask of all subviews. This may require a bit of futzing around to get right however.
The subviews will consist of a UIScrollView and a set of UIButtons. Generally, I'd like to avoid subclassing them, if possible. I'm implementing a control with behaviors very similar to a horizontal UIPickerView. Unfortunately, it lives in a non-rectangular shape, and the surrounding elements are transparent and should NOT show the control underneath.
I'll look into your suggestion. Thanks!
--
RIck -
On Jan 15, 2010, at 4:10 PM, Rick Mann wrote:
>> If not, this could be done (with performance penalty) using the layer's mask property. Simplest way I can think is to make a new layer, assign the contents of the parent layer to that new layer's contents, then assign that layer as the mask of all subviews. This may require a bit of futzing around to get right however.
>
> The subviews will consist of a UIScrollView and a set of UIButtons. Generally, I'd like to avoid subclassing them, if possible. I'm implementing a control with behaviors very similar to a horizontal UIPickerView. Unfortunately, it lives in a non-rectangular shape, and the surrounding elements are transparent and should NOT show the control underneath.
I don't see any reason why the mask property wouldn't work for that, as log as the performance is acceptable to you (I believe using a mask will cause an offscreen render to occur). That said, if you can possibly invert this (that is, invert the mask and draw the content *above* the others) you should have better performance.
That and I think I spoke a little fast on the exact usage of the mask – iirc, you should only need to apply it to a common parent and it should affect all children as well.
--
David Duncan
Apple DTS Animation and Printing -
On Jan 15, 2010, at 16:16:15, David Duncan wrote:
> On Jan 15, 2010, at 4:10 PM, Rick Mann wrote:
>
>>> If not, this could be done (with performance penalty) using the layer's mask property. Simplest way I can think is to make a new layer, assign the contents of the parent layer to that new layer's contents, then assign that layer as the mask of all subviews. This may require a bit of futzing around to get right however.
>>
>> The subviews will consist of a UIScrollView and a set of UIButtons. Generally, I'd like to avoid subclassing them, if possible. I'm implementing a control with behaviors very similar to a horizontal UIPickerView. Unfortunately, it lives in a non-rectangular shape, and the surrounding elements are transparent and should NOT show the control underneath.
>
>
> I don't see any reason why the mask property wouldn't work for that, as log as the performance is acceptable to you (I believe using a mask will cause an offscreen render to occur). That said, if you can possibly invert this (that is, invert the mask and draw the content *above* the others) you should have better performance.
>
> That and I think I spoke a little fast on the exact usage of the mask – iirc, you should only need to apply it to a common parent and it should affect all children as well.
Hmm. I tried some experiments with your suggestion, and seemed to be getting masking with the inverse of what I specified. So I looked at the docs for the mask property, and there's this note: "iPhone OS Note: As a performance consideration, iPhone OS does not support the mask property."
It's clearly doing something, but it's not clear that it's doing what I want. Oh. It's working in the simulator, I bet it won't work on the phone, and it still causes a problem: it doesn't make the masked-out areas transparent.
Thanks, though.
--
Rick -
On Jan 15, 2010, at 5:07 PM, Rick Mann wrote:
> Hmm. I tried some experiments with your suggestion, and seemed to be getting masking with the inverse of what I specified. So I looked at the docs for the mask property, and there's this note: "iPhone OS Note: As a performance consideration, iPhone OS does not support the mask property."
I'm not certain where you see that comment, but it is incorrect (with respect to iPhone OS 3.x – the mask property did not exist on 2.x).
As for the masking operation, it multiplies the alpha from the layer's contents against the layer to be masked, so if you have a mask filled with 0.75 alpha, then the layer's contents will be multiplied against 0.75. I suppose if you expect 0.75 to pass 25% of the image through, then yes it is opposite of what you expect :).
> It's clearly doing something, but it's not clear that it's doing what I want. Oh. It's working in the simulator, I bet it won't work on the phone, and it still causes a problem: it doesn't make the masked-out areas transparent.
I tested on an iPod Touch 2nd Gen and it works fine there, but keep in mind that the masking layer needs to be in the same coordinate space as the layer masked. Simplest way to do this is to assign the masking layer's frame the value of the layer to be mask's frame.
--
David Duncan
Apple DTS Animation and Printing -
On Jan 16, 2010, at 21:23:16, David Duncan wrote:
> I'm not certain where you see that comment, but it is incorrect (with respect to iPhone OS 3.x – the mask property did not exist on 2.x).
Scroll to the bottom of this page:
http://developer.apple.com/Mac/library/documentation/Cocoa/Conceptual/CoreA
nimation_guide/Articles/LayerVisProps.html#//apple_ref/doc/uid/TP40006074



