vendredi 27 mars 2015

iOS UICollectionView Photo Gallery Orientation issue

I'm currently working on implementing a horizontally scrollable photo gallery feature. For this, I'm using UICollectionView with the flow layout set to horizontal. The problem that I'm currently facing is with the orientation. Lets say, I launched this view in landscape and i scrolled across couple of photos and now if change orientation to portrait, i'm loosing the position where i was before in the landscape mode. i want to make sure i show the same photo centered vertically that i have before changing the orientation. please find the included source code below.



class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

var collectionView: UICollectionView!
private var samplePictures:[UIImage] = []
let CELL_IDENTIFIER = "photo_cell"
var currentIndex = 0
var flowLayout: UICollectionViewFlowLayout!


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

samplePictures = [UIImage(named: "Photo-1.png")!, UIImage(named: "Photo-2.png")!, UIImage(named: "Photo-3.png")!, UIImage(named: "Photo-4.png")!, UIImage(named: "Photo-5.png")!, UIImage(named: "Photo-6.png")!]

flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
flowLayout.minimumInteritemSpacing = 0.0
flowLayout.minimumLineSpacing = 0.0
flowLayout.sectionInset = UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0)

collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: flowLayout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.showsHorizontalScrollIndicator = false
collectionView.showsVerticalScrollIndicator = false
collectionView.autoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth
collectionView.pagingEnabled = true
collectionView.registerClass(SampleCollectionCell.self, forCellWithReuseIdentifier: CELL_IDENTIFIER)

self.view.addSubview(collectionView)
}

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
collectionView.reloadData()

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

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

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

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

var cell = collectionView.dequeueReusableCellWithReuseIdentifier(
CELL_IDENTIFIER, forIndexPath: indexPath) as SampleCollectionCell

cell.backgroundColor = UIColor.brownColor()
cell.imageView_sample.image = samplePictures[indexPath.item]

return cell
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return self.view.bounds.size
}


override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
collectionView.collectionViewLayout.invalidateLayout()
}

//supports all orientations
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.All.rawValue)
}

//view will rotate when the interface orientation happens
override func shouldAutorotate() -> Bool {
return true
}
}


This is the source code for the CustomCollectionCell that I'm using



class SampleCollectionCell: UICollectionViewCell {
var imageView_sample: UIImageView!

override init(frame: CGRect) {
super.init(frame: frame)

imageView_sample = UIImageView(frame: self.bounds)
imageView_sample.contentMode = UIViewContentMode.ScaleAspectFit
self.contentView.addSubview(imageView_sample)

}

required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func layoutSubviews() {
super.layoutSubviews()
self.imageView_surveillance.frame = self.bounds
}
}


Any help is truly appreciated. The core idea behind using this is to mimic the Photo app and also update view components when i scroll a picture. Please suggest if you think this can be done in a better way.


Regards.




Aucun commentaire:

Enregistrer un commentaire