Infinite Scroll View

Infinite background

This is game entry.
We need to use infinite background like Flappy bird.
How to make it?

SKScrollingNode

This can scroll X, or Y.

SKScrollingNode.h

#import <UIKit/UIKit.h>
#import <SpriteKit/SpriteKit.h>

@interface SKScrollingNode : SKSpriteNode

@property (nonatomic)BOOL directionX;        // YES : X, NO : Y
@property (nonatomic) CGFloat scrollingSpeed;

+ (id) scrollingNodeWithImageNamed:(NSString *)name inContainerSize:(float) size x:(BOOL)x;
- (void) update:(NSTimeInterval)currentTime;
@end

SKScrollingNode.m


#import "SKScrollingNode.h"

@implementation SKScrollingNode

+ (id) scrollingNodeWithImageNamed:(NSString *)name inContainerSize:(float) size x:(BOOL)x {
    UIImage * image = [UIImage imageNamed:name];
    
    SKScrollingNode * node = nil;
    float image_size = 0.0;
    
    if (x) {
        node = [SKScrollingNode spriteNodeWithColor:[UIColor clearColor] size:CGSizeMake(size, image.size.height)];
        image_size = image.size.width;
    }
    else {
        node = [SKScrollingNode spriteNodeWithColor:[UIColor clearColor] size:CGSizeMake(image.size.width, size)];
        image_size = image.size.height;
    }
    
    node.scrollingSpeed = 1;
    node.directionX = x;
    
    float total = 0;
    while(total<(size + image_size)){
        SKSpriteNode * child = &#91;SKSpriteNode spriteNodeWithImageNamed:name&#93;;
        &#91;child setAnchorPoint:CGPointZero&#93;;
        
        if(x) {
            &#91;child setPosition:CGPointMake(total, 0)&#93;;
            &#91;node addChild:child&#93;;
            total+=child.size.width;
        }
        else {
            &#91;child setPosition:CGPointMake(0, total)&#93;;
            &#91;node addChild:child&#93;;
            total+=child.size.height;
        }
    }
    return node;
}

- (void) update:(NSTimeInterval)currentTime {
    if(self.directionX) {
        &#91;self updateX:currentTime&#93;;
    }
    else {
        &#91;self updateY:currentTime&#93;;
    }
}

- (void) updateX:(NSTimeInterval)currentTime {
    
    &#91;self.children enumerateObjectsUsingBlock:^(SKSpriteNode * child, NSUInteger idx, BOOL *stop) {
        child.position = CGPointMake(child.position.x-self.scrollingSpeed, child.position.y);
        if (child.position.x <= -child.size.width){
            float delta = child.position.x+child.size.width;
            child.position = CGPointMake(child.size.width*(self.children.count-1)+delta, child.position.y);
        }
    }&#93;;
}

- (void) updateY:(NSTimeInterval)currentTime {
    &#91;self.children enumerateObjectsUsingBlock:^(SKSpriteNode * child, NSUInteger idx, BOOL *stop) {
        child.position = CGPointMake(child.position.x, child.position.y - self.scrollingSpeed);
        if (child.position.y <= -child.size.height){
            float delta = child.position.y+child.size.height;
            child.position = CGPointMake(child.position.x, child.size.height*(self.children.count-1)+delta);
        }
    }&#93;;
}
    
@end
&#91;/cpp&#93;

<h3>How to use</h3> 
<h4>Background.h</h4>
This is extended class.
[cpp]
#import <SpriteKit/SpriteKit.h>
#import "SKScrollingNode.h"

@interface BackgroundNode : SKScrollingNode
@end

Background.m

@implementation BackgroundNode

+ (id) scrollingNodeWithImageNamed:(NSString *)name inContainerSize:(float) size x:(BOOL)x {
    SKScrollingNode * node = [super scrollingNodeWithImageNamed:name inContainerSize:size x:x];
    node.name = @"background";
    return node;
}
@end

Scene

#import "FirstScene.h"
#import "BackgroundNode.h"

@interface FirstScene()
@property (nonatomic)BackgroundNode *background;
@end

@implementation FirstScene

-(id)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {
        self.background = [BackgroundNode scrollingNodeWithImageNamed:@"background" inContainerSize:self.frame.size.width x:YES];
        [self addChild:self.background];
    }
    return self;
}

- (void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */
    [self.background update:currentTime];
}
@end

Result

Scroll x direction
scrolling

Ref

SprityBird