用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. LDA理解

    LDA只是一个求解思路. 1.理解LDA首先要理解EM算法,EM不能叫做一个算法,只是一个思想:它要求解的其实是一个极大似然估计,就是我用已知量去求解导致这个已知量出现的最大概率,而在这里又恰恰有点偏 ...

  2. 笔记五:python字符串

    一:学习内容 字符串类型 字符串类型判断 字符串类型互转 字符串小练习 二:字符串类型 1. basestring 在python中和字符串相关的数据类型为:str和unicode,他们都是bases ...

  3. vue-cli的工程如何正确使用Google Analytics?

    前言 最方便的方法,莫过于使用vue-analytics:https://github.com/MatteoGabriele/vue-analytics. 包是有了,可是真正使用起来会发现Google ...

  4. mybatis之@Select、@Insert、@Delete、@Param

    之前学习的时候,看到别人在使用mybatis时,用到@Select.@Insert.@Delete.@Param这几个注解,故楼主研究了一下,在这里与大家分享 当使用这几个注解的时候,可以省去写Map ...

  5. Mvc Moq HttpContext

    1: public class MockMvcHttpContext 2: { 3: public Moq.Mock<System.Web.HttpContextBase> Context ...

  6. SSH框架搭建步骤总结以及Hibernate二级缓存,查询缓存

    二级缓存.查询缓存 一级缓存: 默认启动,生命周期是和session同步的,session独享 二级缓存: 需要加载配置信息,生命周期是和应用服务器同步,session共享 1:在hibernate. ...

  7. Android sdk manager 下载速度慢的问题

    不多说了,直接附上方法: 首先打开Ecplise 中Android sdk manager,打开后, 在此窗口的上方打开偏好设置选项,然后在里面设置HTTP Proxy server和HTTP Pro ...

  8. gRPC 的route_guide例子

      本文的例子代码在: https://github.com/grpc/grpc-go/tree/master/examples/route_guide 功能就类似目前LBS一样,在每个位置上报一些文 ...

  9. 【学习笔记】--- 老男孩学Python,day7 python中is 和 == 的区别 encode decode

    is比较的是id(内存地址)是不是一样,==比较的是值是不是一样 Python中,万物皆对象!万物皆对象!万物皆对象!(很重要,重复3遍) 每个对象包含3个属性,id,type,value id就是对 ...

  10. 原生javascript实现图片自动轮播和点击轮播代码

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...