用UIScrollView产生视差效果

效果:

高级效果:

源码:

MoreInfoView.h  +  MoreInfoView.m

//
// MoreInfoView.h
// YXCell
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import <UIKit/UIKit.h> @interface MoreInfoView : UIView @property (nonatomic, strong) UIImageView *imageView; @end
//
// MoreInfoView.m
// YXCell
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "MoreInfoView.h" @implementation MoreInfoView - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
CGRect rect = frame;
self.layer.borderWidth = 0.5f;
self.layer.borderColor = [UIColor blackColor].CGColor;
self.layer.masksToBounds = YES; _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(-, ,
rect.size.width + *,
rect.size.height)];
[self addSubview:_imageView];
}
return self;
} @end

RootViewController.m

//
// RootViewController.m
// YXCell
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "RootViewController.h"
#import "MoreInfoView.h" @interface RootViewController ()<UIScrollViewDelegate> @property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, assign) CGFloat k;
@property (nonatomic, assign) CGFloat b; @end @implementation RootViewController - (void)linearFunctionPointA:(CGPoint)pointA
pointB:(CGPoint)pointB
{
CGFloat x1 = pointA.x; CGFloat y1 = pointA.y;
CGFloat x2 = pointB.x; CGFloat y2 = pointB.y; _k = calculateSlope(x1, y1, x2, y2);
_b = calculateConstant(x1, y1, x2, y2);
} - (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor]; // 计算斜率
[self linearFunctionPointA:CGPointMake(, -)
pointB:CGPointMake(, )]; _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
_scrollView.delegate = self;
_scrollView.pagingEnabled = YES;
[self.view addSubview:_scrollView]; NSArray *picArray = @[[UIImage imageNamed:@""],
[UIImage imageNamed:@""],
[UIImage imageNamed:@""],
[UIImage imageNamed:@""],
[UIImage imageNamed:@""]]; [picArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
MoreInfoView *show = \
[[MoreInfoView alloc] initWithFrame:CGRectMake(idx*self.view.bounds.size.width, ,
self.view.bounds.size.width,
self.view.bounds.size.height)];
show.imageView.image = obj; [_scrollView addSubview:show];
}]; _scrollView.contentSize = CGSizeMake(picArray.count*self.view.bounds.size.width,
self.view.bounds.size.height);
} - (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat X = scrollView.contentOffset.x; [scrollView.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
MoreInfoView *tmp = obj; if ([tmp isKindOfClass:[MoreInfoView class]])
{
// 产生视差效果
CGRect rect = tmp.imageView.frame;
rect.origin.x = _k * (X - idx*) + _b;
tmp.imageView.frame = rect;
}
}];
} // 计算用
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

需要注意的地方:

1. 这个地方的值不是随便设定的哦:)

2. 修改那个270的值来达到上图显示的各种效果

3. 下面的X - idx*320也是非常关键的哦

用UIScrollView产生视差效果的更多相关文章

  1. Swift - 用UIScrollView实现视差动画效果

    Swift - 用UIScrollView实现视差动画效果 效果 源码 https://github.com/YouXianMing/Swift-Animations // // MoreInfoVi ...

  2. UIScrollView视差效果动画

    UIScrollView视差效果动画 效果 源码 https://github.com/YouXianMing/Animations // // ScrollImageViewController.m ...

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

    使用UITableView实现图片视差效果 视差效果如下: 原理: 根据偏移量计算不同的移动速度,so easy! // // RootTableViewController.h // TableVi ...

  4. 滚动视差效果——background-attachment

    滚动视差效果的实现原理是在同一个页面上将页面元素分为多层,例如可以分为背景.内容.贴图层,在滚动页面的时候让三者滚动的速度不一,从而在人的视觉上能够形成一种立体的近似效果.最近在做一个项目wiki的时 ...

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

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

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

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

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

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

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

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

  9. UITableViewCell图片视差效果

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

随机推荐

  1. android学习-数据存储(一)-----SQLite源码分析

    分析SQLiteDatabase.java,SQLiteStatement.java,SQLiteSession.java,SQLiteConnectionPool.java,SQLiteConnec ...

  2. elasticsearch(三) 之 elasticsearch目录介绍和配置文件详解

    目录 elasticsearch 配置 目录详情 (config) 配置文件 elasticsearch.yml 配置集群名称(cluster.name) 配置 network.host 更改数据和储 ...

  3. windows server 2012 如何开启 hyper-v 并创建虚拟机

    当我们拿到一台新的windows server 2012 服务器的时候,默认hyper-v是没有开启的,如果我们要在windows server上面创建虚拟机,那么我们也就需要开启hyper-v. 开 ...

  4. UML——六大关系整理

    UML——六大关系整理 1.定义 是一种面向对象的建模语言,它是运用统一的.标准化的标记和定义实现对软件系统进行面向对象的描述和建模(百度百科). 2.六种关系 这六种关系分别为,继承.实现.关联.聚 ...

  5. macOS 中文件属性有at符号

    在mac os 下 HFS+的文件系统里,有时候有些文件会附加上mac的专有属性,@属性就表示文件或文件夹是来自互联网下载 xattr -l 文件名:查看attrxattr -d 属性名:删除attr

  6. java SE 入门之输入输出(第四篇)

    在第一篇,八大基本类型的时候,我就介绍了输出,当然,这些输出都是简单的,后续写到流的时候,在细化输入输出. 现在只要求看懂输入输出.输入其实就是接受键盘的输入. public class Hello ...

  7. C# Mysql Dapper和原生sql 插入和查询速度比较

    1.表中有三个字段,已经有100多万条数据,每次插入10万条数据 时间单位:秒 秒 Dapper批量Model插入时间:40.6165513,Dapper单条Model插入时间:95.9492972, ...

  8. 热更新--动态加载framework

    1.准备工作:先自己封装一个framework:http://www.cnblogs.com/sunjianfei/p/5781863.html 2.把封装好的framework压缩成zip,放到本地 ...

  9. Java多线程学习之线程的取消与中断机制

    任务和线程的启动很容易.在大多数情况下我们都会让他们运行直到结束,或是让他们自行停止.但是,有时我们希望提前结束任务或是线程,可能是因为用户请求取消,或是线程在规定时间内没有结束,或是出现了一些问题迫 ...

  10. 十九、curator recipes之PathChildrenCache

    简介 curator可以监听路径下子节点的变更操作,如创建节点,删除节点 官方文档:http://curator.apache.org/curator-recipes/path-cache.html ...