iOS|Android.全球首页投稿(暂停使用,暂停投稿)程序员

iOS使用UITableView实现类似图库浏览的画廊效果的简单

2017-02-22  本文已影响1266人  帅番茄
demo.gif 屏幕快照 2017-02-22 上午12.41.31.png
#import <UIKit/UIKit.h>
@interface GVSupTableViewCell : UITableViewCell
@end
#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
#import <UIKit/UIKit.h>
@interface GVSubTableViewCell : UITableViewCell
@property(strong, nonatomic)UIImageView* mainImageView;
@end
#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
#import <UIKit/UIKit.h>
@interface GVViewController : UIViewController
@end
#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
-(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;
}
上一篇下一篇

猜你喜欢

热点阅读