我的按钮不起作用,我不知道为什么
My buttons aren’t working and I don’t know why
我正在尝试制作一个真心话大冒险的游戏作为我的第一个项目,我没有遇到任何错误,但按钮就是不起作用我 运行 逐步编写代码,当我按下按钮时它们不工作 truthsEng 和 DaresEng 是 truths and dares 它们在共享源文件夹中。
import PlaygroundSupport
import UIKit
let view = UIView()
//adding the baground image
let Game = UIImageView(frame: UIScreen.main.bounds)
Game.image = UIImage(named:"Photo.png")
Game.contentMode = .scaleAspectFill
view.insertSubview(Game, at:0)
let lbl = UILabel(frame: CGRect(x:100 , y:100, width: 300, height: 500))
lbl.text = "test"
lbl.textColor = #colorLiteral(red: 0.6313725709915161, green: 0.6470588445663452, blue: 0.9058823585510254, alpha: 1.0)
Game.addSubview(lbl)
let truthButton = UIButton(frame: CGRect(x:100, y: 100, width: 100, height: 69))
truthButton.layer.cornerRadius = 10
truthButton.backgroundColor = #colorLiteral(red: 0.6325997710227966, green: 0.6475543975830078, blue: 0.905613362789154, alpha: 1.0)
truthButton.setTitle("truth", for: UIControl.State.normal)
Game.addSubview(truthButton)
let dareButton = UIButton(frame: CGRect(x:300, y: 400, width: 100, height: 69))
dareButton.layer.cornerRadius = 10
dareButton.backgroundColor = #colorLiteral(red: 0.6352941393852234, green: 0.6470588445663452, blue: 0.886274516582489, alpha: 1.0)
dareButton.setTitle("dare", for: UIControl.State.normal)
Game.addSubview(dareButton)
class Responder: NSObject{
@objc func PrintTruthAndChangeBaground() {
lbl.text = truthsEng.randomElement()
}
@objc func PrintDareAndChangeBaground() {
lbl.text = daresEng.randomElement()
}
}
let responser = Responder()
truthButton.addTarget(responser, action: #selector(Responder.PrintTruthAndChangeBaground), for:.touchUpInside)
dareButton.addTarget(responser, action: #selector(Responder.PrintDareAndChangeBaground), for: .touchUpInside)
PlaygroundPage.current.liveView = Game
UIKit UIImageView isUserInteractionEnabled
一个布尔值,用于确定是否忽略用户事件并将其从事件队列中删除。
声明
var isUserInteractionEnabled: Bool { 设置 }
讨论
此 属性 继承自 UIView 父级 class。 class 将 属性 的默认值更改为 false。
您需要将此 属性 设置为 true。
我正在尝试制作一个真心话大冒险的游戏作为我的第一个项目,我没有遇到任何错误,但按钮就是不起作用我 运行 逐步编写代码,当我按下按钮时它们不工作 truthsEng 和 DaresEng 是 truths and dares 它们在共享源文件夹中。
import PlaygroundSupport
import UIKit
let view = UIView()
//adding the baground image
let Game = UIImageView(frame: UIScreen.main.bounds)
Game.image = UIImage(named:"Photo.png")
Game.contentMode = .scaleAspectFill
view.insertSubview(Game, at:0)
let lbl = UILabel(frame: CGRect(x:100 , y:100, width: 300, height: 500))
lbl.text = "test"
lbl.textColor = #colorLiteral(red: 0.6313725709915161, green: 0.6470588445663452, blue: 0.9058823585510254, alpha: 1.0)
Game.addSubview(lbl)
let truthButton = UIButton(frame: CGRect(x:100, y: 100, width: 100, height: 69))
truthButton.layer.cornerRadius = 10
truthButton.backgroundColor = #colorLiteral(red: 0.6325997710227966, green: 0.6475543975830078, blue: 0.905613362789154, alpha: 1.0)
truthButton.setTitle("truth", for: UIControl.State.normal)
Game.addSubview(truthButton)
let dareButton = UIButton(frame: CGRect(x:300, y: 400, width: 100, height: 69))
dareButton.layer.cornerRadius = 10
dareButton.backgroundColor = #colorLiteral(red: 0.6352941393852234, green: 0.6470588445663452, blue: 0.886274516582489, alpha: 1.0)
dareButton.setTitle("dare", for: UIControl.State.normal)
Game.addSubview(dareButton)
class Responder: NSObject{
@objc func PrintTruthAndChangeBaground() {
lbl.text = truthsEng.randomElement()
}
@objc func PrintDareAndChangeBaground() {
lbl.text = daresEng.randomElement()
}
}
let responser = Responder()
truthButton.addTarget(responser, action: #selector(Responder.PrintTruthAndChangeBaground), for:.touchUpInside)
dareButton.addTarget(responser, action: #selector(Responder.PrintDareAndChangeBaground), for: .touchUpInside)
PlaygroundPage.current.liveView = Game
UIKit UIImageView isUserInteractionEnabled
一个布尔值,用于确定是否忽略用户事件并将其从事件队列中删除。
声明 var isUserInteractionEnabled: Bool { 设置 }
讨论 此 属性 继承自 UIView 父级 class。 class 将 属性 的默认值更改为 false。
您需要将此 属性 设置为 true。