2016-10-14 2 views
4

볼 수있는 이미지 섹션을 일정한 속도로 오른쪽으로 이동/스크롤하는 애니메이션을 만드는 방법은 무엇입니까? 오히려 : UIScrollView에 오른쪽으로 애니메이션을 적용하는 방법은 무엇입니까?오른쪽으로 UIScrollView 애니메이션하기

예 : 이미지 너비는 4968 픽셀입니다. 이미지는 전체 화면 배경 이미지로 사용됩니다. 처음에는 4968px (이미지 너비)의 처음 1242 픽셀 만 볼 수 있어야합니다. 볼 수있는 이미지 섹션은 균일 한 속도로 오른쪽으로 이동해야합니다. 언제든지 1242 픽셀의 이미지를 볼 수 있습니다. 이미지의 끝에 도달하면이 프로세스를 반복해야합니다.

Visualisation of my question

여기에 지금까지 내 코드입니다. 불완전하기 때문에 원하는 것을 이해하는 것은 유용하지 않지만 함수 업데이트 등에서 사용하는 것을 볼 수 있습니다.

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

답변

2

애니메이션을 적용 할 수 있습니다.

그러나 원활하게 반복하기 위해서는 그 시간에 두 개의 이미지를 저장해야합니다.

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 
} 

두 이미지의 위치를 ​​왼쪽으로 계속 이동하고 일부 임계 값에서 왼쪽 이미지를 오른쪽으로 "랩"하는 것이 좋습니다.

+0

훌륭한 솔루션, 위치 변경은 첫 번째 이미지에 대해 잘 작동합니다. "rollImageAroundIfNeeded"기능이 실행되지 않습니다. 답변을 편집 할 수 있습니까? –

2

당신은 당신이 균일 한 속도로 이동 게임의 배경이 원한다면 당신은 모든 UIScrollView 필요하지 않습니다 그 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) 
    } 
} 
+1

이것은 OP에 대한 약간 불완전한 답변입니다. 1) OP는 반복적이고 반복적 인 스크롤을 요청합니다. 2) OP는 일정한 속도를 요구하고''[UIView animateWithDuration]'''은 기본적으로 여유 공간을 갖습니다. 3) 사용자 스크롤을 방지하지 않습니다. –

관련 문제