ParallaxEffect

  ParallaxEffect是一种用简单的2D贴图来模拟3D效果的简易方法。譬如一棵树,摄像机俯视时,当树远离摄像机时,树顶偏远,当树靠近,树顶偏近。苹果官方Adventure中展示了此种技术。

@interface APAParallaxSprite : SKSpriteNode

@property (nonatomic) BOOL usesParallaxEffect;
@property (nonatomic) CGFloat virtualZRotation; /* If initialized with this method, sprite is set up for parallax effect; otherwise, no parallax. */
- (id)initWithSprites:(NSArray *)sprites usingOffset:(CGFloat)offset; - (void)updateOffset; @end @interface APAParallaxSprite ()
// 指定顶部Node与底部Node最大偏移。
@property (nonatomic) CGFloat parallaxOffset;
@end @implementation APAParallaxSprite #pragma mark - Initialization
- (id)initWithSprites:(NSArray *)sprites usingOffset:(CGFloat)offset {
self = [super init]; if (self) {
_usesParallaxEffect = YES; // Make sure our z layering is correct for the stack.
CGFloat zOffset = 1.0f / (CGFloat)[sprites count]; // All nodes in the stack are direct children, with ordered zPosition.
CGFloat ourZPosition = self.zPosition;
NSUInteger childNumber = ;
for (SKNode *node in sprites) {
node.zPosition = ourZPosition + (zOffset + (zOffset * childNumber));
[self addChild:node];
childNumber++;
} _parallaxOffset = offset;
} return self;
} #pragma mark - Copying
- (id)copyWithZone:(NSZone *)zone {
APAParallaxSprite *sprite = [super copyWithZone:zone];
if (sprite) {
sprite->_parallaxOffset = self.parallaxOffset;
sprite->_usesParallaxEffect = self.usesParallaxEffect;
}
return sprite;
} #pragma mark - Rotation and Offsets
- (void)setZRotation:(CGFloat)rotation {
// Override to apply the zRotation just to the stack nodes, but only if the parallax effect is enabled.
if (!self.usesParallaxEffect) {
[super setZRotation:rotation];
return;
} if (rotation > 0.0f) {
self.zRotation = 0.0f; // never rotate the group node // Instead, apply the desired rotation to each node in the stack.
for (SKNode *child in self.children) {
child.zRotation = rotation;
} self.virtualZRotation = rotation;
}
} - (void)updateOffset {
SKScene *scene = self.scene;
SKNode *parent = self.parent; if (!self.usesParallaxEffect || parent == nil) {
return;
} CGPoint scenePos = [scene convertPoint:self.position fromNode:parent]; // Calculate the offset directions relative to the center of the screen.
// Bias to (-0.5, 0.5) range.
CGFloat offsetX = (-1.0f + (2.0 * (scenePos.x / scene.size.width)));
CGFloat offsetY = (-1.0f + (2.0 * (scenePos.y / scene.size.height))); CGFloat delta = self.parallaxOffset / (CGFloat)self.children.count; int childNumber = ;
for (SKNode *node in self.children) {
node.position = CGPointMake(offsetX*delta*childNumber, offsetY*delta*childNumber);
childNumber++;
}
} @end

  核心算法在udpate方法中。

ParallaxEffect的更多相关文章

  1. 视差滚动(Parallax Scrolling)效果的原理与实现

    视差滚动(Parallax Scrolling)效果的原理与实现1.视差滚动效果的主要特点:    1)直观的设计,快速的响应速度,更合适运用于单页面    2)差异滚动 分层视差    页面上很多的 ...

  2. leaflet地图库

    an open-source JavaScript libraryfor mobile-friendly interactive maps Overview Tutorials Docs Downlo ...

  3. [Android Pro] AndroidX重构和映射

    原文地址:https://developer.android.com/topic/libraries/support-library/refactor https://blog.csdn.net/ch ...

  4. iOS7向开发者开放的新功能汇总

    转自:http://www.25pp.com/news/news_28002.html iOS7才放出第二个测试版本,我们已经看到了不少的新功能和新改变.最近,科技博客9to5Mac将iOS7中向开发 ...

随机推荐

  1. js 柯里化Currying

    今天读一篇博客的时候,看都有关柯里化的东西,由于好奇,特意查了一下,找到一篇比较好的文章,特意收藏. 引子先来看一道小问题:有人在群里出了到一道题目:var s = sum(1)(2)(3) .... ...

  2. verilog case 语句合并问题

    有时候在case语句中会有不同选择执行相同操作的情况,为了简化代码,可以将其合并. 以下解答来自百度知道(由于排版问题,有相应修改): reg [1:0]addr_cnt=2'b11; reg rea ...

  3. MFC中控制Tips的显示 - lingyun1120

    from:  http://www.cnblogs.com/lingyun1120/archive/2011/11/09/2243279.html MFC中为ToolTips生成了一个类CToolTi ...

  4. linux之epoll

    1. epoll简介 epoll 是Linux内核中的一种可扩展IO事件处理机制,最早在 Linux 2.5.44内核中引入,可被用于代替POSIX select 和 poll 系统调用,并且在具有大 ...

  5. 【PS实例】照片拼图的制作

    本系列教程将开始讲解PS的一些制作实例,通过实例的讲解同时介绍各种工具和面板机快捷键的使用,这样能够让大家更有兴趣学习,在学习的同时能够创造出自己喜欢的东西.本人使用的教程都是根据本人多次调试制作,仅 ...

  6. SQLite3的运用

    1.导入库文件:libsqlite3. 2..m文件的代码: // // ViewController.m // SQLite3的运用 // // Created by PengYunjing on ...

  7. Java开发过程中乱码问题理解

    1.Java编译器(即编译成class文件时) 用的是unicode字符集.2.乱码主要是由于不同的字符集相互转换导致的,理论上各个字符的编码规则是不同的,是不能相互转换的,所以根本解决乱码的方法就是 ...

  8. Java 构造器或构造方法

    构造方法的定义 构造方法也叫构造器或者构造函数 构造方法与类名相同,没有返回值,连void都不能写 构造方法可以重载(重载:方法名称相同,参数列表不同) 如果一个类中没有构造方法,那么编译器会为类加上 ...

  9. Linux 简单字符设备驱动

    1.hello_drv.c (1) 初始化和卸载函数的格式是固定的,函数名自定义 (2) printk是内核的打印函数,用法与printf一致 (3) MODULE_LICENSE:模块代码支持开源协 ...

  10. 从ROS bag文件中提取图像

    从ROS bag文件中提取图像 创建launch文件,如下: export.launch <launch> <node pkg="rosbag" type=&qu ...