swift - ScrollView 页面定位已关闭

标签 swift uiscrollview sprite-kit

我目前有一个垂直 ScrollView 正在工作,并且是从 UIscrollview 类定义的:

    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)

ScrollView 完美显示,但我一直无法让页面显示在 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)

我已尝试更改页面的位置,但仍然无法显示它们,有人可以告诉我我做错了什么吗?我仍然不完全理解 CGPoint 是如何工作的。

这也是我的 ScrollView 类:

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
     }
   }
 }

最佳答案

如果你使用CustomScrollView来制作游戏那是个坏主意,我过去用它来制作菜单,但你必须知道使用UIKitscrollView库与 SKNode() 一起减慢了你的场景,你丢失了很多 fps。您必须使用 SKCameraNode()

而不是 CustomScrollView

在水平滚动中检索currentPage的方法可以是这样的:

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

关于SKCameraNode()这是如何使用它的示例:

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
    }
}

关于swift - ScrollView 页面定位已关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38388862/

相关文章:

ios - ViewController 中快速的多维数组

swift - 'requestReview()' 在 iOS 14.0 中被弃用

ios - 使用 UIPanGestureRecognizer 从 CGRect 获取 UIView - Swift 2.0

sprite-kit - 在 Sprite 套件中永远播放视频

objective-c - SpriteKit 作为 SceneKit 的叠加层 SKShapeNode 崩溃

ios - 使用从另一个表查询的用户 objectId 查询用户表

iphone - iOS UIScrollView : Cannot scroll until the end of the content

ios - 什么是 UIScrollView 的 "content view"?

iphone - 可接受的滚动 FPS 是多少?提高性能的技巧是什么?

ios - 是否必须使用 Assets.xcassets?