【问题标题】:Swift 2.0, SpriteKit - Scrollview to not use pages.Swift 2.0,SpriteKit - 滚动视图不使用页面。
【发布时间】:2016-01-19 20:53:03
【问题描述】:

好的,所以我有一个滚动视图,它已经被子类化,可以应用于我在这里提出的上一个问题中的任何场景:

SpriteKit, Swift 2.0 - ScrollView in reverse

import Foundation
import SpriteKit

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

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

class CustomScrollView: UIScrollView {

// MARK: - Static Properties

/// Touches allowed
static var disabledTouches = false

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

// MARK: - Properties

/// Current scene
private weak var currentScene: SKScene?

/// Moveable node
private var moveableNode: SKNode?

/// Scroll direction
private var scrollDirection = ScrollDirection.None

// MARK: - Init
init(frame: CGRect, scene: SKScene, moveableNode: SKNode, scrollDirection: ScrollDirection) {
    print("Scroll View init")
    super.init(frame: frame)

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

    // flip for spritekit (only needed for horizontal)
    if self.scrollDirection == .Horizontal {
        let flip = CGAffineTransformMakeScale(-1,-1)
        self.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?) {
    print("Touch began scroll view")

    guard !CustomScrollView.disabledTouches else { return }
    currentScene?.touchesBegan(touches, withEvent: event)
}

/// moved
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    print("Touch moved scroll view")

    guard !CustomScrollView.disabledTouches else { return }
    currentScene?.touchesMoved(touches, withEvent: event)
}

/// ended
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    print("Touch ended scroll view")

    guard !CustomScrollView.disabledTouches else { return }
    currentScene?.touchesEnded(touches, withEvent: event)
}

/// cancelled
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
    print("Touch cancelled scroll view")

    guard !CustomScrollView.disabledTouches else { return }
    currentScene?.touchesCancelled(touches, withEvent: event)
   }
}

 // MARK: - Touch Controls
 extension CustomScrollView {

/// Disable
class func disable() {
    print("Disabled scroll view")
    CustomScrollView.scrollView?.userInteractionEnabled = false
    CustomScrollView.disabledTouches = true
}

/// Enable
class func enable() {
    print("Enabled scroll view")
    CustomScrollView.scrollView?.userInteractionEnabled = true
    CustomScrollView.disabledTouches = false
   }
 }

 // MARK: - Delegates
 extension CustomScrollView: UIScrollViewDelegate {

/// did scroll
func scrollViewDidScroll(scrollView: UIScrollView) {
    print("Scroll view did scroll")

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

唯一的问题是滚动视图使用页面,而我希望它看起来的方式只滚动精灵,就像 raywenderlich 一样,其中所有精灵都是唯一移动的东西,所以我不必滚动多个页面以找到一个精灵。

项目可以在这里找到:

Raywenderlich Project

因为他们使用他们的 gameViewController,所以我无法像上面那样通过子类滚动视图来实现它。

【问题讨论】:

    标签: ios swift uiscrollview sprite-kit


    【解决方案1】:

    我不明白你在这里问什么。我刚刚查看了 RayWenderlich 教程,它与我的 GitHub 示例项目完全相同。他们只是让精灵更靠近在一起,在我的项目中为了演示目的,我将每个精灵放在一个新页面上。

    如果您只想让精灵滚动,只需将精灵添加到可移动节点,其余的照常直接添加到场景中。

    addChild(background)
    moveableNode.addChild(sprite1)
    

    改变精灵的位置,使它们更靠近。您基本上将第一个精灵添加到滚动视图中的第一页,然后根据先前的精灵 x 位置定位其他精灵。您也可以将这些精灵添加到 sprite1。

    let sprite1 = SKSpriteNode(color: SKColor.redColor(), size: CGSize(width: 50, height: 50))
    sprite1.position = CGPointMake(0, 0)
    page1ScrollView.addChild(sprite1)
    
    let sprite2 = SKSpriteNode(color: SKColor.redColor(), size: CGSize(width: 50, height: 50))
    sprite2.position = CGPointMake(sprite1.position.x + (sprite2.size.width * 1.5), sprite1.position.y)
    sprite1.addChild(sprite2)
    
    let sprite3 = SKSpriteNode(color: SKColor.redColor(), size: CGSize(width: 50, height: 50))
    sprite3.position = CGPointMake(sprite2.position.x + (sprite3.size.width * 1.5), sprite1.position.y)
    sprite1.addChild(sprite3)
    

    我更新了我的 gitHub 项目以实际展示这一点 https://github.com/crashoverride777/Swift2-SpriteKit-UIScrollView-Helper

    【讨论】:

    • 我刚刚尝试了你上面所说的,但是 sprite1 之后的精灵没有排列它们只是屏幕底部的一半。我也根本看不到第三个。
    • 我刚刚更新了我的答案,您需要将精灵 2,3 等添加到精灵 1“sprite1.addChild(sprite2)...”。只需再次复制上面的更新代码,现在就可以看到它的工作了
    • 无论如何要在他们完成滚动后锁定到一个精灵而不是一半在一个精灵上?就像 ray 项目一样。 (我应该再问一个问题吗?)如果可以,我明天再做。
    • 是的,最好提出一个新问题。我也很乐意为新的提供帮助。
    • 我也更新了我对你其他问题的回答stackoverflow.com/questions/34801518/…
    猜你喜欢
    • 1970-01-01
    • 2017-06-05
    • 2017-03-22
    • 2016-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多