用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也是非常关键的哦

最新文章

  1. HDU 3669 Cross the Wall
  2. Docker-compose
  3. mac系统小记
  4. Could not find class &#39;android.support.v4.view.ViewPager&#39;, referenced from me
  5. 使用 Centos 7 的 systemctl 管理服务
  6. Atitit。如何实现dip, di ,ioc ,Service Locator的区别于联系
  7. APMServ 配置记录
  8. hadoop2.2.0的WordCount程序
  9. Android-67-Tomcat启动出错:Server Tomcat v7.0 Server at localhost failed to start.
  10. MySQL 5.6 中 TIMESTAMP 的变化
  11. mysql版本,根据经纬度定位排序sql
  12. bzoj2876 [Noi2012]骑行川藏
  13. Android编译自己的程序到/system/bin
  14. virtualbox 设置centos7 双网卡上网
  15. docker学习端口连接docker容器---第四章节
  16. SonarQube 集成 GitLabCI
  17. python day14--内置函数二
  18. Android 布局学习之——LinearLayout属性baselineAligned的作用及baseline
  19. gdb 调试main
  20. 使用as3crypto在Flex中实现AES加密

热门文章

  1. 玩转mongodb(一):初识mongodb
  2. 【设计模式】观察者模式 Observer Pattern
  3. 如何在.Net Core MVC中为动态表单开启客户端验证
  4. HTML5的拖放事件
  5. [转]ASP.NET MVC4中@model使用多个类型实例的方法
  6. [转]SQL Server 2008- Get table constraints
  7. CentOS普通用户没有sudo权限
  8. SpringBoot(三) Core Features: External Configuration(配置文件)
  9. Spring系列之——使用了哪些设计模式
  10. Springboot集成SpringData JPA