Xcode 9 Swift 4 Playgrounds UIGestureRecognizer 不工作

Posted

技术标签:

【中文标题】Xcode 9 Swift 4 Playgrounds UIGestureRecognizer 不工作【英文标题】:Xcode 9 Swift 4 Playgrounds UIGestureRecognizer not working 【发布时间】:2018-01-06 17:35:44 【问题描述】:

最近,我开始着手 WWDC 2018 提交。出于预期,我在一个 swift playground 文件中开始了我的项目(Apple 通常不接受应用程序提交)。大约两个月前,我将 Xcode 更新到了 9.2 版(9C40b)。这样做之后,我意识到我的手势识别器并没有在我的实时视图上工作。一切都在实时视图助手窗口上完美呈现和显示,但我的手势识别器根本没有响应。有没有人遇到过类似的问题,有没有人能够在 Xcode 9 中使用 Swift 4 的 Swift Playgrounds(我在下面附上了我的代码以供参考)?

//: 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

【问题讨论】:

我不知道,为什么你使用了太多代码?您也可以通过设计来实现这一点。 @KhawarIslam 我正在考虑使用情节提要,但我无法正确编译它以用于 swift playground。 您使用了 StoryBard。如果您卡住了,请提出问题。 @KhawarIslam 那仍然没有回答我的问题。即便如此,在 Swift Playgrounds 文件中使用情节提要并不是设计 UI 原型的最有效方式。 创建一个 Xcode 项目而不是游乐场。这是制作原型的最佳方式。 【参考方案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
    

【讨论】:

@KawaiiFX:编辑了答案。您只需要更改视图的高度。另外,由于您使用的是按钮,因此无需添加 TapGestureRecognizer。

以上是关于Xcode 9 Swift 4 Playgrounds UIGestureRecognizer 不工作的主要内容,如果未能解决你的问题,请参考以下文章

Xcode 9:使用 Swift 3.1 编译的模块无法在 Swift 4.0 中导入

Xcode 9 Swift 4 Playgrounds UIGestureRecognizer 不工作

如何在 Xcode 9.3 中切换到 Swift 4.0?

Xcode 9.3(Swift 4.1)中的 Codable '没有初始化器'

没有这样的模块 'GoogleSignIn' Xcode 9.3 和 Swift 4.1

XCode 9 Swift 4 中没有这样的模块“FBAudienceNetwork”