如何旋转CollectionView Cell以使项目保持居中?

时间:2016-03-29 16:38:44

标签: ipad uicollectionview ios9 uicollectionviewcell

我在iPad应用中设置了一个UIcollectionView,可以一次显示一张照片。当我从横向中的图像旋转时,我想让图像调整大小,以便旋转开始时显示的图像在旋转以纵向结束时居中。调整大小有效,但旋转结束时会显示两个项目之间的边框。我不知道如何保持图像居中,就像我想的那样。

也许“居中”是错误的方式,也许我的意思是保持图像的左侧在框架的左侧。作为iOS和Swift的新手,我的问题的一部分是我不知道甚至框架(没有双关语)正确的问题。

This image显示了问题以及我想要实现的目标。

是否有可能做我想做的事情,如果有的话,最好的方法是什么?

以下是目前的代码:

class PhotoCollectionViewController: UICollectionViewController {

    var businessData = [BData]()
    var theBusiness:BData?
    var currentDevice: UIDevice = UIDevice.currentDevice()


    override func viewDidLoad() {
        super.viewDidLoad()
        definesPresentationContext = true
        businessData = appData.getBusinessData()
    }

    override func numberOfSectionsInCollectionView(collectionView:
        UICollectionView) -> Int {
        return 1
    }

    override func collectionView(collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
        return businessData.count
    }


    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("photoCell", forIndexPath: indexPath) as! PhotoCell

        // Configure the cell

        let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
        let docsDir = dirPaths[0]
        let theBusinessPhoto = docsDir+"/bPhotos/"+businessData[indexPath.row].bPhotoFilename!
        cell.splashPhoto.image = UIImage(contentsOfFile: theBusinessPhoto)

        return cell
    }

    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        if let cell = collectionView.cellForItemAtIndexPath(indexPath) {
            performSegueWithIdentifier("gotoBusinessInfo", sender: cell)
        } else {
            // Error.
        }
    }

    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
        showViewSettings()
        self.collectionView!.collectionViewLayout.invalidateLayout()
        view.layoutIfNeeded()
    }

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

        if (currentDevice.orientation.isLandscape){
            return CGSizeMake(1024, 704)
        } else {
            return CGSizeMake(768, 528)
        }
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        assert(sender as? UICollectionViewCell != nil, "sender is not a collection view")

        if let indexPath = self.collectionView?.indexPathForCell(sender as! UICollectionViewCell) {
            if segue.identifier == "gotoBusinessInfo" {
                let businessDetailVC = segue.destinationViewController as! BusinessInfoVC
                businessDetailVC.businessData = businessData[indexPath.row]
            }

        } else {

            // Error.
        }
    }

    func showViewSettings(){
        print ("Device......: " + UIDevice.currentDevice().name)
        print ("Model.......: " + UIDevice.currentDevice().localizedModel)
        print ("Orientation.: " + String(UIDevice.currentDevice().orientation.rawValue))
        print ("View width..: " + String(self.view.bounds.size.width))
        print ("View height.: " + String(self.view.bounds.size.height))
        print ("S.bar height: " + String(UIApplication.sharedApplication().statusBarFrame.height))
        print ("S.bar hidden: " + String(UIApplication.sharedApplication().statusBarHidden))
        print ("N.bar height: " + String(self.navigationController?.navigationBar.frame.height))
    }

}

showViewSettings()只是一个调试辅助工具。

0 个答案:

没有答案