Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 我可以使用滚动方向与布局方向不同的UICollectionViewFlowLayout吗?_Iphone_Ios_Sdk_Uicollectionview_Flowlayout - Fatal编程技术网

Iphone 我可以使用滚动方向与布局方向不同的UICollectionViewFlowLayout吗?

Iphone 我可以使用滚动方向与布局方向不同的UICollectionViewFlowLayout吗?,iphone,ios,sdk,uicollectionview,flowlayout,Iphone,Ios,Sdk,Uicollectionview,Flowlayout,UICollectionViewFlowLayout很棒,但是,我想稍微调整一下,使滚动方向与布局方向不同。我喜欢的示例是springboard主屏幕或表情键盘,在那里可以向左/向右滑动,但单元格是从左到右、从上到下排列的(而不是像UICollectionViewFlowLayout那样从上到下、从左到右排列,滚动方向设置为水平)。有人知道我如何轻松地将FlowLayout子类化以进行此更改吗?我很失望他们在UICollectionViewFlowLayout对象上除了scrollDirecti

UICollectionViewFlowLayout很棒,但是,我想稍微调整一下,使滚动方向与布局方向不同。我喜欢的示例是springboard主屏幕或表情键盘,在那里可以向左/向右滑动,但单元格是从左到右、从上到下排列的(而不是像UICollectionViewFlowLayout那样从上到下、从左到右排列,滚动方向设置为水平)。有人知道我如何轻松地将FlowLayout子类化以进行此更改吗?我很失望他们在UICollectionViewFlowLayout对象上除了scrollDirection之外没有layoutDirection:(

@rdelmar我实现了一些类似于您的解决方案的东西,这是可行的(没有我想要的那么优雅),但我注意到偶尔项目会从集合中消失,除非重新绘制,这就是为什么我不接受答案的原因。以下是我的结论:

@interface UICollectionViewPagedFlowLayout : UICollectionViewFlowLayout
@property int width;
@property int height;
@end
@implementation UICollectionViewPagedFlowLayout
#warning MINOR BUG: sometimes symbols disappear
- (CGSize)collectionViewContentSize {
    CGSize size = [super collectionViewContentSize];
    // make sure it's wide enough to cover all the objects
    size.width = self.collectionView.bounds.size.width * [self.collectionView numberOfSections];
    return size;
}
- (NSArray*) layoutAttributesForElementsInRect:(CGRect)rect {
    NSArray *array = [super layoutAttributesForElementsInRect:rect];
    CGRect visibleRect;
    visibleRect.origin = self.collectionView.contentOffset;
    visibleRect.size = self.collectionView.bounds.size;

    for (UICollectionViewLayoutAttributes* attributes in array) {
        if (CGRectIntersectsRect(attributes.frame, rect)) {
            // see which section this is in
            CGRect configurableRect = UIEdgeInsetsInsetRect(visibleRect, self.sectionInset);
            int horizontalIndex = attributes.indexPath.row % self.width;
            int verticalIndex = attributes.indexPath.row / self.width;
            double xspace = (configurableRect.size.width - self.width * self.itemSize.width) / self.width;
            double yspace = (configurableRect.size.height - self.height * self.itemSize.height) / self.height;
            attributes.center = CGPointMake(attributes.indexPath.section * visibleRect.size.width + self.sectionInset.left + (self.itemSize.width + xspace) * horizontalIndex + self.itemSize.width / 2, self.sectionInset.top + (self.itemSize.height + yspace) * verticalIndex + self.itemSize.height / 2);
        }
    }
    return array;
}
@end

IIRC,这在on UICollectionView中得到了演示。

我不太清楚,我想这取决于你的定义。我做了一个项目,在那里我有一个接近你想要的布局。这将水平地、从左到右、从上到下排列行。我的数据源是一个数组数组,其中每个子数组都不提供他为一行收集数据

#import "SimpleHorizontalLayout.h" // subclass of UICollectionViewFlowLayout
#define kItemSize 60

@implementation SimpleHorizontalLayout 


-(CGSize)collectionViewContentSize {
    NSInteger xSize = [self.collectionView numberOfItemsInSection:0] * (kItemSize + 20); // the 20 is for spacing between cells.
    NSInteger ySize = [self.collectionView numberOfSections] * (kItemSize + 20);
    return CGSizeMake(xSize, ySize);
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)path {
    UICollectionViewLayoutAttributes* attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:path];
    attributes.size = CGSizeMake(kItemSize,kItemSize);
    NSInteger xValue = kItemSize/2 + path.row * (kItemSize +20) ;
    NSInteger yValue = kItemSize + path.section * (kItemSize +20);
    attributes.center = CGPointMake(xValue, yValue);
    return attributes;
}


