好的,我正在尝试向SKSpriteNode
添加UIScrollView
,我该怎么做?
答案 0 :(得分:2)
到目前为止你尝试了什么? 作为一般的堆栈溢出规则,如果您需要帮助,应始终发布代码。
我建议总是从我的gitHub项目中获取此代码的最新版本,因为我做了更改,因为这个答案,链接在底部。
第1步:创建一个新的swift文件并粘贴此代码
import SpriteKit
/// Scroll direction
enum ScrollDirection {
case vertical // cases start with small letters as I am following Swift 3 guildlines.
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 let currentScene: SKScene
/// Moveable node
private let moveableNode: SKNode
/// Scroll direction
private let scrollDirection: ScrollDirection
/// Touched nodes
private var nodesTouched = [AnyObject]()
// MARK: - Init
init(frame: CGRect, scene: SKScene, moveableNode: SKNode) {
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?) {
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?) {
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?) {
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?) {
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
}
}
}
这将成为UIScrollView的子类并设置它的基本属性。它有自己的触摸方法,可以传递到相关的场景。
步骤2:在您想要使用的相关场景中,您可以创建滚动视图和可移动节点属性,如此
weak var scrollView: CustomScrollView!
let moveableNode = SKNode()
并将它们添加到didMoveToView
中的场景中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.contentSize = CGSizeMake(self.frame.size.width, self.frame.size.height * 2)
view?.addSubview(scrollView)
addChild(moveableNode)
您在第1行中执行的操作是使用场景维度初始化滚动视图助手。您还传递场景以供参考,以及您在步骤2中创建的moveableNode。 第2行是设置scrollView的内容大小的地方,在这种情况下,它是屏幕高度的两倍。
步骤3: - 添加标签或节点等并定位它们。
label1.position.y = CGRectGetMidY(self.frame) - self.frame.size.height
moveableNode.addChild(label1)
在此示例中,标签将位于scrollView的第2页上。这是您必须使用标签和定位的地方。
我建议如果滚动视图中有很多页面,并且有很多标签要执行以下操作。在滚动视图中为每个页面创建一个SKSpriteNode,并使每个页面都与屏幕大小相同。将它们称为page1Node,page2Node等。您可以将第二页上所需的所有标签添加到page2Node。这里的好处是你基本上可以像往常一样在page2Node中定位你所有的东西,而不仅仅是在scrollView中定位page2Node。
你也很幸运,因为垂直使用scrollView(你说你想要的)你不需要做任何翻转和反向定位。
我制作了一些类func,所以如果你需要禁用你的scrollView,你可以在scrollView上覆盖另一个菜单。
CustomScrollView.enable()
CustomScrollView.disable()
最后不要忘记在转换到新场景之前从场景中删除滚动视图。在spritekit中处理UIKit时的痛苦之一。
scrollView?.removeFromSuperView()
答案 1 :(得分:2)
你遇到了一段时间我遇到的问题。根本问题是您无法将SKNode添加到UIKit视图,也无法将UIKit视图添加到SKNodes。视图不是节点,节点不是视图,遗憾的是这不起作用。虽然您可以将UIKit元素添加到SKScene的视图属性中,但这是您可以接受的。在我终于得到它之前,我挣扎了很长一段时间。
我仍然混合使用UIKit和SpriteKit,顺便说一下,但我是以有限的方式这样做的,它从不依赖于向视图添加节点,反之亦然。
您最好的选择是创建一个类似于某种类型的SKNode来保存所有SKSpriteNodes,然后在touchesMoved:函数中调整其在屏幕上的位置。 (基本上创建自己的基于SpriteKit的滚动视图)。
答案 2 :(得分:1)
您可以使用SKCameraNode
和比视口大的场景。这基本上是滚动视图的SpriteKit版本。