Parallax:视差视图
2015-10-22 20:07
197 查看
最近用tableView写了一个有视差滚动效果的小demo,在这儿给大家分享一下,希望对大家有帮助。
在tableView的自定义cell上加了一个scrollView,在scrollView上面加了一个imageView,实现视差滚动(parallax)效果。
首先, 打开xcode,创建一个Single View Application的工程。
在工程里创建一个tableViewCell类,命名为ImageTableViewCell。
之后导入图片资源。
接下来就是写代码了。
ViewController.m
ImageTableViewCell.h的代码:
ImageTableViewCell.m的代码:
到这里就结束啦~写的不是很完善,有好的建议可以告诉我哦~
图片资源链接地址:
http://download.csdn.net/detail/sherry1002_/9204333
在tableView的自定义cell上加了一个scrollView,在scrollView上面加了一个imageView,实现视差滚动(parallax)效果。
首先, 打开xcode,创建一个Single View Application的工程。
在工程里创建一个tableViewCell类,命名为ImageTableViewCell。
之后导入图片资源。
接下来就是写代码了。
ViewController.m
// // ViewController.m // HEHE // // Created by dllo on 15/10/17. // Copyright (c) 2015年 Sherry. All rights reserved. // #import "ViewController.h" #import "ImageTableViewCell.h" #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width) #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height) @interface ViewController () <UITableViewDataSource, UITableViewDelegate> @property (nonatomic, retain) UITableView *tableView; @property (nonatomic, retain) NSMutableArray *tableArray; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.tableArray = [NSMutableArray array]; [self getDataFromLocal]; [self createVCTableView]; } #pragma mark - tableView 代理方法 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"imageCell"]; NSString *imageName = self.tableArray[indexPath.row]; [cell takeDataForCell:imageName]; return cell; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.tableArray.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return SCREEN_HEIGHT/3; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { for (int i = 0; i < self.tableArray.count; i++) { NSIndexPath *index = [NSIndexPath indexPathForRow:i inSection:0]; ImageTableViewCell *imageCell = (ImageTableViewCell *)[self.tableView cellForRowAtIndexPath:index]; // 当tableView滚动到负数不执行不然小cell里会有白色边 if (scrollView.contentOffset.y > 0) { if (i > 3) { [imageCell.scrollView setContentOffset:CGPointMake(0, (scrollView.contentOffset.y -SCREEN_HEIGHT/3*(i-3))/ 2.2)]; } else { [imageCell.scrollView setContentOffset:CGPointMake(0, scrollView.contentOffset.y / 2.5)]; } } } } #pragma mark - 创建方法 /// 创建TableView - (void)createVCTableView { self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain]; self.tableView.delegate = self; self.tableView.dataSource = self; self.tableView.separatorStyle = NO; self.tableView.bounces = NO; [self.tableView registerClass:[ImageTableViewCell class] forCellReuseIdentifier:@"imageCell"]; [self.view addSubview:_tableView]; } /// 准备数据 - (void)getDataFromLocal { for (int i = 0; i < 18; i++) { NSString *imageName = [NSString stringWithFormat:@"%d.jpg", i]; [self.tableArray addObject:imageName]; } [self.tableView reloadData]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
ImageTableViewCell.h的代码:
// // ImageTableViewCell.h // HEHE // // Created by dllo on 15/10/17. // Copyright (c) 2015年 Sherry. All rights reserved. // #import <UIKit/UIKit.h> @interface ImageTableViewCell : UITableViewCell @property (nonatomic, retain) UIScrollView *scrollView; - (void)takeDataForCell:(NSString *)imageName; @end
ImageTableViewCell.m的代码:
// // ImageTableViewCell.m // HEHE // // Created by dllo on 15/10/17. // Copyright (c) 2015年 Sherry. All rights reserved. // #import "ImageTableViewCell.h" @interface ImageTableViewCell () @property (nonatomic, retain) UIImageView *cellImageView; @end @implementation ImageTableViewCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { [self createSomeThingInCell]; } return self; } /// 初始化视图 - (void)createSomeThingInCell { self.cellImageView = [[UIImageView alloc] init]; self.scrollView = [[UIScrollView alloc] init]; [self.scrollView addSubview:self.cellImageView]; self.scrollView.contentSize = CGSizeMake(0, [UIScreen mainScreen].bounds.size.height); self.scrollView.userInteractionEnabled = NO; [self.contentView addSubview:self.scrollView]; } /// 给视图赋值 - (void)takeDataForCell:(NSString *)imageName { UIImage *image = [UIImage imageNamed:imageName]; [self.cellImageView setImage:image]; } - (void)layoutSubviews { [super layoutSubviews]; _cellImageView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height); _scrollView.frame = CGRectMake(0, 0, self.cellImageView.frame.size.width, [UIScreen mainScreen].bounds.size.height/3); } - (void)awakeFromNib { // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state }
到这里就结束啦~写的不是很完善,有好的建议可以告诉我哦~
图片资源链接地址:
http://download.csdn.net/detail/sherry1002_/9204333
相关文章推荐
- [No000028]Python的使用之禅及程序员应该明白的一些道理
- 词法分析实验总结
- js 邮政编码验证
- 红黑树实现
- JS学习日记-one
- verilog笔试题
- Android init进程概述
- block循环引用的理解和self.navigationController的理解
- Mysql的Bin log数据恢复:不小心删除数据库
- 数据库的几个概念:主键,外键,索引,唯一索引
- 可重入与线程安全(Reentrancy and Thread-Safety)
- java基础 - 搭建环境变量
- 学习日志---linux shell编程之while和until
- (189)丢失的第一个正整数
- leetcode Sum Root to Leaf Numbers
- 基于Quartz 2D实现截图功能
- 最长公共子序列问题
- Android 4.1 APP中的static变量即使在APP退出后仍然不会被擦除
- 月光博客:我的知识管理工具列表(强烈推荐收藏)
- 第四次作业——个人作业——软件案例分析