gpt4 book ai didi

ios - ScrollView 停在某个点

转载 作者:行者123 更新时间:2023-11-30 13:11:01 25 4
gpt4 key购买 nike

嗨,我目前有一个水平UIScrollView,它允许我在游戏中选择一个角色,然后选择它,但我现在遇到的问题是我试图让 ScrollView 停止在 Sprite /角色位于屏幕中间的点,而不是让它停在任何地方。

我的UIScrollView有一个“可移动节点”,其中包含多个页面,其中包含所有 Sprite ,如下所示:

    scrollViewHorizontal = CustomScrollView(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height), scene: self, moveableNode: moveableNodeHorizontal, scrollDirection: .Horizontal)
scrollViewHorizontal.contentSize = CGSizeMake(self.frame.size.width * 4, self.frame.size.height) // * 4 makes it three times as wide as screen
view?.addSubview(scrollViewHorizontal)

scrollViewHorizontal.hidden = true
addChild(moveableNodeHorizontal)
moveableNodeHorizontal.hidden = true
moveableNodeHorizontal.zPosition = 100000

scrollViewHorizontal.setContentOffset(CGPoint(x: 0 + self.frame.size.width + self.frame.size.width * 2, y: 0), animated: false)

let page1ScrollView = SKSpriteNode(color: SKColor.clearColor(), size: CGSizeMake(scrollViewHorizontal.frame.size.width, scrollViewHorizontal.frame.size.height))
page1ScrollView.zPosition = -1
page1ScrollView.position = CGPointMake(CGRectGetMidX(self.frame) - (self.frame.size.width * 3.5), CGRectGetMidY(self.frame) - (self.frame.height / 2))
moveableNodeHorizontal.addChild(page1ScrollView)

let page2ScrollView = SKSpriteNode(color: SKColor.clearColor(), size: CGSizeMake(scrollViewHorizontal.frame.size.width, scrollViewHorizontal.frame.size.height))
page2ScrollView.zPosition = -1
page2ScrollView.position = CGPointMake(CGRectGetMidX(self.frame) - (self.frame.size.width * 2.5), CGRectGetMidY(self.frame) - (self.frame.height / 2))
moveableNodeHorizontal.addChild(page2ScrollView)

Characters.append(generateCharacters(CGPointMake(0, 0), page:(page1ScrollView), tex: "YellowFrog"))
Characters.append(generateCharacters(CGPointMake(100, 0), page:(page1ScrollView), tex: "Frog"))

Characters.append(generateCharacters(CGPointMake(0, 0), page:(page2ScrollView), tex: "ball"))
Characters.append(generateCharacters(CGPointMake(100, 0), page:(page2ScrollView), tex: "ball"))

在提出问题之前我之前搜索过这个,建议我使用这个:

  func scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{

但我不知道如何将其实现到我的代码中,因为我的 ScrollView 包含在另一个类中,可以在此处看到:

/// Nodes touched
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
}
}
}

编辑:

extension CustomScrollView: UIScrollViewDelegate {

func scrollViewDidScroll(scrollView: UIScrollView) {

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

最佳答案

CustomScrollView 是一个项目,展示了如何在 sprite-kit 中使用 UIKit,但这会在渲染过程中导致大量工作,从而减慢您的速度游戏(由于 fps 较低),因此建议仅将其用于游戏菜单,而不用于游戏,这是明智且合理的。

通往 iOS8.x 的方法:

例如,您可以构建许多 SKSpriteNode 背景,这些背景相互跟随,并在您的 update 方法中执行以下操作:

override func update(currentTime: CFTimeInterval) {
self.enumerateChildNodesWithName("background") { node, _ in
if node is SKSpriteNode {
if let p = (node as! SKShapeNode).position {
// adjust your background positions to make the scroll
}
}
}
}

通往 iOS9x 的方法:

Apple 在 iOS 9 中向 sprite-kit 添加了 SKCameraNode 类,以便更轻松地滚动和缩放 sprite-kit 场景。

var gameCamera = 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 deltaX = location.x - previousLocation.x
gameCamera.position.x += deltaX
}
}
<小时/>

CustomScrollView 页面选择器

话虽如此,我们遇到了您的情况,假设您将所有页面都放在名为 pages 的数组中,并且您想要停止滚动到 x 页面:

func scrollToPage(page:Int) {
let totalPages : Int = (pages.count - 1)
let reversedPage : Int = totalPages - page
self.scrollView.setContentOffset(CGPointMake(round(self.frame.size.width*CGFloat(reversedPage)), scrollView.contentOffset.y),animated: true)
}

另一个好主意是拦截滚动委托(delegate)方法来更新元素:

func scrollViewDidScroll(scrollView: UIScrollView) {
self.scrollView.scrollViewDidScroll(scrollView)
updateLayout() // call my method to update elements in scroll checking the current page
}

您可以使用以下代码获取页码:

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

关于ios - ScrollView 停在某个点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38734589/

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