0

I want to convert a rectangle from another collectionview (cellContentCollectionView) to an equal rectangle in my root view controller.

The instance method used to do this is convert(_:to:), however I am having trouble setting the frame of the UIView in the root view controllers frame.

Here's what I have so far...

cellContentCollectionView?.convert((playerLayer?.frame)!, to: fullScreenPlayerView)

cellContentCollectionView is the collectionView that it's located in. Which happens to be in a collectionViewCell.

Any suggestions?

Stefan
  • 822
  • 1
  • 10
  • 26
  • convert method returns a new CGRect you must use it, `let newFrame = cellContentCollectionView?.convert((playerLayer?.frame)!, to: fullScreenPlayerView)` – Reinier Melian Jun 17 '17 at 19:25
  • What is the point of the second parameter [to View: UIView?] then? I'm a little confused – Stefan Jun 17 '17 at 19:30
  • is the destination view for conversion, the view were you need to put your your rectangle, you need convert `playerLayer?.frame` from `cellContentCollectionView` to `fullScreenPlayerView ` coordinate system – Reinier Melian Jun 17 '17 at 19:32
  • Alright, that makes sense. I'm still having an issue when I set fullScreenPlayerView.bounds = (newFrame)! I get the bad instructions error because it found nil. – Stefan Jun 17 '17 at 19:37
  • the origin and destination both views must be inside the same view tree, if not this method can return nil – Reinier Melian Jun 17 '17 at 19:41
  • I believe they are, one is in the root view controller. The other is in a UICollectionView cell, inside a UICollectionView, inside another UICollectionView cell... Do you think that might be why it's returning nil? – Stefan Jun 17 '17 at 19:45
  • check this answer https://stackoverflow.com/questions/8465659/understand-convertrecttoview-convertrectfromview-convertpointtoview-and and this https://stackoverflow.com/questions/3219331/convert-a-uiview-origin-point-to-its-window-coordinate-system maybe can help you – Reinier Melian Jun 17 '17 at 20:08

2 Answers2

0

You didn't explain the exact relationship between cellContentCollectionView and playerLayer, so I'm going to assume the worst: that playerLayer is not a subview of cellContentCollectionView.

I also suspect that playerLayer is not actually a view at all, but is a CALayer. That's okay, because on iOS, a view's frame is always its layer.frame, so we can just deal with the layers.

The frame of a layer is in the geometry of its superlayer. So you must ask its superlayer to convert its frame. But, if the layer isn't transformed, you could instead ask the layer to convert its own bounds instead.

If you intend to use the resulting rectangle as the frame of a target layer, you must convert to the target layer's superlayer. You can't just convert to the target layer's geometry and set the target layer's bounds, because that doesn't update the position of the target layer.

Thus I think you want to write this:

if
    let playerLayer = playerLayer,
    let fullScreenPlayerSuper = fullScreenPlayerView.layer.superlayer
{
    let frame = playerLayer.convert(playerLayer.bounds, to: fullScreenPlayerSuper)
    fullScreenPlayerView.frame = frame
}
rob mayoff
  • 342,380
  • 53
  • 730
  • 766
0

This is how I would do it, assuming that playerLayer is a UIView.

guard let playerLayer = playerLayer, superview = playerLayer.superview else { return }

fullScreenPlayerView.convert(playerLayer.frame, from: superview)
Infinity James
  • 4,376
  • 4
  • 19
  • 34
  • playerLayer is an AVPlayerLayer the reason I referenced it was because I am going to need it's visual output anyways. However it is masked to a UIView so I'll try using the UIView with your code – Stefan Jun 17 '17 at 21:20
  • Your code doesn't produce anything. It's as if it's producing nil, but it doesn't say it. – Stefan Jun 17 '17 at 21:27