背景
我正在实现UICollectionView(第一次)努力实现切片的分页水平滚动视图.我希望每个瓷砖显示在框架的中央,它的左边和右边部分可见的姐妹瓷砖(类似于Safari应用程序中的页面选择器).我有兴趣使用它UICollectionView来利用内置的单元格出列,而不是使用旋转UITableView.
问题
我发现的问题是,当使用pagingEnabled = YES和时clipsToBounds = NO,一旦分页完成,就UICollectionView删除collectionView帧外的单元格(它们不在visibleCells数组中).任何人都可以提供如何在保持这种基本设置的同时实现显示姐妹瓷砖预览效果的建议吗?或者我接近这个错误?
截图
开始
滚动
结束

在滚动屏幕是完全正确的.但是在开始和结束镜头中,我希望在蓝色边缘可以看到绿色.
这是我的AppDelegate.m中的内容(在此基本设置中归功于tutsplus.com):
#import "AppDelegate.h"
@interface ViewController : UICollectionViewController
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"ID"];
[self.view setBackgroundColor:[UIColor blueColor]];
// pad the collection view by 20 px
UIEdgeInsets padding = UIEdgeInsetsMake(20.0, 20.0, 20.0, 20.0);
[self.collectionView setFrame:UIEdgeInsetsInsetRect(self.view.frame, padding)];
// set pagingEnabled and clipsToBounds off
[self.collectionView setPagingEnabled:YES];
[self.collectionView setClipsToBounds:NO];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 5;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ID" forIndexPath:indexPath];
UILabel *label = [[UILabel alloc] initWithFrame:cell.bounds];
label.textAlignment = NSTextAlignmentCenter;
label.text = [NSString stringWithFormat:@"%d", indexPath.row];
[label setBackgroundColor:[UIColor greenColor]];
[cell.contentView addSubview:label];
return cell;
}
@end
@implementation AppDelegate
{
ViewController *vc;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// setup the UICollectionViewFlowLayout
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(280, 280);
layout.minimumInteritemSpacing = 0;
layout.minimumLineSpacing = 0;
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
// add a custom UICollectionViewController to the window
vc = [[ViewController alloc] initWithCollectionViewLayout:layout];
self.window.rootViewController = vc;
self.window.backgroundColor = [UIColor yellowColor];
[self.window makeKeyAndVisible];
return YES;
}
@end
Run Code Online (Sandbox Code Playgroud)
事实证明,解决方案实际上非常简单.我只需要用UICollectionViewCell足够的像素重叠单元格,让它们在分页滚动完成后仍然显示在collectionView的框架内.相关代码是
layout.itemSize = CGSizeMake(300, 300);
layout.minimumLineSpacing = -20.0;
Run Code Online (Sandbox Code Playgroud)
并且我UICollectionViewFlowLayout将该(CGSize)collectionViewContentSize方法子类化并覆盖该方法以返回单元格的非重叠大小.
| 归档时间: |
|
| 查看次数: |
6244 次 |
| 最近记录: |