如何获取UIView在模态呈现viewController的位置

问题描述:

我正在用自定义的presentationStyle呈现一个viewcontroller。父控制器是一个UICollectionViewController。在我的UICollectionViewController的didSelectAtIndexPath方法中,我选取了所选单元格的UIImageView并将其动画化为模态呈现的视图控制器内的UIView框架的位置。如何获取UIView在模态呈现viewController的位置

我怎样才能得到UIView的模态呈现视图控制器的框架?

self.imageView!.frame = trophyOverlayVC.trophyImageLocationView!.frame 

不起作用。

它在大多数情况下出现在屏幕外。

import UIKit 

class CollectionHolderViewController: MainPageContentViewController, UICollectionViewDelegate, UICollectionViewDataSource { 

    [..insert other methods here..] 

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("collectionCell", forIndexPath: indexPath) as TrophyCollectionViewCell 

     self.collectionViewController.collectionView!.scrollEnabled = false // disable scrolling so view won't move 
     self.collectionViewController.collectionView!.userInteractionEnabled = false 
     let innerOffset = collectionView.contentOffset as CGPoint // offset of content view due to scrolling 
     let attributes = self.collectionViewController.collectionView!.layoutAttributesForItemAtIndexPath(indexPath) as UICollectionViewLayoutAttributes! 
     let cellRect = attributes.frame // frame of cell in contentView 

     imageView = UIImageView(frame: CGRectMake(cellRect.origin.x, cellRect.origin.y, cellRect.size.width, cellRect.size.height)) //places image in location of collectionViewCell. 
     imageView!.image = UIImage(named: placeHolderArray[indexPath.row]) //change. 
     imageView!.contentMode = UIViewContentMode.ScaleAspectFit 

     self.collectionViewController.collectionView!.addSubview(imageView!) 


     self.placeHolderArray[indexPath.row] = "" 
     UIView.setAnimationsEnabled(false) 
     self.collectionViewController.collectionView!.reloadItemsAtIndexPaths([indexPath]) 
     UIView.setAnimationsEnabled(true) 

     var trophyOverlayVC = self.collectionViewController.storyboard!.instantiateViewControllerWithIdentifier("TrophyOverlayVC") as TrophyOverlayViewController 
     trophyOverlayVC.modalPresentationStyle = UIModalPresentationStyle.Custom 
     self.presentViewController(trophyOverlayVC, animated: false, completion: self.presentViewController(trophyOverlayVC, animated: false, completion: { (value: Void) in UIView.animateWithDuration(0.5, animations: { 
     self.imageView!.frame = trophyOverlayVC.trophyImageLocationView!.frame 
     }, completion: { 
      (value: Bool) in 
    }) }) 
} 

,而不是试图立即制作动画,这样做的presentViewController完成块内(在您目前正在通过nil)。此时,所有frame将被正确设置。

此外,这种方法只有在它们的超级视图在坐标系统中一致时才有效。请记住,框架的origin(和视图的center)是关于其超级视图的坐标。如果它们不相同,则需要使用UIViewconvertRect(_:toView:)convertRect(_:fromView:)将矩形转换为相应视图的坐标系。

+0

好吧,所以我现在有动画发生在完成块。这并没有解决问题,所以我想我需要使用convertRect。 – CaptainCOOLGUY 2014-12-19 00:18:21

+0

任何人都可以使用我发布的代码提供示例吗? – CaptainCOOLGUY 2014-12-19 00:40:35