iOS瀑布流視圖控制項"quilt"的用法

來源:互聯網
上載者:User

  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]; 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.