我正在展示一个带有自定义 presentationStyle 的 View Controller 。父 Controller 是一个 UICollectionViewController。在我的 UICollectionViewController 的 didSelectAtIndexPath 方法中,我获取所选单元格的 UIImageView 并将其动画化到模态呈现的 View Controller 中 UIView 框架的位置。
如何获取模态呈现的 View Controller 的 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
完成 block (您当前正在传递 nil
的位置)内执行此操作。此时,所有的 frame
都将被正确设置。
此外,这种方法只有在它们的父 View 在坐标系中放置相同时才有效。请记住,框架的原点
(和 View 的中心
)是相对于它们的父 View 的坐标。如果它们不相同,则需要使用 UIView
的 convertRect(_:toView:)
或 convertRect(_:fromView:)
将 rect 转换为适当的 View 坐标系。
关于ios - 如何在模态呈现的 viewController 中获取 UIView 的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27557660/