2016-07-15 19 views
0

Ich habe derzeit eine vertikale Scrollview arbeitet und wie diese von einer UIScrollView-Klasse definiert:Scroll Seite Positionierung ist ausgeschaltet

scrollView = CustomScrollView(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height), scene: self, moveableNode: moveableNode, scrollDirection: .Vertical) 
    scrollView.center = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)) 
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height * 4) // * 3 makes it 3times as long as screen 
    view?.addSubview(scrollView) 

die Scrollview zeigt perfekt, aber ich habe Probleme wurden mit den Seiten immer in zeigen, die Scrollview:

let page1ScrollView = SKSpriteNode(color: SKColor.blueColor(), size: CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height)) 
    page1ScrollView.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)) 
    moveableNode.addChild(page1ScrollView) 

ich habe versucht, die Positionierung der Seiten zu ändern, aber ich kann immer noch nicht bekommen sie einige bitte zu zeigen, kann mir sagen, was ich falsch mache? Ich verstehe immer noch nicht ganz, wie CGPoint funktioniert.

Auch hier ist meine Scrollview Klasse:

var nodesTouched: [AnyObject] = [] // global 

/// Scroll direction 
enum ScrollDirection: Int { 
case None = 0 
case Vertical 
case Horizontal 
} 

/// Custom UIScrollView class 
class CustomScrollView: UIScrollView { 

// MARK: - Static Properties 

/// Touches allowed 
static var disabledTouches = false 

/// Scroll view 
private static var scrollView: UIScrollView! 

// MARK: - Properties 

/// Nodes touched. This will forward touches to node subclasses. 
private var nodesTouched = [AnyObject]() 

/// Current scene 
private let currentScene: SKScene 

/// Moveable node 
private let moveableNode: SKNode 

/// Scroll direction 
private let scrollDirection: ScrollDirection 

// MARK: - Init 
init(frame: CGRect, scene: SKScene, moveableNode: SKNode, scrollDirection: ScrollDirection) { 
    self.currentScene = scene 
    self.moveableNode = moveableNode 
    self.scrollDirection = scrollDirection 
    super.init(frame: frame) 

    CustomScrollView.scrollView = self 
    self.frame = frame 
    delegate = self 
    indicatorStyle = .White 
    scrollEnabled = true 
    userInteractionEnabled = true 
    //canCancelContentTouches = false 
    //self.minimumZoomScale = 1 
    //self.maximumZoomScale = 3 

    if scrollDirection == .Horizontal { 
     let flip = CGAffineTransformMakeScale(-1,-1) 
     transform = flip 
    } 
} 

required init?(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
} 
} 

// MARK: - Touches 
extension CustomScrollView { 

/// Began 
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    //super.touchesBegan(touches, withEvent: event) 

    for touch in touches { 
     let location = touch.locationInNode(currentScene) 

     guard !CustomScrollView.disabledTouches else { return } 

     /// Call touches began in current scene 
     currentScene.touchesBegan(touches, withEvent: event) 

     /// Call touches began in all touched nodes in the current scene 
     nodesTouched = currentScene.nodesAtPoint(location) 
     for node in nodesTouched { 
      node.touchesBegan(touches, withEvent: event) 
     } 
    } 
} 

/// Moved 
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    //super.touchesMoved(touches, withEvent: event) 

    for touch in touches { 
     let location = touch.locationInNode(currentScene) 

     guard !CustomScrollView.disabledTouches else { return } 

     /// Call touches moved in current scene 
     currentScene.touchesMoved(touches, withEvent: event) 

     /// Call touches moved in all touched nodes in the current scene 
     nodesTouched = currentScene.nodesAtPoint(location) 
     for node in nodesTouched { 
      node.touchesMoved(touches, withEvent: event) 
     } 
    } 
} 

/// Ended 
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    //super.touchesEnded(touches, withEvent: event) 

    for touch in touches { 
     let location = touch.locationInNode(currentScene) 

     guard !CustomScrollView.disabledTouches else { return } 

     /// Call touches ended in current scene 
     currentScene.touchesEnded(touches, withEvent: event) 

     /// Call touches ended in all touched nodes in the current scene 
     nodesTouched = currentScene.nodesAtPoint(location) 
     for node in nodesTouched { 
      node.touchesEnded(touches, withEvent: event) 
     } 
    } 
} 

/// Cancelled 
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) { 
    //super.touchesCancelled(touches, withEvent: event) 

    for touch in touches! { 
     let location = touch.locationInNode(currentScene) 

     guard !CustomScrollView.disabledTouches else { return } 

     /// Call touches cancelled in current scene 
     currentScene.touchesCancelled(touches, withEvent: event) 

     /// Call touches cancelled in all touched nodes in the current scene 
     nodesTouched = currentScene.nodesAtPoint(location) 
     for node in nodesTouched { 
      node.touchesCancelled(touches, withEvent: event) 
     } 
    } 
} 
} 

// MARK: - Touch Controls 
extension CustomScrollView { 

/// Disable 
class func disable() { 
    CustomScrollView.scrollView?.userInteractionEnabled = false 
    CustomScrollView.disabledTouches = true 
} 

/// Enable 
class func enable() { 
    CustomScrollView.scrollView?.userInteractionEnabled = true 
    CustomScrollView.disabledTouches = false 
    } 
} 

// MARK: - Delegates 
extension CustomScrollView: UIScrollViewDelegate { 

func scrollViewDidScroll(scrollView: UIScrollView) { 

    if scrollDirection == .Horizontal { 
     moveableNode.position.x = scrollView.contentOffset.x 
    } else { 
     moveableNode.position.y = scrollView.contentOffset.y 
    } 
    } 
} 

Antwort

0

Wenn Sie CustomScrollView verwenden, um ein Spiel zu machen, das eine schlechte Idee ist, ich habe es in Vergangenheit verwendet, um ein Menü zu machen, aber Sie müssen wissen, dass mit UIKit ScrollView-Bibliothek zusammen mit SKNode() verlangsamen Sie viel Ihre Szene, Sie verloren viele fps. Statt CustomScrollView Bibliothek müssen Sie SKCameraNode()

Verfahren verwendet, um die currentPage in einer horizontalen Scrollen abrufen kann dies sein:

extension UIScrollView { 
    var currentPage: Int { 
     return abs(Int(round((contentSize.width - self.contentOffset.x)/self.bounds.size.width))-1) 
    } 
} 

über SKCameraNode() dies ist ein Beispiel, wie mit ihm zu arbeiten:

var boardCamera = SKCameraNode() 

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    for touch in touches { 
     let location = touch.locationInNode(self) 
     let previousLocation = touch.previousLocationInNode(self) 
     let deltaY = location.y - previousLocation.y 
     boardCamera.position.y += deltaY 
    } 
} 
+0

Sorry, ich habe vergessen zu erwähnen, dass die Scrollview, die ich benutze, eine vertikale ist und auch nicht UIkit ist, was alle anderen Leute verwenden, um Menüs für ihre Spiele in Spritekit zu erstellen? Entschuldigung, ich bin ein Noob zu diesem ganzen Menü, das für Spritekit Spiele schafft. Auch wo stelle ich diese Erweiterung? – Astrum