ParallaxEffect
【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的更多相关文章
- 视差滚动(Parallax Scrolling)效果的原理与实现
视差滚动(Parallax Scrolling)效果的原理与实现1.视差滚动效果的主要特点: 1)直观的设计,快速的响应速度,更合适运用于单页面 2)差异滚动 分层视差 页面上很多的 ...
- leaflet地图库
an open-source JavaScript libraryfor mobile-friendly interactive maps Overview Tutorials Docs Downlo ...
- [Android Pro] AndroidX重构和映射
原文地址:https://developer.android.com/topic/libraries/support-library/refactor https://blog.csdn.net/ch ...
- iOS7向开发者开放的新功能汇总
转自:http://www.25pp.com/news/news_28002.html iOS7才放出第二个测试版本,我们已经看到了不少的新功能和新改变.最近,科技博客9to5Mac将iOS7中向开发 ...
随机推荐
- 剑指Offer面试题:1.实现单例模式
一 题目:实现单例模式Singleton 题目:设计一个类,我们只能生产该类的一个实例. 只能生成一个实例的类是实现了Singleton(单例)模式的类型.由于设计模式在面向对象程序设计中起着举足轻重 ...
- PS基础教程[4]如何载入笔刷
笔刷是我们制作图片时的一个很好的工具,能够快速方便的帮助我们制作出很多现有的效果,所以我们都会制作很多的笔刷保存起来载入到PS中方便我们使用.本次系类经验的第四篇就来介绍一下笔刷的导入. 方法 1.笔 ...
- GIT多人合作开发
. 建立代码仓库(专门用于团队开发的代码仓库) ============================================================================ ...
- 记一次愚蠢的gradle操作
今晚把工作移植到mac平台,在用gradle命令 exec ./gradlew --parallel --info assembleDebug 打包apk时卡住,gradle一直处于下载状态,过了几分 ...
- fn project AWS Lambda 格式 functions
Creating Lambda Functions Creating Lambda functions is not much different than using regular funct ...
- PHP实现同服务器多个二级域名共享 SESSion
现在很多分类信息网站都会分出很多个二级域名出来,比如:sh.ganji.com(上海赶集网), su.ganji.com(苏州赶集网)等等,像这种拥有多个二级域名的网站,该如何实现同步共享sessio ...
- windows下安装storm1.1.0并启动
64位windows安装storm前需要先搞定zookeeper和python,所以下面我们3步走: 一.zookeeper 1.上https://zookeeper.apache.org/点击下方d ...
- FPGA层次结构和复位策略
FPGA设计中,层次结构设计和复位策略影响着FPGA的时序.在高速设计时,合理的层次结构设计与正确的复位策略可以优化时序,提高运行频率. 设计中,合理的层次结构是我们所追求的. 划分时,按照逻辑分区将 ...
- java显示网格————————
总结:看图 +---+---+ | | | | | | +---+---+ */ package com.aaa; //在屏幕上显如下网格 public class adga { public sta ...
- appium控制Android按键
按键的keycode,如下: KEYCODE_HOME Home键 3 KEYCODE_BACK 返回键 4 KEYCODE_CALL 拨号键 5 KEYCODE_ENDCALL 挂机键 6 KEYC ...