UIButton:在swift

时间:2018-02-11 05:28:00

标签: ios swift uibutton avfoundation

当我点击UIButton:“StartButton”时,我正试图播放声音。但是,我收到的错误是:

  

参数类型'CGPoint'不符合预期类型'UIFocusEnvironment'

我创建了如下按钮并为其分配了一个CGRect poisiton,我试图使用touchesBeagn函数来检测触摸的位置并确定它是否与Button的位置匹配,如果是,则调用playButtonSound()功能。

这是我在MainMenu.swift文件中的代码:

//  MainMenuScene.swift

import Foundation
import SpriteKit
import AVFoundation


let startButton:UIButton = UIButton(type: UIButtonType.roundedRect)


    class MainMenuScene: SKScene

        {

        var buttonSound = AVAudioPlayer()

        override func didMove(to view: SKView) {

            let background = SKSpriteNode(imageNamed: "background")
            background.size = self.size
            background.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
            background.zPosition = 0
            self.addChild(background)

            let menuLabel1 = SKLabelNode(fontNamed: "The Bold Font")
            menuLabel1.text = "Hit"
            menuLabel1.position = CGPoint(x: self.size.width * 0.45, y: self.size.height * 0.78)
            menuLabel1.fontSize = 300
            menuLabel1.fontColor = UIColor.yellow
            menuLabel1.colorBlendFactor = 1
            menuLabel1.zPosition = 1
            self.addChild(menuLabel1)

            let menuLabel2 = SKLabelNode(fontNamed: "The Bold Font")
            menuLabel2.text = "&"
            menuLabel2.position = CGPoint(x: self.size.width * 0.55, y: self.size.height * 0.64)
            menuLabel2.fontSize = 350
            menuLabel2.fontColor = UIColor.red
            menuLabel2.colorBlendFactor = 1
            menuLabel2.zPosition = 1
            self.addChild(menuLabel2)

            let menuLabel3 = SKLabelNode(fontNamed: "The Bold Font")
            menuLabel3.text = "Go"
            menuLabel3.position = CGPoint(x: self.size.width * 0.60, y: self.size.height * 0.52)
            menuLabel3.fontSize = 300
            menuLabel3.fontColor = UIColor.yellow
            menuLabel3.colorBlendFactor = 1
            menuLabel3.zPosition = 1
            self.addChild(menuLabel3)

            startButton.frame = CGRect(x: (self.view?.center.x)! * 0.5  , y: (self.view?.center.y)! * 1.5 , width: 200, height: 50)
            startButton.backgroundColor = .red
            startButton.setTitleColor(.white, for: .normal)
            startButton.titleLabel?.adjustsFontSizeToFitWidth = true
            startButton.layer.cornerRadius = 10
            startButton.clipsToBounds = true
            startButton.setTitle("Start Game", for: .normal)
            startButton.titleLabel!.font =  UIFont(name: "The Bold Font", size: 60)
            startButton.addTarget(self, action:#selector(self.startButtonClicked), for: .touchUpInside)
            self.view?.addSubview(startButton)

        }

        @objc func startButtonClicked() {
            let sceneToMoveTo = GameScene(size: self.size)
            sceneToMoveTo.scaleMode = self.scaleMode
            let myTransition = SKTransition.reveal(with: .right , duration: 0.5)
            self.view!.presentScene(sceneToMoveTo, transition: myTransition)
        }



        @objc func playButtonSound(filename: String) {
            let url = Bundle.main.url(forResource: "button.mp3" , withExtension: nil)
            guard let newURL = url else {
                print("Could not find file: \(filename)")
                return
            }
            do {
                buttonSound = try AVAudioPlayer(contentsOf: newURL)
                buttonSound.numberOfLoops = -1
                buttonSound.prepareToPlay()
                buttonSound.setVolume(0.50, fadeDuration: 0.1)
                buttonSound.play()
            } catch let error as NSError {
                print(error.description)
            }
        }

        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            for touch: AnyObject in touches {
                var pointOfTouch = touch.location(in: self)

                if startButton.contains(pointOfTouch)
                {
                        playButtonSound(filename: "button.mp3")
                }



            }
        }

    }

错误的原因是什么?

1 个答案:

答案 0 :(得分:3)

问题是你正在调用错误的contains方法。

您正在UIButton上调用具有此签名的方法:

public func contains(_ environment: UIFocusEnvironment) -> Bool

您可能要调用的是按钮框架上的方法:

if startButton.frame.contains(pointOfTouch)

您可能需要阅读文档以确保使用框架是您想要的。

相关问题