1000memories已經在MIT協議下開源了它的iOS瀑布流視圖控件"quilt"。
瀑布流(quilt)-以不同的縱橫比在多個列中顯示圖片和媒體,是1000memories網站、iPhone和Android版ShoeBox的設計美學核心。它給了用戶一種真實相冊的感覺並強調了老照片的美。
好吧,上面兩段話是摘抄過來的,算是開場白,我們直接入正題好了。
"quilt"的用法:
1.首先去github上下載開源的代碼吧。
2.你會發現下載下來的代碼中有好幾個文件夾,將下面路徑下的6個文件直接拖拽到你的工程裡(不用像demo中添加那麼多):
3.去往你要實現的類,在頭文件中添加如下代碼:
[csharp]
#import <UIKit/UIKit.h>
#import "TMQuiltView.h"
@interface WaterFlowVC : UIViewController<TMQuiltViewDataSource,TMQuiltViewDelegate>
{
TMQuiltView *_tmQuiltView;
NSMutableArray *_images;
}
@property (nonatomic,retain)TMQuiltView *tmQuiltView;
@property (nonatomic,retain)NSMutableArray *images;
@end
#import <UIKit/UIKit.h>
#import "TMQuiltView.h"
@interface WaterFlowVC : UIViewController<TMQuiltViewDataSource,TMQuiltViewDelegate>
{
TMQuiltView *_tmQuiltView;
NSMutableArray *_images;
}
@property (nonatomic,retain)TMQuiltView *tmQuiltView;
@property (nonatomic,retain)NSMutableArray *images;
@end
其中tmQuiltView就是1000memories他們定義的瀑布流控件了,類似於tableView(從添加的協議就可以看的出來很像),_images是個數據源,存放圖片的數組。
4.再去.m文件實現數據源和代理:
[csharp]
#pragma mark -
#pragma mark TMQuiltViewDataSource
-(NSInteger)quiltViewNumberOfCells:(TMQuiltView *)TMQuiltView
{
return [self.images count];
}
-(TMQuiltViewCell *)quiltView:(TMQuiltView *)quiltView cellAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifierStr = @"photoIdentifier";
TMPhotoQuiltViewCell *cell = (TMPhotoQuiltViewCell *)[quiltView dequeueReusableCellWithReuseIdentifier:identifierStr];
if (!cell)
{
cell = [[[TMPhotoQuiltViewCell alloc] initWithReuseIdentifier:identifierStr] autorelease];
}
cell.photoView.image = [self imageAtIndexPath:indexPath];
cell.titleLabel.text = [NSString stringWithFormat:@"%d", indexPath.row + 1];
return cell;
}
#pragma mark -
#pragma mark TMQuiltViewDelegate
//列數
- (NSInteger)quiltViewNumberOfColumns:(TMQuiltView *)quiltView
{
return 2;
}
//單元高度
- (CGFloat)quiltView:(TMQuiltView *)quiltView heightForCellAtIndexPath:(NSIndexPath *)indexPath {
float height = [self imageAtIndexPath:indexPath].size.height / [self quiltViewNumberOfColumns:quiltView];
return height;
}
#pragma mark -
#pragma mark TMQuiltViewDataSource
-(NSInteger)quiltViewNumberOfCells:(TMQuiltView *)TMQuiltView
{
return [self.images count];
}
-(TMQuiltViewCell *)quiltView:(TMQuiltView *)quiltView cellAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifierStr = @"photoIdentifier";
TMPhotoQuiltViewCell *cell = (TMPhotoQuiltViewCell *)[quiltView dequeueReusableCellWithReuseIdentifier:identifierStr];
if (!cell)
{
cell = [[[TMPhotoQuiltViewCell alloc] initWithReuseIdentifier:identifierStr] autorelease];
}
cell.photoView.image = [self imageAtIndexPath:indexPath];
cell.titleLabel.text = [NSString stringWithFormat:@"%d", indexPath.row + 1];
return cell;
}
#pragma mark -
#pragma mark TMQuiltViewDelegate
//列數
- (NSInteger)quiltViewNumberOfColumns:(TMQuiltView *)quiltView
{
return 2;
}
//單元高度
- (CGFloat)quiltView:(TMQuiltView *)quiltView heightForCellAtIndexPath:(NSIndexPath *)indexPath {
float height = [self imageAtIndexPath:indexPath].size.height / [self quiltViewNumberOfColumns:quiltView];
return height;
}
數據源和代理方法需要的就這麼多,有其它需求可以跳轉到定義數據源和代理的類看一下。給出的demo中將
-(NSArray *)images和- (UIImage *)imageAtIndexPath:(NSIndexPath *)indexPath兩個方法也寫在了數據源中,注意這兩個方法不是數據源的方法。
其中TMPhotoQuiltViewCell是我直接從demo中拷貝出來的自定義的單元,你可以根據自己的需求將其改成你想要的外觀。
代理方法與tableview的代理很是相似,所以也很容易懂。
5.最後是一些需要定義的變量了:
[csharp]
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
_tmQuiltView = [[TMQuiltView alloc] init];
_tmQuiltView.frame = CGRectMake(0, 0, 320, [[UIScreen mainScreen] bounds].size.height-20-44);
_tmQuiltView.delegate = self;
_tmQuiltView.dataSource = self;
[self.view addSubview:_tmQuiltView];
[_tmQuiltView reloadData];
NSMutableArray *imageNames = [[NSMutableArray alloc] init];
for (int i = 0; i< kNumberOfCells;i++ )
{
[imageNames addObject:[NSString stringWithFormat:@"%d.jpeg",i % 10 + 1]];
}
self.images = imageNames;
[imageNames release];
[_tmQuiltView reloadData];
}