iOS使用UITableView實現(xiàn)類似圖庫瀏覽的畫廊效果的簡單demo

  • 先看看效果圖:
demo.gif
  • 思路很簡單趋箩,將UITableView橫過來放置就可以了畦娄,就是用到CGAffineTransformMakeRotation()方法來設(shè)置UITableView的transform屬性即可。當然還要對應(yīng)的設(shè)置一下其cell的旋轉(zhuǎn)熟尉。

  • 下面貼代碼:
    自定義類有三個:

屏幕快照 2017-02-22 上午12.41.31.png
  • GVSupTableViewCell.h:
#import <UIKit/UIKit.h>
@interface GVSupTableViewCell : UITableViewCell
@end
  • GVSupTableViewCell.m:
#import "GVSupTableViewCell.h"
#import "GVSubTableViewCell.h"
@interface GVSupTableViewCell()<UITableViewDelegate, UITableViewDataSource>
@property(strong, nonatomic)UITableView* tableView;
@end
@implementation GVSupTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.bounds = CGRectMake(0, 0, 120, [UIScreen mainScreen].bounds.size.width);
        self.tableView.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, 60);
        self.tableView.showsVerticalScrollIndicator = NO;
        self.tableView.transform  = CGAffineTransformMakeRotation(-M_PI_2);
        [self.contentView addSubview:self.tableView];
    }
    return self;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 20;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GVSubTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
    if(!cell){
        cell = [[GVSubTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
        cell.transform = CGAffineTransformMakeRotation(M_PI_2);
        cell.mainImageView.layer.cornerRadius = 3;
    }
    UIImage* image = [[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"timg-%ld", (long)indexPath.row+1] ofType:@"jpeg"]];
    cell.mainImageView.image = image;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}
//控制滾動時圖片的放大縮小
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSArray* indexPathArray = [self.tableView indexPathsForVisibleRows];
    double centerY = scrollView.contentOffset.y + [UIScreen mainScreen].bounds.size.width/2;
    for(NSIndexPath* indexPath in indexPathArray){
     GVSubTableViewCell* cell =  [self.tableView cellForRowAtIndexPath:indexPath];
        NSLog(@"cell.center.x: %f , y: %f", cell.center.x, cell.center.y);
        double scaleFactor = ((cell.center.y - centerY)/([UIScreen mainScreen].bounds.size.width/2))*M_PI_2;
        double cosFactor = cos(0.6*scaleFactor);
        if(cosFactor<0.7){
            cosFactor =  0.7;
        }
        cell.contentView.transform = CGAffineTransformMakeScale((CGFloat)cosFactor, (CGFloat)cosFactor);
    }
    NSLog(@"滾動了归露,offset.y: %f, center.y: %f", scrollView.contentOffset.y, scrollView.center.y);
}
@end
  • GVSubTableViewCell.h
#import <UIKit/UIKit.h>
@interface GVSubTableViewCell : UITableViewCell
@property(strong, nonatomic)UIImageView* mainImageView;
@end
  • GVSubTableViewCell.m
#import "GVSubTableViewCell.h"
@implementation GVSubTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        self.mainImageView = [[UIImageView alloc]init];
        self.mainImageView.frame = CGRectMake(10, 10, 100, 100);
        [self.contentView addSubview:self.mainImageView];
    }
    return self;
}
@end
  • GVViewController.h:
#import <UIKit/UIKit.h>
@interface GVViewController : UIViewController
@end
  • GVViewController.m:
#import "GVViewController.h"
#import "GVSupTableViewCell.h"
@interface GVViewController ()<UITableViewDataSource , UITableViewDelegate>
@property(nonatomic, strong)UITableView* tableView;
@end
@implementation GVViewController
-(instancetype)init{
    self = [super init];
    if(self){
        self.view.backgroundColor = [UIColor whiteColor];
        self.tableView = [[UITableView alloc]initWithFrame:[self.view bounds] style:UITableViewStyleGrouped];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        [self.view addSubview:self.tableView];
}
    return self;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GVSupTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(!cell){
        cell = [[GVSupTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];      
    }
    return cell;
}
-(void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
@end
  • 再者就是在AppDelegate.m文件中
    修改該-application:didFinishLaunchingWithOptions:方法
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    GVViewController* controller = [[GVViewController alloc]init];
    UINavigationController* navigation = [[UINavigationController alloc]initWithRootViewController:controller];
    self.window= [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = navigation;
    [self.window makeKeyAndVisible];
    return YES;
}
  • 當然,圖片就自己找吧斤儿。剧包。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市往果,隨后出現(xiàn)的幾起案子疆液,更是在濱河造成了極大的恐慌,老刑警劉巖棚放,帶你破解...
    沈念sama閱讀 216,372評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件枚粘,死亡現(xiàn)場離奇詭異,居然都是意外死亡飘蚯,警方通過查閱死者的電腦和手機馍迄,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評論 3 392
  • 文/潘曉璐 我一進店門福也,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人攀圈,你說我怎么就攤上這事暴凑。” “怎么了赘来?”我有些...
    開封第一講書人閱讀 162,415評論 0 353
  • 文/不壞的土叔 我叫張陵现喳,是天一觀的道長。 經(jīng)常有香客問我犬辰,道長嗦篱,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,157評論 1 292
  • 正文 為了忘掉前任幌缝,我火速辦了婚禮灸促,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘涵卵。我一直安慰自己浴栽,他們只是感情好,可當我...
    茶點故事閱讀 67,171評論 6 388
  • 文/花漫 我一把揭開白布轿偎。 她就那樣靜靜地躺著典鸡,像睡著了一般。 火紅的嫁衣襯著肌膚如雪坏晦。 梳的紋絲不亂的頭發(fā)上萝玷,一...
    開封第一講書人閱讀 51,125評論 1 297
  • 那天,我揣著相機與錄音英遭,去河邊找鬼间护。 笑死,一個胖子當著我的面吹牛挖诸,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播法精,決...
    沈念sama閱讀 40,028評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼多律,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了搂蜓?” 一聲冷哼從身側(cè)響起狼荞,我...
    開封第一講書人閱讀 38,887評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎帮碰,沒想到半個月后相味,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,310評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡殉挽,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,533評論 2 332
  • 正文 我和宋清朗相戀三年丰涉,在試婚紗的時候發(fā)現(xiàn)自己被綠了拓巧。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,690評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡一死,死狀恐怖肛度,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情投慈,我是刑警寧澤承耿,帶...
    沈念sama閱讀 35,411評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站伪煤,受9級特大地震影響加袋,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜抱既,卻給世界環(huán)境...
    茶點故事閱讀 41,004評論 3 325
  • 文/蒙蒙 一锁荔、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧蝙砌,春花似錦阳堕、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至肚邢,卻和暖如春壹堰,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背骡湖。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評論 1 268
  • 我被黑心中介騙來泰國打工贱纠, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人响蕴。 一個月前我還...
    沈念sama閱讀 47,693評論 2 368
  • 正文 我出身青樓谆焊,卻偏偏與公主長得像,于是被迫代替她去往敵國和親浦夷。 傳聞我的和親對象是個殘疾皇子辖试,可洞房花燭夜當晚...
    茶點故事閱讀 44,577評論 2 353

推薦閱讀更多精彩內(nèi)容