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

标签 ios objective-c iphone swift

我正在展示一个带有自定义 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 的坐标。如果它们不相同,则需要使用 UIViewconvertRect(_:toView:)convertRect(_:fromView:) 将 rect 转换为适当的 View 坐标系。

关于ios - 如何在模态呈现的 viewController 中获取 UIView 的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27557660/

相关文章:

ios - 禁用按钮

iphone - 禁用 UIButton 直到 UITextField 中有文本

ios - 如何在 alamofire 中快速打印错误 html?

objective-c - iOS中的本地化

ios - 我们可以使用 Facebook SDK 将文档目录视频上传到 Facebook 吗?

iphone - 如何在 objective-c 中声明指针数组

ios - 如何在 CollectionView 中选择单个单元格并取消选择所有其他单元格

ios - 在 iphone 应用程序中集成 vine?

iphone - 我想检查 sqlite 表上是否存在主键

ios - 无法使用类型为 '[NSObject : AnyObject]' 的索引下标类型为 'NSKeyValueChangeKey' 的值