2014-12-15 15 views
2

기본적으로 SpriteKit 프로젝트에서 UIScrollView를 작성해야하지만 SKButtons (버튼 관리 용 사용자 정의 SKNode 클래스)를 추가하는 데 많은 문제가 있습니다. 그래서 터치 제스처를 사용하여 스크롤 가능한 SKNode를 만들었지 만, 분명히이 막대에는 기본 UIScrollView 가속 기능이 없습니다 : 내가 찾고있는 기능입니다. 이 어리석게도 잘 작동하지만가속화 된 SpriteKit 사용자 정의 UIScrollView

-(void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    [blueNode setPosition:CGPointMake(blueNode.position.x, scrollerUI.contentOffset)]; 
} 

:이 코드를 사용

enter image description here

:

그래서이 같은 위치의 모든 변화를 네이티브있는 UIScrollView를 추가하여이 문제를 추월하고 잡으려고 시도 버튼을 추가하면 터치 제스쳐가 버튼의 터치 이벤트를 인식하지 못한다는 사실을 잊어 버렸습니다. UIScrollView가 우선 순위를가집니다.

어쩌면 어리석은 질문 일 수도 있지만 실제로 어떻게 알아 내야할지 모르겠다. 아마 내 자신의 가속 방법을 프로그래밍?

+0

uikit 요소 (uiscrollview)를 장면에 직접 추가하고 있습니까? 아마도 최선의 방법은 아닙니다. 그냥 sknode를 슬라이드하는 것이 훨씬 쉽습니다. 그리고 네, 당신은 일종의 완화와 가속을 프로그램해야합니다. – hamobi

+0

실제로 가속도, 속도 등을 만들어야합니다. – Maarten

+0

이 점은 흥미로운 질문입니다. 그것을 upvoted 및 누군가에 의해 훌륭한 답변을 기대. – Maarten

답변

2

은 내가 접촉을 처리 할 수있는 솔루션을 함께했다 생각합니다. UIScrollView이 스크롤 할 수 있도록 모든 접촉을 먹기 때문에 SKScene으로 전달해야합니다. 당신은 UIScrollView를 서브 클래 싱하여 해당 작업을 수행 할 수 있습니다

class SpriteScrollView: UIScrollView { 

    var scene: SKScene? 


    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { 
     scene?.touchesBegan(touches, withEvent: event) 
    } 

    override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) { 
     scene?.touchesMoved(touches, withEvent: event) 
    } 

    override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) { 
     scene?.touchesCancelled(touches, withEvent: event) 
    } 

    override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) { 
     scene?.touchesEnded(touches, withEvent: event) 
    } 
} 

그런 다음 장면은 터치가 어떤 노드를 명중하고 적절하게 그 노드에 접촉을 보내는 경우 볼 필요

. 그래서 SKScene이 추가 :이 하나의 접촉을위한 잘 오류를 처리하지 않습니다

var nodesTouched: [AnyObject] = [] 

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { 
    super.touchesBegan(touches, withEvent: event) 

    let location = (touches.first as! UITouch).locationInNode(self) 
    nodesTouched = nodesAtPoint(location) 

    for node in nodesTouched as! [SKNode] { 
     node.touchesBegan(touches, withEvent: event) 
    } 
} 

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) { 
    super.touchesMoved(touches, withEvent: event) 

    for node in nodesTouched as! [SKNode] { 
     node.touchesMoved(touches, withEvent: event) 
    } 
} 

override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) { 
    super.touchesCancelled(touches, withEvent: event) 
    for node in nodesTouched as! [SKNode] { 
     node.touchesCancelled(touches, withEvent: event) 
    } 
} 

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) { 
    super.touchesEnded(touches, withEvent: event) 
    for node in nodesTouched as! [SKNode] { 
     node.touchesEnded(touches, withEvent: event) 
    } 
} 

주, 당신의 일부를 포장하는 데 필요한 아래 if 문이 제대로이 작업을 수행하려는 경우에 주조 강제

나는이 질문이 몇 개월 된 사람에게 도움이되기를 바랍니다.

1

한 가지 가능한 솔루션은 현장에 탭 제스처를 추가하는 것입니다 :

override func didMoveToView(view: SKView) { 
    let tap : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("tapped:")) 
    view.addGestureRecognizer(tap) 
} 

func tapped(tap : UITapGestureRecognizer) { 
    let viewLocation = tap.locationInView(tap.view) 
    let location = convertPointFromView(viewLocation) 
    let node = nodeAtPoint(location) 

    if node.name == "lvlButton" { 
     var button = node as! SKButton 
     button.touchBegan(location) 
    } 
} 
+0

감사합니다. 이것은 나를 위해 아주 잘 작동했습니다! 나에게 몇 시간의 코드를 저장했다 !!! –