[英]Xcode 9 Swift 4 Playgrounds UIGestureRecognizer not working
最近,我开始处理WWDC 2018提交的内容。 预计我会在一个快速的Playgrounds文件中启动我的项目(Apple通常不接受应用程序提交)。 大约两个月前,我将Xcode更新到9.2版(9C40b)。 完成此操作后,我意识到我的手势识别器无法用于实时取景。 一切都被渲染并完美显示在实时取景助手窗口中,但是我的手势识别器根本无法响应。 有谁遇到过类似问题的任何问题,并且有人能够在Xcode 9中将Swift 4与Swift操场一起使用吗(我在下面附上了我的代码以供参考)?
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
class HomeViewController : UIViewController, UIGestureRecognizerDelegate {
let cardViewOne = UIView()
let cardButtonOne = UIButton()
let cardViewTwo = UIView()
let cardButtonTwo = UIButton()
let cardViewThree = UIView()
let cardButtonThree = UIButton()
let cardViewControllerTitleLabel = UILabel()
override func loadView() {
let view = UIView()
view.backgroundColor = .white
//Card One
cardViewOne.frame = CGRect(x: 0, y: 218, width: 375, height: 480)
cardViewOne.backgroundColor = #colorLiteral(red: 1, green: 0.1058823529, blue: 0.3607843137, alpha: 1)
cardButtonOne.frame = CGRect(x: 0, y: 0, width: 375, height: 47)
cardButtonOne.backgroundColor = UIColor(red:0.90, green:0.10, blue:0.31, alpha:0.0)
cardButtonOne.setTitle("About", for: .normal)
cardButtonOne.titleLabel?.font = UIFont(name: "Avenir-Black", size: 18)
cardViewOne.layer.shadowColor = UIColor.black.cgColor
cardViewOne.layer.shadowOpacity = 0.25
cardViewOne.layer.shadowOffset = CGSize.zero
cardViewOne.layer.shadowRadius = 40
cardViewOne.layer.shadowPath = UIBezierPath(rect: cardViewOne.bounds).cgPath
//Card Two
cardViewTwo.frame = CGRect(x: 0, y: 164, width: 375, height: 480)
cardViewTwo.backgroundColor = #colorLiteral(red: 0.8965349741, green: 0.0931719053, blue: 0.3287236417, alpha: 1)
cardButtonTwo.frame = CGRect(x: 0, y: 0, width: 375, height: 47)
cardButtonTwo.backgroundColor = UIColor(red:0.90, green:0.10, blue:0.31, alpha:0.0)
cardButtonTwo.setTitle("Mix", for: .normal)
cardButtonTwo.titleLabel?.font = UIFont(name: "Avenir-Black", size: 18)
cardViewTwo.layer.shadowColor = UIColor.black.cgColor
cardViewTwo.layer.shadowOpacity = 0.25
cardViewTwo.layer.shadowOffset = CGSize.zero
cardViewTwo.layer.shadowRadius = 40
cardViewTwo.layer.shadowPath = UIBezierPath(rect: cardViewTwo.bounds).cgPath
//Card Three
cardViewThree.frame = CGRect(x: 0, y: 109, width: 375, height: 480)
cardViewThree.backgroundColor = #colorLiteral(red: 0.8446810233, green: 0.08778301191, blue: 0.3097108647, alpha: 1)
cardButtonThree.frame = CGRect(x: 0, y: 0, width: 375, height: 47)
cardButtonThree.backgroundColor = UIColor(red:0.90, green:0.10, blue:0.31, alpha:0.0)
cardButtonThree.setTitle("Experiment", for: .normal)
cardButtonThree.titleLabel?.font = UIFont(name: "Avenir-Black", size: 18)
cardViewThree.layer.shadowColor = UIColor.black.cgColor
cardViewThree.layer.shadowOpacity = 0.25
cardViewThree.layer.shadowOffset = CGSize.zero
cardViewThree.layer.shadowRadius = 40
cardViewThree.layer.shadowPath = UIBezierPath(rect: cardViewThree.bounds).cgPath
cardViewControllerTitleLabel.text = "Vibe"
cardViewControllerTitleLabel.frame = CGRect(x: 158, y: 20, width: 81, height: 34)
cardViewControllerTitleLabel.font = UIFont(name: "Avenir-Black", size: 32)
cardViewControllerTitleLabel.textColor = #colorLiteral(red: 1, green: 0.1215686275, blue: 0.3529411765, alpha: 1)
//ViewController Title
cardViewOne.layer.zPosition = 3
cardViewTwo.layer.zPosition = 2
//z-index adjustment
view.addSubview(cardViewOne)
view.addSubview(cardViewTwo)
view.addSubview(cardViewThree)
cardViewOne.addSubview(cardButtonOne)
cardViewTwo.addSubview(cardButtonTwo)
cardViewThree.addSubview(cardButtonThree)
view.addSubview(cardViewControllerTitleLabel)
self.view = view
}
override func viewDidLoad() {
let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap))
tap.delegate = self
cardButtonTwo.addGestureRecognizer(tap)
print("Gesture recognizer added")
self.navigationController?.isNavigationBarHidden = true
}
@objc func handleTap(sender: UITapGestureRecognizer? = nil) {
let detail = TestViewController()
navigationController?.pushViewController(detail, animated: true)
}
}
class TestViewController: UIViewController {
}
let root = HomeViewController()
let nav = UINavigationController(rootViewController: root)
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = nav
您为名片视图使用了不正确的高度值。
将高度值更改为较小的47个值有效:
cardViewOne.frame = CGRect(x: 0, y: 218, width: 375, height: 47)
cardViewTwo.frame = CGRect(x: 0, y: 164, width: 375, height: 47)
cardViewThree.frame = CGRect(x: 0, y: 109, width: 375, height: 47)
另外,由于cardButtonTwo
是UIButton
,因此不需要使用UITapGestureRecognizer
。 您可以简单地为touchUpInside
事件添加目标。
override func viewDidLoad() {
cardButtonTwo.addTarget(self, action: #selector(HomeViewController.handleTap(sender:)), for: .touchUpInside)
self.navigationController?.isNavigationBarHidden = true
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.