ios - UIScrollViewDelegate 函数未调用

标签 ios swift sprite-kit uiscrollviewdelegate skview

当我拖动 View 时,它似乎会滚动(侧栏出现并四处移动),但我的委托(delegate)功能不会触发。 问题:为什么协议(protocol)函数中的 println 没有被调用?作为背景,我正在尝试重写 https://github.com/bobmoff/ScrollKit在 Swift 中,这就是 UIView 与 SKView 等混合的原因

我的日志输出是:

852.0
480.0
Finished 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
    }
}

最佳答案

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

例如:

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

应该是:

func scrollViewDidScroll(scrollView: UIScrollView) {
    println("entering scrollViewDidScroll function")
    adjustContent()
}

关于ios - UIScrollViewDelegate 函数未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27667105/

相关文章:

ios - 具有 CurrentValueSubject 绑定(bind)的 TextField 上的 "Binding<String> action tried to update multiple times per frame"

ios - 崩溃 : com. apple.spritekit.renderQueue

swift - 如何在 SwiftUI( hive 网格)中使用自定义形状创建六边形镶嵌

swift 3 一个函数没有被调用

ios - 在 SpriteKit 中已经运行 Action 时停止 Action

iphone - 我可以将自己的音频插入 iPhone 的通话中吗?

iOS cordova 插件 : How to send plugin result from ios plugin to javascript in background thread

ios - 在同一 viewController 中嵌入的不同 Collection View 的两个单元格之间发送数据

ios - 使用 Sprite Kit 创建自定义 slider - 如何传递 @selector?

swift - 相机不会跟随 Sprite