未调用UIScrollViewDelegate函数

时间:2014-12-27 12:11:04

标签: ios swift sprite-kit uiscrollviewdelegate skview

当我拖动它时,我的视图似乎滚动(侧边栏出现并移动),但我的委托功能不会触发。 问题:为什么不打算调用协议函数中的println?作为背景,我试图在Swift中重写https://github.com/bobmoff/ScrollKit为什么UIView与SKView等混合在一起

我的日志输出是:

  

852.0
480.0
完成viewDidLoad

代码是:

import UIKit
import SpriteKit
var kViewTransformChanged = "view transform changed"
var scene = GameScene()
var skView = SKView()
var clearContentView = UIView()

extension SKNode {
    class func unarchiveFromFile(file : NSString) -> SKNode? {
        if let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") {
            var sceneData = NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe, error: nil)!
            var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)

            archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
            let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as GameScene
            archiver.finishDecoding()
            return scene
        } else {
            return nil
        }
    }
}

class GameViewController: UIViewController, UIScrollViewDelegate {

    override func viewDidLoad() {

        super.viewDidLoad()

        // Configure the view.
        skView = self.view as SKView
        skView.showsFPS = true
        skView.showsNodeCount = true

        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true

        /* Set the scale mode to scale to fit the window */
        scene.scaleMode = .AspectFill

        skView.presentScene(scene)

        var contentSize = skView.bounds.size
        contentSize.height *= 1.5
        contentSize.width *= 1.5
        scene.size = contentSize

        println(contentSize.height)
        println(contentSize.width)

        var scrollView = UIScrollView()
        scrollView.frame = skView.frame
        scrollView.contentSize = contentSize
        scrollView.delegate = self
        scrollView.maximumZoomScale = 3
        scrollView.minimumZoomScale = 1
        scrollView.indicatorStyle = UIScrollViewIndicatorStyle.White


        clearContentView.frame = CGRect(origin: CGPointZero, size: contentSize)

        clearContentView.backgroundColor = UIColor.clearColor()

        scrollView.addSubview(clearContentView)
        clearContentView.addObserver(self, forKeyPath: "transform", options: NSKeyValueObservingOptions(), context: &kViewTransformChanged)

        skView.addSubview(scrollView)

        //
        println("Finished viewDidLoad")
    }


    func viewForZoomingInScrollView() -> UIView? {
        println("entering viewForZoomingInScrollView function")
        return self.view
    }

    func scrollViewDidScroll() {
        println("entering scrollViewDidScroll function")
        adjustContent()
    }

    // NOT SURE ABOUT THIS FUNCTION
    func adjustContent() {
        println("entering adjustContent function")
        NSLog("?")
        var scrollV = self.view as UIScrollView
        scene.setScale(scrollV.zoomScale)
        scene.position = scrollV.contentOffset
    }

    func scrollViewDidZoom() {
        println("entering scrollViewDidZoom function")
        adjustContent()
    }

    func scrollViewDidTransform() {
        println("entering scrollViewDidTransform function")
        adjustContent()
    }

    func scrollViewDidEndZooming() {
        println("entering scrollViewDidEndZooming function")
        adjustContent()
    }

    override func shouldAutorotate() -> Bool {
        return true
    }

    override func supportedInterfaceOrientations() -> Int {
        if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
            return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
        } else {
            return Int(UIInterfaceOrientationMask.All.rawValue)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Release any cached data, images, etc that aren't in use.
    }

    override func prefersStatusBarHidden() -> Bool {
        return true
    }
}

1 个答案:

答案 0 :(得分:2)

您的UIScrollViewDelegate函数实现与the documentation中的声明不匹配。具体来说,您错过了UIScrollView参数。

例如,这个:

func scrollViewDidScroll() {
    println("entering scrollViewDidScroll function")
    adjustContent()
}

应该是:

func scrollViewDidScroll(scrollView: UIScrollView) {
    println("entering scrollViewDidScroll function")
    adjustContent()
}
相关问题