【问题标题】:Animating UIScrollView rightwards向右动画 UIScrollView
【发布时间】:2016-10-14 18:20:51
【问题描述】:

如何创建以匀速向右移动/滚动可见图像部分的动画?或者更确切地说:如何向右动画 UIScrollView?

示例:图像宽度为 4968 像素。该图像用作全屏背景图像。开始时应该只能看到 4968px(图像宽度)的前 1242px。可见图像部分应以匀速向右移动。在任何时候,1242px 的图像都是可见的。当图像结束时,应该迭代这个过程。

到目前为止,这是我的代码。理解我想要完成的事情并没有什么用,因为它不完整,但是您看到我在函数更新等中使用了它。

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)
            }
        }))
    }
}

【问题讨论】:

    标签: swift uiviewcontroller uiscrollview uiimageview


    【解决方案1】:

    如果你想让游戏背景匀速移动,你根本不需要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
    }
    

    这个想法是你不断地将两个图像位置移动到左侧,并在某个阈值处将左侧图像“包裹”到右侧。

    【讨论】:

    • 很好的解决方案,位置变化适用于第一张图像。 “rollImageAroundIfNeeded”函数没有执行,你能编辑你的答案吗?
    【解决方案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)
        }
    }
    

    【讨论】:

    • 这对 OP 的回答有点不完整。 1) OP 要求连续、迭代滚动 2) OP 要求均匀速度,+[UIView animateWithDuration] 默认情况下会有缓动 3) 这不会阻止用户滚动
    猜你喜欢
    • 1970-01-01
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多