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中向开发 ...
随机推荐
- oracle的序列号(sequence)
oracle的自增列,要采用序列号(sequence). 初始化阶段要手动建立一个sequence,然后插入的时候,还要手动自己去读这个sequence的nextval赋给相关字段,如ID,麻烦的很. ...
- SEH:结构化异常处理 学习
SEH:结构化异常处理 结构化异常处理机制提供了一个操作系统,用于优化结构的方案,为客户提供更强大的程序执行环境.试想一下,你写程序不用考虑内存访问错误,那里是空指针错误,一直在按照程序的逻辑结构来写 ...
- (十二)break,continue
class Break { //break,continue public static void main(String[] args) { //break for(int i =0;i<=5 ...
- C++之指针使用
C++指针使用的好坏直接反映了编程人员水平的高低,下面从指针和数组的区别.指针参数是如何传递内存.野指针.malloc/free.new/delete和内存耗尽怎么办方面进行总结. 一 指针和数组对比 ...
- 20165222 实验一java开发环境的熟悉
实验内容及步骤 实验一 Java开发环境的熟悉-1 1 建立“自己学号exp1”的目录 2 在“自己学号exp1”目录下建立src,bin等目录 3 javac,java的执行在“自己学号exp1”目 ...
- HttpModule和HttpHandler -- 系列文章
ASP.NET 生命周期 在ASP.Net2.0中使用UrlRewritingNet实现链接重写 IHttpModule实现URL重写 使用IHttpHandler防盗链 HttpModule,Htt ...
- linux基础命令复习
1.ls 查看文件和文件夹 1).ls -a 查看文件和文件夹,包括隐藏的 2).ls -l 查看文件和文件夹详情 3).ls -lh 查看文件和文件夹详情,自动生成文件大小单位 4).ls ...
- 一个detect问题引发的一系列思考
在用BoneCP的时候,发现一个JVM日志中报了一个异常,大意是“探测(detect)到有数据库链接没有关闭”(不得不说JVM的强大),但是我用的是连接池里面的链接啊,怎么会需要关闭呢? 有问题首先找 ...
- 【openCV学习笔记】【2】读取并播放一段视频
#include <iostream> #include <opencv/highgui.h> int main(int argc, char** argv){ cvNamed ...
- FTP,FTPS,FTPS与防火墙
昨天搭建了一台FTPS服务器,过程中学习了很多不清楚的知识点,还有遇到的问题,记录一下. (大部分内容汇集.整理自网络) 一. 关于FTP传输模式 众所周知,FTP传输有两种工作模式,Active M ...