gpt4 book ai didi

ios - 在 SpriteKit 中模拟 UIScrollView : scrolling SKScene works when SKView is 2000 points tall but fails when height is 2050

转载 作者:行者123 更新时间:2023-11-28 23:50:12 25 4
gpt4 key购买 nike

目标是滚动 SKScene,其行为与 UIScrollView 相同。

其他问题类似,但答案没有帮助。

此代码说明了当 SKView subview 高 2000 点时 UIScrollView + SKView 如何显示内容,但如果 subview 高 2050 点则什么也不显示。

高度很重要,因为例如,如果 SKScene 包含 3000 点高的节点图,我们需要在 UIScrollView 中渲染场景(通过在 SKView 内呈现场景,SKView 本身就是 UIScrollView 的 subview ) .

class TestViewController: UIViewController, UIScrollViewDelegate {

@IBOutlet weak var scrollView: UIScrollView!

override func viewDidLoad() {
// Call super
super.viewDidLoad()

// Make scene (then someone call the police)
let scene = TestScene(size: UIScreen.main.bounds.size)

// Allow other overlays to get presented
definesPresentationContext = true

// Set background color
scrollView.backgroundColor = UIColor.clear

// Create content view for scrolling since SKViews vanish with height > 2050
let contentHeight = scene.getScrollHeight()
let contentFrame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: contentHeight)
let contentView = UIView(frame: contentFrame)
contentView.backgroundColor = UIColor.clear

// Create SKView
let skFrame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: contentHeight)
let skView = SKView(frame: skFrame)

// Configure <scrollView>
scrollView.insertSubview(skView, at: 0)
scrollView.addSubview(contentView)
scrollView.delegate = self
scrollView.contentSize = contentFrame.size

// Present scene
skView.presentScene(scene)
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
scene.scrollBy(offset: scrollView.contentOffset.y)
}
}

最佳答案

这两个类演示了如何使用 UIScrollView 行为滚动 SKScene 菜单或场景。

感谢@KnightofDragon 发现了代码中的关键缺陷。

令人沮丧的是,Apple 本身并未提供此功能,但希望其他人不必浪费时间尝试在 SpriteKit 中实现菜单滚动的最佳方法!

UIViewController 假定 UIScrollView 已通过 Storyboard 添加。

class ScrollViewController: UIViewController, UIScrollViewDelegate {
// IB Outlets
@IBOutlet weak var scrollView: UIScrollView!

// General Vars
var scene = ScrollScene()

// =======================================================================================================
// MARK: Public Functions
// =======================================================================================================
override func viewDidLoad() {
// Call super
super.viewDidLoad()

// Create scene
scene = ScrollScene()

// Allow other overlays to get presented
definesPresentationContext = true

// Create content view for scrolling since SKViews vanish with height > ~2048
let contentHeight = scene.getScrollHeight()
let contentFrame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: contentHeight)
let contentView = UIView(frame: contentFrame)
contentView.backgroundColor = UIColor.clear

// Create SKView with same frame as <scrollView>, must manually compute because <scrollView> frame not ready at this point
let scrollViewPosY = CGFloat(0)
let scrollViewHeight = UIScreen.main.bounds.size.height - scrollViewPosY
let scrollViewFrame = CGRect(x: 0, y: scrollViewPosY, width: UIScreen.main.bounds.size.width, height: scrollViewHeight)
let skView = SKView(frame: scrollViewFrame)
view.insertSubview(skView, at: 0)

// Configure <scrollView>
scrollView.addSubview(contentView)
scrollView.delegate = self
scrollView.contentSize = contentFrame.size

// Present scene
skView.presentScene(scene)

// Handle taps on <scrollView>
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(scrollViewDidTap))
scrollView.addGestureRecognizer(tapGesture)
}

// =======================================================================================================
// MARK: UIScrollViewDelegate Functions
// =======================================================================================================
func scrollViewDidScroll(_ scrollView: UIScrollView) {
scene.scrollBy(contentOffset: scrollView.contentOffset.y)
}


// =======================================================================================================
// MARK: Gesture Functions
// =======================================================================================================
@objc func scrollViewDidTap(_ sender: UITapGestureRecognizer) {
let scrollViewPoint = sender.location(in: sender.view!)
scene.viewDidTapPoint(viewPoint: scrollViewPoint, contentOffset: scrollView.contentOffset.y)
}
}



class ScrollScene : SKScene {
// Layer Vars
let scrollLayer = SKNode()

// General Vars
var originalPosY = CGFloat(0)


// ================================================================================================
// MARK: Initializers
// ================================================================================================
override init() {
super.init()
}


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


// ================================================================================================
// MARK: Public Functions
// ================================================================================================
func scrollBy(contentOffset: CGFloat) {
scrollLayer.position.y = originalPosY + contentOffset
}


func viewDidTapPoint(viewPoint: CGPoint, contentOffset: CGFloat) {
let nodes = getNodesTouchedFromView(point: viewPoint, contentOffset: contentOffset)
}


func getScrollHeight() -> CGFloat {
return scrollLayer.calculateAccumulatedFrame().height
}


fileprivate func getNodesTouchedFromView(point: CGPoint, contentOffset: CGFloat) -> [SKNode] {
var scenePoint = convertPoint(fromView: point)
scenePoint.y += contentOffset
return scrollLayer.nodes(at: scenePoint)
}
}

关于ios - 在 SpriteKit 中模拟 UIScrollView : scrolling SKScene works when SKView is 2000 points tall but fails when height is 2050,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52087650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com