Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I am not sure I fully understand your description of the problem, so a dummy app would be really helpful. But here's my analysis from what I can seen now:</p> <p>You say that you have two UIImageViews, Background and MaskImage. From this, and from your code, I understand that what you want to do is:</p> <ol> <li>get a CGPath that represents the actual shape of MaskImage, after various transforms have been applied, from the point of view of (presumably) a superview or some higher-than-superview. </li> <li>Use that CGPath to define a CAShapeLayer, which you will use as the mask on the backing layer of some other view, presumably the Background UIImageView</li> </ol> <p>In that case, MaskImage in your text corresponds to your code's <code>currentView</code> (since that's where you're getting the transform), and Background in your text corresponds to your code's <code>currentImageView</code> (since that's where you're changing the layer's mask). Right?</p> <p>If all of that is correct, then there are two key problems with your code:</p> <ol> <li><p>You want to get the a CGPath representing the <em>transformed shape</em> of currentView. But you're grabbing <code>currentView.frame</code>. This is wrong. <code>currentView.frame</code> will represent the smallest rect, in the coordinates of <code>currentView.superview</code>, which can contain the transformed shape of currentView. So, for instance, if currentView is a square rotated by 45 degrees, then currentView.frame is still a square, just a larger square. Instead, what you should be doing is getting <code>currentView.bounds</code>, which is the rect that represents how currentView's shape in its own coordinate system, generating a path from that, and then apply <code>currentView.transform</code> to that path.</p></li> <li><p>You don't specify anywhere, in your code or text, how to map from the coordinate system of <code>currentView.superview</code> to the coordinate system of MaskImage. You also need to do this before applying the shape to MaskImage.layer.mask.</p></li> </ol> <p>For instance, I can imagine an application where the screen was cut into two equal side left and right, which presented a large photo image on the left side of the screen, and on the right side of the screen it allowed you to pan, rotate, and scale an image of a black rectangle. Then the app would use the black rectangle on the right-hand side to define a CAShapeLayer that was applied as a mask over the image on the left-hand side. This sounds like what you're going for. So what I'm saying is, that the logical set of steps you need for this to work is:</p> <ol> <li>Get a CGPath for the <em>bounds</em> of the black rect, which will be an untransfored rect.</li> <li>Transform this CGPath with the same transform being applied on the black rectangle.</li> <li>Represent this CGPath in the coordinate system of a rect containing just the right-hand side of the screen. Since the right-hand and left-hand sides are congruent, it is now also in the coordinate system that can be directly applied to the left-hand side of the screen.</li> <li>Use the CGPath to define a CALayer, apply this CALayer to the view representing the left-hand side of the screen, and call setNeedsDisplay on that view's layer.</li> </ol> <p>P.S. By the way, there's a third problem. Note that, technically, when you add a .mask to a layer, that layer should not have a superlayer. So you shouldn't add a .mask to the backing layer of a UIView already in the view hierarchy, since it will have a super layer.So, I believe, what you should do is temporarily remove the UIView from the view hierarchy, add the mask layer, and then re-add the view to the view hierarchy.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload