UICollectionView在初始化的时候移动到某个距离
2015-08-19 20:32
525 查看
#pragma mark -- 使用场景:选中非第一张图片用CollectionView进行浏览时,CollectionView滑动到相应的位置
#pragma mark -- 重点在于UICollectionViewFlowLayout的prepareLayout方法的使用
#pragma mark -- 自定义UICollectionViewFlowLayout的h文件
@interface SSCollectionViewFlowLayout : UICollectionViewFlowLayout
/**
* collectionView的偏移量
*/
@property (nonatomic, assign) CGPoint offsetpoint;
@end
#pragma mark -- 自定义UICollectionViewFlowLayout的m文件
@implementation SSCollectionViewFlowLayout
- (instancetype)init{
self = [super init];
if (self) {
self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
}
return self;
}
- (void)prepareLayout{
[super prepareLayout];
self.collectionView.contentOffset = self.offsetpoint;
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)oldBounds{
return NO;
}
#pragma mark -- 剩下的工作就是在UICollectionView 所在的ViewController设置偏移量
@property (nonatomic, strong) SSCollectionViewFlowLayout *viewLayout;
@property (nonatomic, strong) UICollectionView *ssCollectionView;
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.ssCollectionView.frame = CGRectMake(0.f, 0.f, ScreenWidth, ScreenHeight);
self.viewLayout.offsetpoint = CGPointMake(ScreenWidth *self.indexNumber, 0.f);
}
- (UICollectionView *)ssCollectionView{
if (_ssCollectionView != nil) {
return _ssCollectionView;
}
self.viewLayout = [[SSCollectionViewFlowLayout alloc] init];
_ssCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.viewLayout];
_ssCollectionView.showsHorizontalScrollIndicator = FALSE; // 去掉滚动条
_ssCollectionView.pagingEnabled = YES;
_ssCollectionView.delegate = self;
_ssCollectionView.dataSource = self;
[_ssCollectionView registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:@"CollectionCell"];
return
_ssCollectionView;
}
#pragma mark -- 重点在于UICollectionViewFlowLayout的prepareLayout方法的使用
#pragma mark -- 自定义UICollectionViewFlowLayout的h文件
@interface SSCollectionViewFlowLayout : UICollectionViewFlowLayout
/**
* collectionView的偏移量
*/
@property (nonatomic, assign) CGPoint offsetpoint;
@end
#pragma mark -- 自定义UICollectionViewFlowLayout的m文件
@implementation SSCollectionViewFlowLayout
- (instancetype)init{
self = [super init];
if (self) {
self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
}
return self;
}
- (void)prepareLayout{
[super prepareLayout];
self.collectionView.contentOffset = self.offsetpoint;
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)oldBounds{
return NO;
}
#pragma mark -- 剩下的工作就是在UICollectionView 所在的ViewController设置偏移量
@property (nonatomic, strong) SSCollectionViewFlowLayout *viewLayout;
@property (nonatomic, strong) UICollectionView *ssCollectionView;
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.ssCollectionView.frame = CGRectMake(0.f, 0.f, ScreenWidth, ScreenHeight);
self.viewLayout.offsetpoint = CGPointMake(ScreenWidth *self.indexNumber, 0.f);
}
- (UICollectionView *)ssCollectionView{
if (_ssCollectionView != nil) {
return _ssCollectionView;
}
self.viewLayout = [[SSCollectionViewFlowLayout alloc] init];
_ssCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.viewLayout];
_ssCollectionView.showsHorizontalScrollIndicator = FALSE; // 去掉滚动条
_ssCollectionView.pagingEnabled = YES;
_ssCollectionView.delegate = self;
_ssCollectionView.dataSource = self;
[_ssCollectionView registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:@"CollectionCell"];
return
_ssCollectionView;
}
相关文章推荐
- 参加北京bluemix云计算大会偶记
- uiView 设置圆角
- CUICatalog: Invalid asset name supplied: (null) _configureCellForDisplay:forIndexPath
- UI计算器的设计
- ios 延迟调用 && UIImageView && UILabel && UISegmentedControl && UISwitch && UISlider
- ios UITextField
- Building A Fence, usaco 2008 Oct
- queue队列容器
- UIScrollView
- UI16_数据持久化(NSUserDefaults,简单对象,复杂对象归档反归档,文件管理者"删除添加文件")
- Codeforces Round #241 (Div. 2) AGuess a number!
- IOS学习之路十二(UITableView下拉刷新页面)
- Handler详解(一)---关联到UI线程
- Unique Binary Search Trees II
- sql server中的TimeStamp时间戳与UniqueIdentifier数据类型
- 在navgationController中添加UISegmentedControl
- EF里Guid类型数据的自增长、时间戳和复杂类型的用法
- ios-UIKit动画
- EF里的默认映射以及如何使用Data Annotations和Fluent API配置数据库的映射
- Unique Paths