UIScrollView animation to the right

How to create an animation that moves / scrolls the visible part of the image at a uniform speed to the right? OR sooner: how to animate UIScrollView to the right?

Example: image width is 4968 pixels. The image is used as a full-screen background image. Only the first 1242px of 4968px (image width) should be visible at the beginning. The section of the visible image should move at the same speed to the right. 1242px images are visible at any time. When the end of the image is reached, this process must be repeated.

Visualization of my question

Here is my code so far. It is not useful to understand what I want to achieve, because it is incomplete, but you see that I used it in updating a function, etc.

override func update(_ currentTime: TimeInterval) {
    if gameStarted == true {
        enumerateChildNodes(withName: "background", using: ({
            (node, error) in

            let bg = node as! SKSpriteNode

            bg.position = CGPoint(x: bg.position.x - 2, y: bg.position.y)
            }
        }))
    }
}
+4
2

UIScrollView , , .

, , .

var imageWidth : Float = 4968
var viewPortWidth : Float = 1242 
var velocity : Float = 10
var currentNodeIndex : Int = 0
let backgrounds : [String] = ["background0", "background1"]
var currentNodeName : String {
    get {
        return backgrounds[currentNodeIndex]
    }
}

func setup() {
    node : SKNode = SKSpriteNode(imageNamed:"backgroundImage")
    node.name = "background0"
    node1 : SKNode = SKSpriteNode(imageNamed:"backgroundImage")
    node1.name = "background1"
    addChild(node)
    addChild(node1)
    node.position = CGPoint(x: imageWidth, y: 0)
    node1.position = CGPoint(x: 0, y: 0)
}

override func update(_ currentTime: TimeInterval) {
    if gameStarted == true {
        backgrounds.forEach {
            enumerateChildNodes(withName: $0, using: {(node, error) in
                node.position = CGPoint(x: node.position.x - velocity, y: node.position.y)
            })
        }
    }
}

private func rollImageAroundIfNeeded() {
    if needsRollImages() {
        rollImagesAround()
    }
}

private func needsRollImages() -> Bool {
    node : SKNode = childNode(withName:currentNodeName)!
    return node.position.x < 2 * (screenWidth - imageWidth)
}

private func rollImageAround() {
    node : SKNode = childNode(withName:currentNodeName)!
    node.position = CGPoint(x: node.position.x + 2 * imageWidth, y: node.position.y)
    currentNodeIndex = (currentNodeIndex + 1) % 2
}

, , "" .

+2

contentOffset

@IBOutlet weak var scrollView: UIScrollView!

override func viewDidAppear(animated: Bool) {
    // This code scrolls to the point (x: 4000, y:0) in 5 seconds
    UIView.animateWithDuration(5) { [unowned self] in
        self.scrollView.contentOffset = CGPoint(x: 4000, y: 0)
    }
}
+2

Source: https://habr.com/ru/post/1657804/


All Articles