使用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实现图片视差效果的更多相关文章

  1. UITableViewCell图片视差效果

    UITableViewCell图片视差效果 效果 源码 https://github.com/YouXianMing/Animations 细节 OffsetImageCell.m OffsetCel ...

  2. 基于Parallax设计HTML视差效果

    年关将至,给大家拜年. 最近时间充裕了一点,给大家介绍一个比较有意思的控件:Parallax.它可以用来实现鼠标移动时,页面上的元素也做偏移的视差效果.在一些有表现层次,布局空旷的页面上,用来做Hea ...

  3. css中的视距perspective和视差效果

    概述 之前觉得2个效果很叼,一个是3D翻转效果,另一个是视差效果.今天好好的研究一下,把心得记录下来,供以后开发时参考,相信对其他人也有用. 3D翻转 3D翻转效果其实非常简单,其实就是perspec ...

  4. banner 跟随鼠标呈现视差效果

    参考 Element 官网,利用 js / jq 和 css3, 实现某图片随着鼠标移动呈现的视差效果. <!DOCTYPE html> <html> <head> ...

  5. 使用NestedScrollView+ViewPager+RecyclerView+SmartRefreshLayout打造酷炫下拉视差效果并解决各种滑动冲突

    使用NestedScrollView+ViewPager+RecyclerView+SmartRefreshLayout打造酷炫下拉视差效果并解决各种冲突 如果你还在为处理滑动冲突而发愁,那么你需要静 ...

  6. Android 使用NestedScrollView+ViewPager+RecyclerView+SmartRefreshLayout打造酷炫下拉视差效果并解决各种滑动冲突

    如果你还在为处理滑动冲突而发愁,那么你需要静下心来看看这边文章,如果你能彻底理解这篇文章中使用的技术,那么,一切滑动冲突的问题解决起来就轻而易举了: 先扔一个最终实现的效果图 先分析下效果图中实现的功 ...

  7. js鼠标滚轮滚动图片切换效果

    效果体验网址:http://keleyi.com/keleyi/phtml/image/12.htm HTML文件代码: <!DOCTYPE html PUBLIC "-//W3C// ...

  8. jQuery实现鼠标经过图片变亮效果

    在线体验效果:http://hovertree.com/texiao/jquery/1.htm 以下是完整源代码: <!DOCTYPE html PUBLIC "-//W3C//DTD ...

  9. [读码][js,css3]能感知鼠标方向的图片遮罩效果

    效果图: 无意间看到过去流行的一个效果:[能感知鼠标方向的图片遮罩效果]近来不忙,就仔细的看了一看看到后来发现,网上有好多版本,谁是原著者似乎已经无法考证.读码就要读比较全面的,读像是原著的代码.代码 ...

随机推荐

  1. Check类之TypeValidation

    (1)Validator类的visitTypeApply()方法 实例1: class TestTypeVal<T extends InputStream>{ TestTypeVal< ...

  2. CI中使用log4php调试程序

    下载log4php.我下载的版本是:apache-log4php-2.3.0-src.zip.借压缩,将压缩文件中的src/main/php/文件夹拷贝到CI的application/thrid_pa ...

  3. 一文带你入门图像分析,成为AI专家不是梦!

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯云AI中心发表于云+社区专栏 腾讯云高级研究员讲述,从成像到图像分析如何入门 文︱冀永楠 "AI来了"邀请到我 ...

  4. 深入redis内部--实现字符串

    redis字符串的定义和实现在Ssd.h和Ssd.c中. 1.定义 typedef char *sds; //本质是字符char的指针 2.字符串的操作 sds sdsnew(const char * ...

  5. Delphi 通得进程ID获取主窗口句柄

    只知道进程ID,获取主窗口句柄的方法如下: 通过EnumWindows枚举所有窗口 使用GetWindowThreadProcessID,通过窗口句柄获取进程ID 比便获取的进程ID与当前已知的进程I ...

  6. Angular2-给属性做双向绑定

    呈现一个实体对象的DOM结构,这个DOM有个自定义属性是需要动态赋值的,比如说 item.data 要绑到自定义属性 data 上:你可能会这么写: data="{{item.data}}& ...

  7. [SQL Server] 无法连接到本地数据库

    打开SQL Server配置管理器 启用下图两个协议 打开SQL Server服务 这一步可能出现这种情况: 故障原因是,安装Visual Studio 2012的时候,自动安装“Microsoft ...

  8. JavaScript三大对象详细解说

    Js三大对象 一 浏览器对象 浏览器窗口.文档document.URL地址等 常用的浏览器对象: 浏览器对象的分层结构 Window对象 (1) 属性 名称 说明 document 表示给定浏览器窗口 ...

  9. C# System.Timers.Timers的用法在工控设备上位中的用法

    这两天写设备的上位,由于要读取服务器上的数据库,通过WEBSERVICE访问数据库,我具体的做法是: 1.专门用Timer起线程执行,由于在用的时候报错,不能访问其他线程资源的错误,因此我用了委托的方 ...

  10. nodejs搭建简易的rpc服务

    这里主要使用的是jayson包,使用jayson实现rpc server端十分简单,如下: var jayson = require('jayson') // create a server var ...