2014-12-18 38 views
0

我正在用自定義的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 
    }) }) 
} 

回答

1

,而不是試圖立即製作動畫,這樣做的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

相關問題