스위프트와 함께 UICollectionView
을 사용하여 포토 갤러리를 만들려고합니다. 전체 화면으로 사진을 표시하고 오른쪽으로 스 와이프하여 다음 이미지로 이동하려고합니다. 세로 프레임에 대한 작업 코드가 있지만 가로로 가면 마자 셀이 올바른 위치와 크기로 다시 조정되지 않는 것처럼 보입니다.UICollectionView 신속한 사진 갤러리
항상 세로 화면의 width
, height
,및 y position
을 유지한다는 것을 의미합니다. 또한 세로 및 가로 프레임이 오류를 얻을 :
->는 UICollectionViewFlowLayout의 동작이 정의되지 않은 이유는
-> 항목 높이가 작아야합니다 그 UICollectionView 마이너스 섹션 세트의 높이 상단 및 하단 값.
나는 웹에서 답을 찾아 보았지만 가능한 모든 해결책을 시도했지만 저에게 효과적이지 않습니다. 누구든지이 일을 도울 수 있으면 알려주세요. 매우 감사.
import UIKit
class ImageDetailViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,UINavigationControllerDelegate {
@IBOutlet weak var imgDetailCollectionView: UICollectionView!
var index: Int!
var imageItems: Array<Items> = []
var currentIndex: CGFloat!
var size = UIScreen.mainScreen().bounds.size
init(index: Int , imageItems: Array<Items>) {
self.index = index
self.imageItems = imageItems
super.init(nibName: "ImageDetailViewController", bundle: NSBundle.mainBundle())
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
imgDetailCollectionView.registerNib(UINib(nibName: "ImageDetailCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "ImageDetailCollectionViewCell")
//self.automaticallyAdjustsScrollViewInsets = false
imgDetailCollectionView.setZoomScale(0.5, animated: true)
}
override func viewDidLayoutSubviews() {
imgDetailCollectionView.scrollRectToVisible(CGRectMake(320 * CGFloat(index), 0, 320 , 240), animated: false)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
return self.imageItems.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
var cell = imgDetailCollectionView.dequeueReusableCellWithReuseIdentifier("ImageDetailCollectionViewCell", forIndexPath: indexPath) as ImageDetailCollectionViewCell
cell.setup(imageItems[indexPath.row].url!)
if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation)){
self.imgDetailCollectionView.collectionViewLayout.invalidateLayout()
var frame = cell.frame
frame.size = CGSizeMake(size.width, size.height)
cell.frame = frame
}else{
self.imgDetailCollectionView.collectionViewLayout.invalidateLayout()
var frame = cell.frame
frame.size = CGSizeMake(size.height, size.width)
cell.frame = frame
}
return cell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
var size = UIScreen.mainScreen().bounds.size
if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation)){
return CGSizeMake(size.width, size.height)
}else{
return CGSizeMake(size.height, size.width)
}
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return 0
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return 0
}
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
self.imgDetailCollectionView.collectionViewLayout.invalidateLayout()
var currentOffset = self.imgDetailCollectionView.contentOffset
self.currentIndex = currentOffset.x/imgDetailCollectionView.frame.size.width
}
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
var currentSize = imgDetailCollectionView.bounds.size
var offset = self.currentIndex * currentSize.width
imgDetailCollectionView.setContentOffset(CGPointMake(offset, 0), animated: false)
imgDetailCollectionView.reloadData()
}
override func willAnimateRotationToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
self.imgDetailCollectionView.collectionViewLayout.invalidateLayout()
}
}