[英]UICollectionView horizontal layout with paging IOS Swift 3.0
我想显示 UIcollectionview 单元格水平布局,分页和两个单元格之间的空间。
左右滑动移动收藏视图页面。
我从 storyboard 设置了以下属性。
滚动启用“假”,分页启用“真”,滚动方向“水平”
为此,我添加了 SwipeGesture,查看以下代码。
import UIKit
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource , UICollectionViewDelegateFlowLayout, UIGestureRecognizerDelegate{
@IBOutlet weak var pageControl: UIPageControl!;
@IBOutlet weak var heightOfCollection: NSLayoutConstraint!;
@IBOutlet weak var MyCollection: UICollectionView!;
var cellHeight:CGFloat = 0.0;
var numberOfItems: Float = 10;
var setCurrentIndex:Int = 0;
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//Add Gestures
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture(gesture:)));
swipeRight.direction = UISwipeGestureRecognizerDirection.right;
MyCollection.addGestureRecognizer(swipeRight);
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture(gesture:)));
swipeLeft.direction = UISwipeGestureRecognizerDirection.left;
MyCollection.addGestureRecognizer(swipeLeft);
}
override func viewWillAppear(_ animated: Bool) {
self.view.layoutIfNeeded();
cellHeight = (MyCollection.frame.size.width - 20) / 3;
heightOfCollection.constant = (cellHeight * 2) + 10;
MyCollection.reloadData();
MyCollection.clipsToBounds = true;
let noOfPage = ceil(Double(numberOfItems/6));
pageControl.numberOfPages = Int(noOfPage.rounded(.up));
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Collection View Delegate Methods.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: cellHeight, height: cellHeight);
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return Int(numberOfItems);
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath as IndexPath) as! MyCell;
cell.backgroundColor = UIColor(red: CGFloat(drand48()), green: CGFloat(drand48()), blue: CGFloat(drand48()), alpha: 1.0);
cell.lblNumber.text = "\(indexPath.row + 1)";
return cell;
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print(indexPath.row);
}
//MARK:- Handle Swipe Gesture Recognizer.
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:
if setCurrentIndex > 0 {
setCurrentIndex -= 1
let transition:CATransition! = CATransition();
transition.duration = 0.5;
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut);
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
MyCollection.layer.add(transition, forKey: nil);
MyCollection.contentOffset.x = (CGFloat(setCurrentIndex) * MyCollection.frame.size.width) + CGFloat(setCurrentIndex * 10)
pageControl.currentPage = setCurrentIndex;
}
case UISwipeGestureRecognizerDirection.left:
if setCurrentIndex < pageControl.numberOfPages - 1 {
setCurrentIndex += 1;
let transition:CATransition! = CATransition();
transition.duration = 0.5;
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut);
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
MyCollection.layer.add(transition, forKey: nil);
MyCollection.contentOffset.x = (CGFloat(setCurrentIndex) * MyCollection.frame.size.width) + CGFloat(setCurrentIndex * 10)
pageControl.currentPage = setCurrentIndex;
}
default:
break
}
}
}
}
我想显示方面的 output 就像在红框中的屏幕截图中一样。
我使用KSTCollectionViewPageHorizontalLayout解决了这个问题。
在github @https上看到这个演示://github.com/kelystor/KSTCollectionViewPageHorizontalLayout
override func viewWillAppear(_ animated: Bool) {
self.view.layoutIfNeeded();
cellHeight = (collectionListing.frame.size.width - 20) / 3;
collectionHeightConst.constant = (cellHeight * 2) + 10;
let pageHorizontalLayout = KSTCollectionViewPageHorizontalLayout();
pageHorizontalLayout.itemSize = CGSize(width: cellHeight, height: cellHeight);
pageHorizontalLayout.lineSpacing = 10;
pageHorizontalLayout.interitemSpacing = 10;
collectionListing.collectionViewLayout = pageHorizontalLayout;
collectionListing.reloadData();
}
我已经使用带有组合布局的集合视图解决了这个问题——
在Github上查看此演示
private func secondLayoutSection() -> NSCollectionLayoutSection { let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.33), heightDimension: .absolute(100)) let item = NSCollectionLayoutItem(layoutSize: itemSize) item.contentInsets =.init(top: 0, leading: 0, bottom: 15, trailing: 15) let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .estimated(500)) let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item]) let group1 = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item]) let MainGroup = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitems: [group,group1]) let section = NSCollectionLayoutSection(group: MainGroup) section.contentInsets.leading = 15 section.orthogonalScrollingBehavior =.groupPaging section.boundarySupplementaryItems = [ NSCollectionLayoutBoundarySupplementaryItem(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .estimated(44)), elementKind: categoryHeaderId, alignment: .top) ] return section
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.