我试图在swift中构建collectionView
,我希望当设备处于纵向状态时collectionView
垂直滚动,而当我以横向模式旋转设备时{{1}水平滚动。这是collectionView
类:
CollectionView
要更改滚动方向,我需要更改class SceneBuilderVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{
@IBOutlet var collection: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func shouldAutorotate() -> Bool{
return true
}
override func supportedInterfaceOrientations() -> Int{
return Int(UIInterfaceOrientationMask.All.toRaw())
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView!) -> Int {
return 1;
}
func collectionView(collectionView: UICollectionView!, numberOfItemsInSection section: Int) -> Int {
return 50;
}
func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell!{
var cell:UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as UICollectionViewCell
cell.backgroundColor = UIColor.redColor()
return cell
}
}
布局。
collectionView
此外,对于iOS8,Apple使用 var flowLayout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
var scrollDirection = UICollectionViewScrollDirection.Vertical
flowLayout.scrollDirection = scrollDirection
self.collection.collectionViewLayout = flowLayout
willRotateToInterfaceOrientation:duration
如何使用** viewWillTransitionToSize:withTransitionCoordinator
*方法检查设备何时旋转,从而改变滚动方向?
答案 0 :(得分:3)
使用animateAlongsideTransition中的完成处理程序检查旋转后的方向并进行任何调整。
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
coordinator.animateAlongsideTransition(nil, completion: {context in
if(UIDevice.currentDevice().orientation == UIDeviceOrientation.Portrait || UIDevice.currentDevice().orientation == UIDeviceOrientation.PortraitUpsideDown){
//this is portrait (or upsidedown), do something
}else{
//landscape
}
})
}
但除非在特殊情况下,您不应该非常关心设备的确切方向,您只需将视图调整为"新尺寸"
答案 1 :(得分:0)
选项#1:
如果您的收藏视图占据整个屏幕,那么您只需检查宽度是否超过高度,并将结果视为横向。
选项#2:
或者您可以使用功能:
UIInterfaceOrientationIsLandscape(self.interfaceOrientation)
但是你应该知道在iOS8中已经弃用了interfaceOrientation
属性。
答案 2 :(得分:0)
这是完整的功能:
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator!){
if let theAppDelegate = UIApplication.sharedApplication() {
let orientation = theAppDelegate.statusBarOrientation
if orientation.isPortrait {
println("Landscape")
var flowLayout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
//flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
//flowLayout.itemSize = CGSizeMake(80, 80)
var scrollDirection = UICollectionViewScrollDirection.Horizontal
flowLayout.scrollDirection = scrollDirection
self.collection.collectionViewLayout = flowLayout
}else{
println("Protrait")
var flowLayout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
// flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
//flowLayout.itemSize = CGSizeMake(80, 80)
var scrollDirection = UICollectionViewScrollDirection.Vertical
flowLayout.scrollDirection = scrollDirection
self.collection.collectionViewLayout = flowLayout
}
}
}