UICollectionView水平布局,带分页IOS Swift 3.0
我想显示UIcollectionview单元格的水平布局以及两个单元格之间的分页和空间 左右滑动可移动收藏视图页面 我已经从情节提要设置了以下属性 滚动启用“假”,分页启用“真”,滚动方向“水平” 为此,我添加了SwipeGesture,查看下面的代码UICollectionView水平布局,带分页IOS Swift 3.0,ios,objective-c,swift3,uicollectionview,uicollectionviewlayout,Ios,Objective C,Swift3,Uicollectionview,Uicollectionviewlayout,我想显示UIcollectionview单元格的水平布局以及两个单元格之间的分页和空间 左右滑动可移动收藏视图页面 我已经从情节提要设置了以下属性 滚动启用“假”,分页启用“真”,滚动方向“水平” 为此,我添加了SwipeGesture,查看下面的代码 import UIKit class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource , UICollecti
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
}
}
}
}
导入UIKit
类ViewController:UIViewController、UICollectionViewDelegate、UICollectionViewDataSource、UICollectionViewDelegateFlowLayout、UIGestureRecognitizerDelegate{
@IBVAR弱pageControl:UIPageControl!;
@IBOutlet弱var集合高度:NSLayoutConstraint!;
@ibvar MyCollection:UICollectionView!;
变量单元格高度:CGFloat=0.0;
var numberOfItems:浮动=10;
var setCurrentIndex:Int=0;
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后,通常从nib执行任何其他设置。
//添加手势
让swipeRight=UISweepGestureRecognitor(目标:自我,操作:#选择器(响应Sweep手势(手势:)));
swipeRight.direction=UISWipGestureRecognitizerDirection.right;
MyCollection.AddgestureRecognitor(swipeRight);
让swipeLeft=UISweepGestureRecognitor(目标:自我,操作:#选择器(响应Sweep手势(手势:)));
swipelft.direction=uiswipgesturerecognizerdirection.left;
MyCollection.AddgestureRecognitor(swipeLeft);
}
覆盖函数视图将出现(uo动画:Bool){
self.view.layoutifneed();
cellHeight=(MyCollection.frame.size.width-20)/3;
收集高度。常数=(单元格高度*2)+10;
MyCollection.reloadData();
MyCollection.clipsToBounds=true;
设noOfPage=ceil(双倍(numberOfItems/6));
pageControl.numberOfPages=Int(noOfPage.rounded(.up));
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
//标记:-集合视图委托方法。
func collectionView(collectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayout,sizeForItemAt indexPath:indexPath)->CGSize{
返回CGSize(宽度:cellHeight,高度:cellHeight);
}
func collectionView(collectionView:UICollectionView,numberOfItemsInSection:Int)->Int{
返回Int(numberOfItems);
}
func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell{
让cell=collectionView.dequeueReusableCell(带有reuseidentifier:“MyCell”,for:indexPath作为indexPath)作为!MyCell;
cell.backgroundColor=UIColor(红色:CGFloat(drand48()),绿色:CGFloat(drand48()),蓝色:CGFloat(drand48()),alpha:1.0);
cell.lblNumber.text=“\(indexPath.row+1)”;
返回单元;
}
func collectionView(collectionView:UICollectionView,didSelectItemAt indexPath:indexPath){
打印(indexPath.row);
}
//标记:-手柄滑动手势识别器。
func RespondToSweep手势(手势:UIGestureRecognitor){
如果让swipeGesture=手势为?UISweepGestureRecognitor{
开关开关方向{
案例UISweepGestureRecognitzerDirection.right:
如果setCurrentIndex>0{
setCurrentIndex-=1
let transition:cattransition!=cattransition();
过渡时间=0.5;
transition.timingFunction=CAMediaTimingFunction(名称: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;
}
案例UISweepGestureRecognitzerDirection.left:
如果setCurrentIndex
我想在红色框中显示像下面屏幕截图一样的aspected输出
我已使用kst收集视图页面水平布局解决了此问题 在github上查看此演示
我已使用kst收集视图页面水平布局解决了此问题 在github上查看此演示
…A什么不起作用?您的确切问题是什么?请在屏幕截图红色框中查看我的透视输出,当前显示错误的序列…aa以及什么不起作用?您的确切问题是什么?请在屏幕截图红色框中查看我的透视输出,当前显示错误的序列。
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();
}