使用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. Ubuntu 16.04 RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller” 不能上网

    来源:http://forum.ubuntu.org.cn/viewtopic.php?f=116&t=463646 1.执行如下命令 uname -a sudo lspci -knn sud ...

  2. 使用单体模式设计原生js插件

    ----------基于上次写的jquery插件进行改造  http://www.cnblogs.com/GerryOfZhong/p/5533773.html 背景:jQuery插件依赖jQuery ...

  3. 解决图片浮动调节不了的问题(使用vertical-align属性)

    vertical-align: middle; vertical-align 属性设置元素的垂直对齐方式. baseline  默认.元素放置在父元素的基线上.sub 垂直对齐文本的下标.super ...

  4. linux 升级-杂

    apt-cache search linux apt-cache search linux | grep generic apt-cache search linux | grep 4.10. apt ...

  5. ASP.NET MVC Web API 学习笔记---联系人增删改查

    本章节简单介绍一下使用ASP.NET MVC Web API 做增删改查. 目前很多Http服务还是通过REST或者类似RESP的模型来进行数据操作的. 下面我们通过创建一个简单的Web API来管理 ...

  6. 【c++】字符串流输出恢复状态问题

    缘起 #include <iostream> #include <sstream> using namespace std; int main() { istringstrea ...

  7. jQuery中hover与mouseover和mouseout的区别分析

    本文实例分析了jQuery中hover与mouseover和mouseout的区别.分享给大家供大家参考,具体如下: 以前一直以为在jquery中其实mouseover和mouseout两个事件等于h ...

  8. XML的基本概念和Android下的使用

    1. XML的基本概念 1. 什么是XML: 1). XML是指可扩展标记语言(eXtensible Markup Language),它是一种标记语言,很类似HTML.它被设计的宗旨是表示数据,而非 ...

  9. DDD学习笔记(一)

    最近开始筹备一个电商项目. 其实是公司的老本行了. 但今年公司希望在做项目的同时, 沉淀出一套针对电商的基础产品. 这样可以提高新项目的开发效率, 减少重复劳动. 那现如今, DDD(领域驱动设计)应 ...

  10. 禁止选中页面内容-兼容ie、firefox、chrome

    使用js禁止用户选中网页上的内容,IE及Chrome下的方法一样.使用onselectstart, 比如: 在body中加入<body onselectstart="return fa ...