Xcode 9 Swift 4 Playgrounds UIGestureRecognizer无法正常工作

时间:2018-01-06 17:35:45

标签: ios swift xcode uigesturerecognizer swift-playground

最近,我开始处理我的WWDC 2018提交。在预期中,我在一个快速的游乐场文件中启动了我的项目(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

1 个答案:

答案 0 :(得分:1)

您的卡片视图使用了不正确的高度值 将高度值更改为较低值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)

另外,因为cardButtonTwoUIButton,所以您不需要使用UITapGestureRecognizer。您只需添加touchUpInside事件的目标。

override func viewDidLoad() {
        cardButtonTwo.addTarget(self, action: #selector(HomeViewController.handleTap(sender:)), for: .touchUpInside)
        self.navigationController?.isNavigationBarHidden = true
    }