StackView

2015-10-21  本文已影响212人  CenturyGuo

StackView

心情不是很好,也抬不起太大的兴趣来写代码。随便拿了个之前写的代码做个小小的讲解。

Reason

最近可能要用到一个叠起来的View容器,但是似乎网上没有非常合适组件(也可能是我没有深挖),之前我的一位同事做过一个这样的组件,但是没有发布出来,我看了一下实现的源代码,做了一些改动,并做成了组件,发布到了Pod上。

做法

这个容器是我重新用View来写的,而且写的比较笨拙,如果有好的改动,欢迎fork我在Github上面的代码,并提交pull request。</br>
不可否认的是,这肯定是可以通过自定义CollectionViewLayout来实现的,将来可能会带来用CollectionViewLayout来实现的代码,可能还会实现Swift版本,同样很欢迎大家来帮忙写一些代码并pull request上来。</br>
那我们来一起看一看代码:

/**
 * It's a property save cells and handle cell actions
 *
 */
@property (nonatomic, strong) StackViewDefaultPile *pile;

/**
 * The components datasource
 *
 */
@property (nonatomic, weak) id<StackViewDatasource> datasource;

/**
 * Reload data clear all cell and readd cells into component
 *
 */
- (void)reloadData;

/**
 * Get Cell with Reuseid
 *
 */
- (UIView *)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndex:(NSInteger)index;

/**
 * regist cell with nibname and id
 *
 */
- (void)registerCellWithNibName:(NSString *)nibName forCellReuseIdentifier:(NSString *)identifier;

先看属性
方法(几乎按照标准的UITableView和UICollectionView来设计)
StackViewDatasource
实现文件
pile
@property (nonatomic, strong) NSArray *alphaArray;
@property (nonatomic, strong) NSArray *zTransformArray;
@property (nonatomic, assign) CGFloat rangeLength;
@property (nonatomic, strong) UIView *previousCell;
@property (nonatomic, strong) UIView *nextCell;
@property (nonatomic, assign) CATransform3D cellTransform;

- (void)reset;
- (void)recover;
- (void)pushNextCell;
- (void)pushPreviousCell;
- (UIView *)cellOfIndex:(NSInteger)index;
- (void)setCell:(UIView *)cell atIndex:(NSInteger)index;
- (UIView *)removeCellAtIndex:(NSInteger)index;
- (UIView *)bringPreviousCellToTop;
- (UIView *)bringNextCellToBottom;
- (NSInteger)maxSize;
- (void)resetCell:(UIView *)cell atIndex:(NSInteger)index;
- (void)updateCellWithOffset:(CGFloat)offset;

这些代码就不多做解释了,要想自定义Pile,只要像StackViewDefaultPile一样实现
- (NSInteger)maxSize, - (void)reset, - (UIView *)cellOfIndex:(NSInteger)index, - (void)setCell:(UIView *)cell atIndex:(NSInteger)index方法,并定义几个View,关于一些属性alphaArray,zTransformArray,rangeLength可以自己定义,产生更好地叠加效果

上一篇 下一篇

猜你喜欢

热点阅读