Swift 2.0,SpriteKit-Scrollview不使用页面. [英] Swift 2.0, SpriteKit - Scrollview to not use pages.

查看:47
本文介绍了Swift 2.0,SpriteKit-Scrollview不使用页面.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我有一个scrollView,它已被子类化,可以应用于我在这里问过的上一个问题中的任何场景:

Ok so I have a scrollView which has been subclassed to be able to be applied to any scene which is from a previous question I asked here:

SpriteKit,Swift 2.0-ScrollView反向

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一样,其中所有精灵都是唯一移动的物体,因此我不必在多个页面上滚动以获取一个精灵.

The only problem is that the scrollview uses pages, while, the way I want it to look, scrolls only through the sprites, like the raywenderlich one where all the sprites are the only things moving and so I don't have to scroll across multiple pages to get to a sprite.

可以在这里找到该项目:

the project can be found here:

Raywenderlich项目

因为他们使用了他们的gameViewController,所以我很难像上面一样通过子类scrollview来实现它.

Because they use their gameViewController I am having trouble figuring out how to implement it through a subclass scrollview like I have above.

推荐答案

我不明白您在这里问的是什么.我刚刚检查了RayWenderlich教程,它与我的GitHub示例项目完全相同.它们只是将精灵保持在一起,就像在我的项目中那样,出于演示目的,我将每个精灵放在新的页面上.

I dont understand what you are asking here. I just checked the RayWenderlich tutorial and its exactly the same as my GitHub sample project. They just keep the sprites closer together where as in my project for demonstration purposes I put each sprite on a new page.

如果您只想让精灵滚动,只需将精灵添加到moveableNode,然后将其余的和平常一样直接添加到场景即可.

If you just want sprites to scroll that just add the sprites to the moveableNode and the rest as usual to the scene directly.

addChild(background)
moveableNode.addChild(sprite1)

比改变精灵的位置要近得多.您基本上是将第一个精灵添加到scrollView的第一个页面中,然后根据先前的精灵x位置来放置其他精灵.您也将这些精灵添加到sprite1.

Than change the sprite positions so they are closer together. You basically add the 1st sprite to the 1st page in the scrollView and than position the other sprites based on the previous sprites x position. You add these sprites to sprite1 as well.

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

I updated my gitHub project to show this in action https://github.com/crashoverride777/Swift2-SpriteKit-UIScrollView-Helper

这篇关于Swift 2.0,SpriteKit-Scrollview不使用页面.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