一開始幾頁滑動是沒有問題的,等滑到三四個頁面之后,就出現奇怪的縫隙,一開始死活找不到原因,最后在layout的代理方法minimumLineSpacingForSectionAtIndex返回值設置為0才解決,一開始想我只顯示一行,跟這個應該沒什么關系吧,就沒設置,其他的兩個代理方法minimumInteritemSpacingForSectionAtIndex和insetForSectionAtIndex都返回的是0.最后沒轍了,1個晚上過去了,加了一個代理方法,然后問題就奇妙的解決了。
//添加UICollectionView
{
MyBigPictureFlowLayout *layout = [[MyBigPictureFlowLayout alloc] init];
//UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
[layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
self.albumCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, kAllWidth, kAllHeight - 64) collectionViewLayout:layout];
self.albumCollection.backgroundColor = [UIColor clearColor];
self.albumCollection.dataSource = self;
self.albumCollection.delegate = self;
self.albumCollection.pagingEnabled = YES;
self.albumCollection.bounces = YES;
self.albumCollection.showsVerticalScrollIndicator = NO;
self.albumCollection.showsHorizontalScrollIndicator = NO;
self.albumCollection.contentSize = CGSizeMake(kAllWidth*self.dataSource.count, kAllHeight-64);
self.albumCollection.contentOffset = CGPointMake(kAllWidth*(self.index-1), 0);
[vContainer addSubview:self.albumCollection];
//注冊cell的視圖
static NSString *identifer = @"BigPictureCell";
[self.albumCollection registerClass:[BigPictureCell class] forCellWithReuseIdentifier:identifer];
}
#pragma mark-Delegate的代理方法
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
return 0;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(kAllWidth, kAllHeight - 64);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(0, 0, 0, 0);
}
UICollectionViewFlowLayout的寫法:
#import <UIKit/UIKit.h>
@interface MyBigPictureFlowLayout : UICollectionViewFlowLayout
@end
#import "MyBigPictureFlowLayout.h"
@implementation MyBigPictureFlowLayout
-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{
NSArray *answer=[[super layoutAttributesForElementsInRect:rect] mutableCopy];
for (int i=1; i<[answer count]; i++) {
UICollectionViewLayoutAttributes *currentLayoutAttributes=answer[i];
UICollectionViewLayoutAttributes *prevLayoutAttributesans=answer[i-1];
NSInteger maximumSpacing=0;
//NSInteger origin=CGRectGetMaxX(prevLayoutAttributesans.frame);
float origin = prevLayoutAttributesans.frame.origin.x + prevLayoutAttributesans.frame.size.width;
CGRect frame=currentLayoutAttributes.frame;
frame.origin.x=origin+maximumSpacing;
currentLayoutAttributes.frame=frame;
}
NSLog(@"%@",answer);
return answer;
}
@end
代碼地址:http://git.oschina.net/hyp.cn/demo