-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect {
    NSInteger minRow =  (rect.origin.x > 0)?  rect.origin.x/(kItemSize +20) : 0; // need to check because bounce gives negative values for x.
    NSInteger maxRow = rect.size.width/(kItemSize +20) + minRow;
    NSMutableArray* attributes = [NSMutableArray array];
    for(NSInteger i=0 ; i < self.collectionView.numberOfSections; i++) {
        for (NSInteger j=minRow ; j < maxRow; j++) {
            NSIndexPath* indexPath = [NSIndexPath indexPathForItem:j inSection:i];
            [attributes addObject:[self layoutAttributesForItemAtIndexPath:indexPath]];
        }
    }
    return attributes;
}
#导入“SimpleHorizontalLayout.h”//UICollectionViewFlowLayout的子类
#定义kItemSize 60
@实现SimpleHorizontalLayout
-(CGSize)集合视图内容大小{
NSInteger xSize=[self.collectionView numberofitemsinssection:0]*(kItemSize+20);//20表示单元格之间的间距。
NSInteger ySize=[self.collectionView numberOfSections]*(kItemSize+20);
返回CGSizeMake(xSize,ySize);
}
-(UICollectionViewLayoutAttributes*)layoutAttributesForItemAtIndexPath:(NSIndexPath*)路径{
UICollectionViewLayoutAttributes*属性=[UICollectionViewLayoutAttributes LayoutAttributes layoutAttributesForCellWithIndexPath:path];
attributes.size=CGSizeMake(kItemSize,kItemSize);
NSInteger xValue=kItemSize/2+path.row*(kItemSize+20);
NSInteger yValue=kItemSize+path.section*(kItemSize+20);
attributes.center=CGPointMake(xValue,yValue);
返回属性;
}
-(NSArray*)布局属性ForElementsInRect:(CGRect)rect{
NSInteger minRow=(rect.origin.x>0)?rect.origin.x/(kItemSize+20):0;//需要检查,因为反弹为x提供负值。
NSInteger maxRow=矩形尺寸宽度/(kItemSize+20)+最小行;
NSMutableArray*属性=[NSMutableArray];
对于(NSInteger i=0;i
直到苹果公司添加了一个简单的标志来改变流程布局的布局方向,我最终还是进行了分类。其中一些内容是针对我的实现的,但可能对其他人有所帮助:

@interface UICollectionViewPagedFlowLayout : UICollectionViewFlowLayout
@property int width;
@property int height;
@end
@implementation UICollectionViewPagedFlowLayout
#warning MINOR BUG: sometimes items disappear
- (CGSize)collectionViewContentSize {
    CGSize size = [super collectionViewContentSize];
    size.width = self.collectionView.bounds.size.width * [self.collectionView numberOfSections];
    return size;
}
- (NSArray*) layoutAttributesForElementsInRect:(CGRect)rect {
    NSArray *array = [super layoutAttributesForElementsInRect:rect];
    CGRect visibleRect;
    visibleRect.origin = self.collectionView.contentOffset;
    visibleRect.size = self.collectionView.bounds.size;

    for (UICollectionViewLayoutAttributes* attributes in array) {
        if (CGRectIntersectsRect(attributes.frame, rect)) {
            // see which section this is in
            CGRect configurableRect = UIEdgeInsetsInsetRect(visibleRect, self.sectionInset);
            int horizontalIndex = attributes.indexPath.row % self.width;
            int verticalIndex = attributes.indexPath.row / self.width;
            double xspace = (configurableRect.size.width - self.width * self.itemSize.width) / self.width;
            double yspace = (configurableRect.size.height - self.height * self.itemSize.height) / self.height;
            attributes.center = CGPointMake(attributes.indexPath.section * visibleRect.size.width + self.sectionInset.left + (self.itemSize.width + xspace) * horizontalIndex + self.itemSize.width / 2, self.sectionInset.top + (self.itemSize.height + yspace) * verticalIndex + self.itemSize.height / 2);
        }
    }
    return array;
}
@end

我重新观看了这两个会话,都没有涉及独立于UICollectionViewFlowLayout布局的滚动方向。我正试图实现这一点,用于跳板式布局。您的代码要点是不是在
layoutAttributesForElementsInRect
中交换节/行索引?还有什么对于数据源的
numberOfSection
numberofrowsinsSection
?@DanF否,该方法中的代码的目的是为了提高效率,只需为屏幕上可见的项调用layoutAttributesForItemAtIndexPath。您可以循环数组中的所有项,并且仍然有效(但速度可能较慢且占用更多内存)。layoutAttributesForItemAtIndexPath方法进行实际计算,以确定每个项的位置。就我的数据源方法而言,numberOfItemsInSection是[self.theData[section]count],numberOfSections是[data.count]