使用UITableView实现图片视差效果
使用UITableView实现图片视差效果

视差效果如下:


原理:
根据偏移量计算不同的移动速度,so easy!
//
// RootTableViewController.h
// TableView
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import <UIKit/UIKit.h> @interface RootTableViewController : UITableViewController @end
//
// RootTableViewController.m
// TableView
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "RootTableViewController.h"
#import "ImageCell.h"
#import "UIImage+ImageEffects.h"
#import "FrameAccessor.h" #define IMAGE [UIImage imageNamed:@"girl"]
#define IMAGE_HEIGHT [IMAGE scaleWithFixedWidth:320.f].size.height @interface RootTableViewController ()<UIScrollViewDelegate> @property (nonatomic, strong) ImageCell *showImageCell; @property (nonatomic, strong) UIImage *rootImage; @end @implementation RootTableViewController - (void)viewDidLoad
{
[super viewDidLoad]; _rootImage = [IMAGE scaleWithFixedWidth:.f];
} #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return ;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return ;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == )
{
static NSString *reusedLableImage = @"Image";
ImageCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedLableImage];
if (cell == nil)
{
cell = [[ImageCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:reusedLableImage];
} _showImageCell = cell;
cell.showImageView.image = _rootImage;
cell.showImageView.viewSize = _rootImage.size; return cell;
}
else
{
static NSString *reusedLableOne = @"Normal";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedLableOne];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:reusedLableOne]; cell.backgroundColor = [UIColor whiteColor]; cell.textLabel.text = @"YouXianMing";
cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin"
size:.f];
} return cell;
}
} - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 返回图片高度
if (indexPath.row == )
{
return [IMAGE scaleWithFixedWidth:.f].size.height;
} return ;
} - (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// 防止出现bug
if (scrollView.contentOffset.y <= )
{
_showImageCell.layer.masksToBounds = NO;
}
else
{
_showImageCell.layer.masksToBounds = YES;
} // 计算偏移量
_showImageCell.showImageView.y \
= calculateSlope(, , , )*scrollView.contentOffset.y +
calculateConstant(, , , );
} CGFloat calculateSlope(CGFloat x1, CGFloat y1, CGFloat x2, CGFloat y2)
{
return (y2 - y1) / (x2 - x1);
} CGFloat calculateConstant(CGFloat x1, CGFloat y1, CGFloat x2, CGFloat y2)
{
return (y1*(x2 - x1) - x1*(y2 - y1)) / (x2 - x1);
} @end
//
// ImageCell.h
// TableView
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import <UIKit/UIKit.h> @interface ImageCell : UITableViewCell @property (nonatomic, strong) UIImageView *showImageView; @end
//
// ImageCell.m
// TableView
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "ImageCell.h"
#import "FrameAccessor.h" @implementation ImageCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
_showImageView = [[UIImageView alloc] init];
_showImageView.frame = (CGRect){CGPointZero, CGSizeZero}; [self addSubview:_showImageView];
}
return self;
} @end
好吧,止足于这种效果的话就太简单了,来点复杂的:)


