关闭按钮在 Swift Playground 中不起作用

Posted

技术标签:

【中文标题】关闭按钮在 Swift Playground 中不起作用【英文标题】:Close Button Not Working in Swift Playground 【发布时间】:2018-03-30 00:35:29 【问题描述】:

我正在为 WWDC 开发一个 Swift Playground。我需要一个 UI Button 来关闭 Web 视图和工具栏(该按钮称为 closeButton,该功能位于按钮的底部),并在单击时返回上一个屏幕(第一个视图)。下面的代码是我目前输入的 Swift:

import UIKit
import WebKit
import PlaygroundSupport
import Foundation

PlaygroundPage.current.needsIndefiniteExecution = true

class MyViewController : UIViewController 
    let cardView = UIView()
    let accountButton = UIButton()
    let coverImageView = UIImageView()
    let swiftLogoView = UIImageView()
    let openLabel = UILabel()
    let titleLabel = UILabel()
    let label = UILabel()
    let captionLabel = UILabel()
    let descriptionLabel = UILabel()
    let backgroundImageView = UIImageView()
    let closeButton = UIButton()
    let openButton = UIButton()
    let doneButton = UIButton()
    let url1 = URL(string: "https://youtube.com/embed/uuxXHAKA1WY")!
    let alertController = UIAlertController(title: "Who Made This Playground:", message: "Mark Bruckert made this playground for WWDC 2018. He is a Web and Graphic Designer learning Swift and ios Design. He would love to attend this year, to learn the new frameworks revealed at the event and have IOS Engineers review his designs and code.", preferredStyle: .alert)
    let defaultAction = UIAlertAction(title: "Close", style: .default, handler: nil)
    let menuBar = UIView()
    let doneLabel = UILabel()
    let webView = UIWebView()

    let cardView2 = UIView()
    let coverImageView2 = UIImageView()
    let titleLabel2 = UILabel()
    let captionLabel2 = UILabel()
    let descriptionLabel2 = UILabel()
    let backgroundImageView2 = UIImageView()
    let url2 = URL(string: "https://youtu.be/uuxXHAKA1WY")!

    override func loadView() 
        let view = UIView()
        view.backgroundColor = .white

        label.frame = CGRect(x: 20, y: 30, width: 272, height: 38)
        label.text = "Dev Tutorials:"
        label.textColor = .black
        label.font = UIFont.systemFont(ofSize: 32, weight: .bold)

        openLabel.frame = CGRect(x: 140, y: 215, width: 272, height: 38)
        openLabel.text = "Play Video"
        openLabel.textColor = .black
        openLabel.font = UIFont.systemFont(ofSize: 32, weight: .semibold)
        self.openLabel.alpha = 0
        openLabel.layer.zPosition = 5

        doneLabel.frame = CGRect(x: 25, y: 5, width: 272, height: 38)
        doneLabel.text = "Done"
        doneLabel.textColor = .white
        doneLabel.font = UIFont.systemFont(ofSize: 32, weight: .light)
        openLabel.layer.zPosition = 7
        doneButton.addTarget(self, action: #selector(doneButtonTapped), for: .touchUpInside
        )

        cardView.frame = CGRect(x: 60, y: 100, width: 300, height: 250)
        cardView.layer.cornerRadius = 14
        cardView.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        cardView.layer.shadowOpacity = 0.25
        cardView.layer.shadowOffset = CGSize(width: 0, height: 10)
        cardView.layer.shadowRadius = 10

        cardView2.frame = CGRect(x: 60, y: 100, width: 300, height: 250)
        cardView2.layer.cornerRadius = 14
        cardView2.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        cardView2.layer.shadowOpacity = 0.25
        cardView2.layer.shadowOffset = CGSize(width: 0, height: 10)
        cardView2.layer.shadowRadius = 10

        titleLabel.frame = CGRect(x: 16, y: 16, width: 272, height: 38)
        titleLabel.text = "Portals with ARKit"
        titleLabel.textColor = .white
        titleLabel.font = UIFont.systemFont(ofSize: 32, weight: .semibold)

        captionLabel.frame = CGRect(x: 16, y: 204, width: 272, height: 40)
        captionLabel.text = "by Jared Davidson"
        captionLabel.textColor = .white
        captionLabel.numberOfLines = 0

        descriptionLabel.frame = CGRect(x: 20, y: 400, width: 335, height: 132)
        descriptionLabel.text = "In this tutorial, you will learn how to use ARKit by Apple to transport yourself through a portal."
        descriptionLabel.textColor = .black
        descriptionLabel.numberOfLines = 10
        descriptionLabel.alpha = 0

        coverImageView.frame = CGRect(x: 0, y: 0, width: 300, height: 250)
        coverImageView.contentMode = .scaleAspectFill
        coverImageView.image = #imageLiteral(resourceName: "Cover.jpg")
        coverImageView.clipsToBounds = true

        swiftLogoView.frame = CGRect(x: 8, y: 8, width: 35, height: 35)
        swiftLogoView.contentMode = .scaleAspectFill
        swiftLogoView.image = #imageLiteral(resourceName: "Swift_logo.png")
        swiftLogoView.clipsToBounds = true
        coverImageView.layer.cornerRadius = 14

        accountButton.frame = CGRect(x: 360, y: 20, width: 55, height: 55)
        accountButton.backgroundColor = #colorLiteral(red: 0.803921580314636, green: 0.803921580314636, blue: 0.803921580314636, alpha: 1.0)
        accountButton.layer.cornerRadius = 30
        accountButton.addTarget(self, action: #selector(accountButtonTapped), for: .touchUpInside)

        closeButton.frame = CGRect(x: 360, y: 20, width: 28, height: 28)
        closeButton.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.5)
        closeButton.layer.cornerRadius = 14
        closeButton.setImage(#imageLiteral(resourceName: "Action-Close@2x.png"), for: .normal)
        closeButton.addTarget(self, action: #selector(closeButtonTapped), for: .touchUpInside)
        closeButton.alpha = 0

        openButton.frame = CGRect(x: 100, y: 200, width: 220, height: 75)
        openButton.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        openButton.layer.cornerRadius = 14
        openButton.addTarget(self, action: #selector(openButtonTapped), for: .touchUpInside)
        openButton.alpha = 0

        doneButton.frame = CGRect(x: 10, y: 5, width: 130, height: 50)
        doneButton.layer.borderWidth = 3.0
        doneButton.layer.borderColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        doneButton.layer.cornerRadius = 14
        doneButton.addTarget(self, action: #selector(doneButtonTapped), for: .touchUpInside)


        cardView.addSubview(coverImageView)


        cardView.addSubview(openLabel)

        doneButton.addSubview(doneLabel)
        cardView.addSubview(closeButton)

        cardView.addSubview(openButton)

        menuBar.addSubview(doneButton)
        cardView.addSubview(descriptionLabel)

        view.addSubview(cardView)

        view.addSubview(label)

        view.addSubview(accountButton)

        let tap = UITapGestureRecognizer(target: self, action: #selector(cardViewTapped))
        cardView.addGestureRecognizer(tap)
        cardView.isUserInteractionEnabled = true

        doneButton.isUserInteractionEnabled = true

        self.view = view
    

    @objc func cardViewTapped() 
        let animator = UIViewPropertyAnimator(duration: 0.7, dampingRatio: 0.7) 
            self.cardView.frame = CGRect(x: 0, y: 0, width: 450, height: 667)

            self.label.layer.isHidden = true
            self.cardView.layer.cornerRadius = 0
            self.titleLabel.frame = CGRect(x: 20, y: 20, width: 374, height: 38)
            self.captionLabel.frame = CGRect(x: 20, y: 370, width: 272, height: 40)
            self.descriptionLabel.alpha = 1
            self.coverImageView.frame = CGRect(x: 0, y: 0, width: 450, height: 420)
            self.coverImageView.layer.cornerRadius = 0

            self.closeButton.alpha = 1

            self.openButton.alpha = 0.7

            self.accountButton.alpha = 0

            self.openLabel.alpha = 1
        
        animator.startAnimation()
    

    @objc func closeButtonTapped() 
        let animator = UIViewPropertyAnimator(duration: 0.7, dampingRatio: 0.7) 
            self.cardView.frame = CGRect(x: 60, y: 100, width: 300, height: 250)
            self.cardView.layer.cornerRadius = 14
            self.titleLabel.frame = CGRect(x: 16, y: 16, width: 272, height: 38)
            self.captionLabel.frame = CGRect(x: 16, y: 204, width: 272, height: 40)
            self.descriptionLabel.alpha = 0
            self.coverImageView.frame = CGRect(x: 0, y: 0, width: 300, height: 250)
            self.coverImageView.layer.cornerRadius = 14
            self.closeButton.alpha = 0

            self.label.layer.isHidden = false

            self.openButton.alpha = 0

            self.openLabel.alpha = 0

            self.accountButton.alpha = 1
        
        animator.startAnimation()
    

    @objc func openButtonTapped() 

        let view = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 600))
        view.backgroundColor = UIColor.lightGray
        PlaygroundPage.current.liveView = view

        menuBar.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(menuBar)
        menuBar.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        menuBar.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        menuBar.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        menuBar.heightAnchor.constraint(equalToConstant: 64.0).isActive = true
        menuBar.backgroundColor = #colorLiteral(red: 0.803921580314636, green: 0.803921580314636, blue: 0.803921580314636, alpha: 1.0)

        let toolbar = UIView()
        toolbar.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(toolbar)
        toolbar.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        toolbar.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        toolbar.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        toolbar.heightAnchor.constraint(equalToConstant: 64.0).isActive = true
        toolbar.backgroundColor = #colorLiteral(red: 0.803921580314636, green: 0.803921580314636, blue: 0.803921580314636, alpha: 1.0)

        webView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(webView)
        webView.topAnchor.constraint(equalTo: menuBar.bottomAnchor, constant: 8.0).isActive = true
        webView.bottomAnchor.constraint(equalTo: toolbar.topAnchor, constant: -8.0).isActive = true
        webView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        webView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        webView.loadRequest(URLRequest(url: URL(string:"https://youtube.com/embed/uuxXHAKA1WY")!))
    

    @objc func accountButtonTapped() 
        present(alertController, animated: true, completion: nil)
        alertController.addAction(defaultAction)
    

    @objc func doneButtonTapped() 
        self.webView.alpha = 0
        self.view.alpha = 0
    

// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

提前感谢您的帮助, 马克 B。

【问题讨论】:

上面的代码能用吗?我把它贴在我的操场上,它没有运行 @Harish 对您不起作用的原因是您在 Mark(OP)拥有的资源文件夹中没有 .png 文件。 @MichaelDautermann 感谢您为我回答这个问题……这就是代码没有为他运行的原因。 【参考方案1】:

在方法 openButtonTapped 中,局部变量 view 给您的代码带来了问题。另外,您又调用了 liveView。我已经修改了您的代码,假设最后一个屏幕将转到上一个屏幕。

首先,将 toolBar 声明为成员变量,如 menuBar。然后是以下代码。点击完成按钮将转到上一个。

 @objc func openButtonTapped() 
    view.backgroundColor = UIColor.cyan
    menuBar.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(menuBar)
    menuBar.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    menuBar.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    menuBar.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    menuBar.heightAnchor.constraint(equalToConstant: 64.0).isActive = true
    menuBar.backgroundColor = #colorLiteral(red: 0.803921580314636, green: 0.803921580314636, blue: 0.803921580314636, alpha: 1.0)

    toolbar.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(toolbar)
    toolbar.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    toolbar.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    toolbar.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    toolbar.heightAnchor.constraint(equalToConstant: 64.0).isActive = true
    toolbar.backgroundColor = #colorLiteral(red: 0.803921580314636, green: 0.803921580314636, blue: 0.803921580314636, alpha: 1.0)


    webView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(webView)
    webView.topAnchor.constraint(equalTo: menuBar.bottomAnchor, constant: 8.0).isActive = true
    webView.bottomAnchor.constraint(equalTo: toolbar.topAnchor, constant: -8.0).isActive = true
    webView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    webView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    webView.loadRequest(URLRequest(url: URL(string:"https://youtube.com/embed/uuxXHAKA1WY")!))


@objc func accountButtonTapped() 
    present(alertController, animated: true, completion: nil)
    alertController.addAction(defaultAction)


@objc func doneButtonTapped() 
    print("test")
    self.webView.stopLoading()
    self.webView.alpha = 0
    toolbar.removeFromSuperview()
    menuBar.removeFromSuperview()

【讨论】:

非常感谢...您是救生员!这些更改解决了我遇到的问题。 @user9550112,不客气,请接受我的回答。

以上是关于关闭按钮在 Swift Playground 中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

如何引用 Swift Playground 本身?

使用 Swift Playground 无法在助手编辑器中查看控制台输出

如何制作多页 Swift Playground

如何在 Swift 的操场上接受用户输入? [关闭]

有没有办法在不重新启动 Xcode 6 的情况下打开 Swift Playground?

Swift:按钮在 UIScrollView 中不起作用