iOS使用UITableView实现类似图库浏览的画廊效果的简单
2017-02-22 本文已影响1266人
帅番茄
- 先看看效果图:
-
思路很简单,将UITableView横过来放置就可以了,就是用到CGAffineTransformMakeRotation()方法来设置UITableView的transform属性即可。当然还要对应的设置一下其cell的旋转。
-
下面贴代码:
自定义类有三个:
- 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;
}
- 当然,图片就自己找吧。。