//
// RootTableViewController.h
// TableView
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import <UIKit/UIKit.h> @interface RootTableViewController : UITableViewController @end
//
// RootTableViewController.m
// TableView
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "RootTableViewController.h"
#import "ImageCell.h"
#import "UIImage+ImageEffects.h"
#import "FrameAccessor.h" #define IMAGE [UIImage imageNamed:@"girl"]
#define IMAGE_HEIGHT [IMAGE scaleWithFixedWidth:320.f].size.height @interface RootTableViewController ()<UIScrollViewDelegate> @property (nonatomic, strong) ImageCell *showImageCell; @property (nonatomic, strong) UIImage *rootImage;
@property (nonatomic, strong) UIImage *rootBlurImage; @end @implementation RootTableViewController - (void)viewDidLoad
{
[super viewDidLoad]; _rootImage = [IMAGE scaleWithFixedWidth:.f];
_rootBlurImage = [[IMAGE scaleWithFixedWidth:.f] grayScale];
} #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return ;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return ;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == )
{
static NSString *reusedLableImage = @"Image";
ImageCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedLableImage];
if (cell == nil)
{
cell = [[ImageCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:reusedLableImage];
} _showImageCell = cell;
cell.showImageView.image = _rootImage;
cell.showImageView.viewSize = _rootImage.size; cell.showBlurImageView.image = _rootBlurImage;
cell.showBlurImageView.viewSize = _rootBlurImage.size; return cell;
}
else
{
static NSString *reusedLableOne = @"Normal";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedLableOne];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:reusedLableOne]; cell.backgroundColor = [UIColor whiteColor]; cell.textLabel.text = @"YouXianMing";
cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin"
size:.f];
} return cell;
}
} - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 返回图片高度
if (indexPath.row == )
{
return [IMAGE scaleWithFixedWidth:.f].size.height;
} return ;
} - (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// 防止出现bug
if (scrollView.contentOffset.y <= )
{
_showImageCell.layer.masksToBounds = NO;
}
else
{
_showImageCell.layer.masksToBounds = YES;
} // 计算偏移量
_showImageCell.showImageView.y \
= calculateSlope(, , , )*scrollView.contentOffset.y +
calculateConstant(, , , ); // 计算偏移量
_showImageCell.showBlurImageView.y \
= calculateSlope(, , , )*scrollView.contentOffset.y +
calculateConstant(, , , ); // 计算偏移量
_showImageCell.showBlurImageView.alpha \
= calculateSlope(, , , )*scrollView.contentOffset.y +
calculateConstant(, , , );
} CGFloat calculateSlope(CGFloat x1, CGFloat y1, CGFloat x2, CGFloat y2)
{
return (y2 - y1) / (x2 - x1);
} CGFloat calculateConstant(CGFloat x1, CGFloat y1, CGFloat x2, CGFloat y2)
{
return (y1*(x2 - x1) - x1*(y2 - y1)) / (x2 - x1);
} @end
//
// ImageCell.h
// TableView
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import <UIKit/UIKit.h> @interface ImageCell : UITableViewCell @property (nonatomic, strong) UIImageView *showImageView;
@property (nonatomic, strong) UIImageView *showBlurImageView; @end
//
// ImageCell.m
// TableView
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "ImageCell.h"
#import "FrameAccessor.h" @implementation ImageCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
_showImageView = [[UIImageView alloc] init];
_showImageView.frame = (CGRect){CGPointZero, CGSizeZero};
[self addSubview:_showImageView]; _showBlurImageView = [[UIImageView alloc] init];
_showBlurImageView.frame = (CGRect){CGPointZero, CGSizeZero};
_showBlurImageView.alpha = .f;
[self addSubview:_showBlurImageView];
}
return self;
} @end
就是这么简单:)
使用UITableView实现图片视差效果的更多相关文章
- UITableViewCell图片视差效果
UITableViewCell图片视差效果 效果 源码 https://github.com/YouXianMing/Animations 细节 OffsetImageCell.m OffsetCel ...
- 基于Parallax设计HTML视差效果
年关将至,给大家拜年. 最近时间充裕了一点,给大家介绍一个比较有意思的控件:Parallax.它可以用来实现鼠标移动时,页面上的元素也做偏移的视差效果.在一些有表现层次,布局空旷的页面上,用来做Hea ...
- css中的视距perspective和视差效果
概述 之前觉得2个效果很叼,一个是3D翻转效果,另一个是视差效果.今天好好的研究一下,把心得记录下来,供以后开发时参考,相信对其他人也有用. 3D翻转 3D翻转效果其实非常简单,其实就是perspec ...
- banner 跟随鼠标呈现视差效果
参考 Element 官网,利用 js / jq 和 css3, 实现某图片随着鼠标移动呈现的视差效果. <!DOCTYPE html> <html> <head> ...
- 使用NestedScrollView+ViewPager+RecyclerView+SmartRefreshLayout打造酷炫下拉视差效果并解决各种滑动冲突
使用NestedScrollView+ViewPager+RecyclerView+SmartRefreshLayout打造酷炫下拉视差效果并解决各种冲突 如果你还在为处理滑动冲突而发愁,那么你需要静 ...
- Android 使用NestedScrollView+ViewPager+RecyclerView+SmartRefreshLayout打造酷炫下拉视差效果并解决各种滑动冲突
如果你还在为处理滑动冲突而发愁,那么你需要静下心来看看这边文章,如果你能彻底理解这篇文章中使用的技术,那么,一切滑动冲突的问题解决起来就轻而易举了: 先扔一个最终实现的效果图 先分析下效果图中实现的功 ...
- js鼠标滚轮滚动图片切换效果
效果体验网址:http://keleyi.com/keleyi/phtml/image/12.htm HTML文件代码: <!DOCTYPE html PUBLIC "-//W3C// ...
- jQuery实现鼠标经过图片变亮效果
在线体验效果:http://hovertree.com/texiao/jquery/1.htm 以下是完整源代码: <!DOCTYPE html PUBLIC "-//W3C//DTD ...
- [读码][js,css3]能感知鼠标方向的图片遮罩效果
效果图: 无意间看到过去流行的一个效果:[能感知鼠标方向的图片遮罩效果]近来不忙,就仔细的看了一看看到后来发现,网上有好多版本,谁是原著者似乎已经无法考证.读码就要读比较全面的,读像是原著的代码.代码 ...
随机推荐
- HelloStruts2
第一个struts2项目: 前言 假 如 你 的 人 生 有 理 想,那 么 就 一 定 要 去 追,不 管 你 现 在 的 理 想 在 别 人 看 来是 多 么 的 可 笑 , 你 也 不 用 在 ...
- c++ 网络编程课设入门超详细教程 ---目录
原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/9663167.html c++ 网络编程(一)TCP/UDP windows/linux 下入门 ...
- python-select异步IO
#实现多任务在同一个线程切换 #!/usr/bin/python from socket import * from select import * from time import ctime so ...
- 使用jdk生成ssl证书文件
java自带有 ssl 证书生成的工具, 在 /bin/keytools.exe 需要确认已经正确配置JAVA_HOME的环境变量 生成服务端证书: keytool -genkey -v -alias ...
- Jetty 的工作原理
创建一个ServletContextServer类,用来初始化web应用程序的Context,并且指定Servlet和Servlet匹配的url.这里指定了两个Servlet,分别是HelloServ ...
- java的instanceof简单使用
instanceof:是java中用来判断一个对象属于哪个类型的关键字 (instanceof是instance和of两个单词组成,但of并没有大写) eg: public class Test{ ...
- memcached 学习笔记 3
适合什么场合 memcached不是万能的,它也不是适用在所有场合. Memcached是“分布式”的内存对象缓存系统,那么就是说,那些不需要“分布”的,不需要共享的,或者干脆规模小到只有一台服务器的 ...
- java中string的replace和replace的区别
乍一看,字面上理解好像replace只替换第一个出现的字符(受javascript的影响),replaceall替换所有的字符,其实大不然,只是替换的用途不一样,简而言之,replace用新串序列替换 ...
- 724_Find-Pivot-Index
目录 724_Find-Pivot-Index Description Solution Java solution Python solution 724_Find-Pivot-Index Desc ...
- IOS TableView实现省市联动
之前用UIPickerView实现了省市联动,上个月网友让用UITableView给他实现了下.今天也把这些贴出来. // // ViewController.m // doubleTable // ...