Swift - 没有看到我从另一个自定义视图类调用的自定义视图类

时间:2016-12-11 01:07:28

标签: ios swift uiview

只是尝试关注一些youtube教程并了解自定义视图。我相信我在这里遗漏了一些非常基本的东西。在这里和网上看了几个小时后,我放弃了。

我有一个自定义视图,可以滑出来。我打算根据需要填写不同的自定义视图。 slideOutView显示正常。但是,我无法在其中看到名为FlashCardView的其他自定义视图。我从SlideOutView调用FlashCardView。

感谢您的帮助!

FlashCardView类的代码:

import UIKit

protocol FlashCardViewDelegate: class {

}

class FlashCardView: UIView {

    weak var delegate: FlashCardViewDelegate?
    let questionLabel = UILabel()
    let scrollView = UIScrollView()
    let dummyView = UIView()


    //MARK: Initialization

    override init(frame: CGRect) {

        super.init(frame: frame)
        self.backgroundColor = UIColor.black
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

SlideOutView类的代码:

import UIKit

class SlideOutView: UIView {

    let slideOutView = UIView()
    let menuBarLine = UIView()
    let menuBarView = UIView()
    let menuButton = UIButton()
    let backButton = UIButton()
    let flashCardView = FlashCardView()

    let ratio = DeviceSpecificValues()

    override init(frame: CGRect) {

        super.init(frame: frame)
        self.addView()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func addView() {

        if let window = UIApplication.shared.keyWindow {

            slideOutView.frame = CGRect(x: 0, y: window.bounds.height, width: window.bounds.width, height: window.bounds.height / 2)
            print("slideoutview frame is: \(slideOutView.frame)")
            slideOutView.backgroundColor = UIColor.white
            window.addSubview(slideOutView)

            menuBarView.frame = CGRect(x: 0, y: 0, width: slideOutView.bounds.width, height: window.bounds.height / ratio.menuBarRatio)
            menuBarView.backgroundColor = GlobalValues.menuBarViewColor
            slideOutView.addSubview(menuBarView)

            menuBarLine.frame = CGRect(x: 0, y: 0, width: menuBarView.bounds.width, height: GlobalValues.menuBarLineWidth)
            menuBarLine.backgroundColor = GlobalValues.menuTextColor
            menuBarView.addSubview(menuBarLine)

            backButton.frame = CGRect(x: 0, y: 0, width: menuBarView.bounds.width * 0.1, height: menuBarView.bounds.height * 0.9)
            backButton.center.y = menuBarView.bounds.height / 2 + GlobalValues.menuBarLineWidth
            backButton.setTitle("<Back", for: .normal)
            backButton.titleLabel?.adjustsFontSizeToFitWidth = true
            backButton.setTitleColor(GlobalValues.menuTextColor, for: UIControlState())
            backButton.showsTouchWhenHighlighted = true
            menuBarView.addSubview(backButton)

            menuButton.frame = CGRect(x: menuBarView.bounds.width * 0.9, y: 0, width: menuBarView.bounds.width * 0.1, height: menuBarView.bounds.height * 0.9)
            menuButton.center.y = menuBarView.bounds.height / 2 + GlobalValues.menuBarLineWidth
            menuButton.setTitle("Menu", for: .normal)
            menuButton.titleLabel?.adjustsFontSizeToFitWidth = true
            menuButton.setTitleColor(GlobalValues.menuTextColor, for: UIControlState())
            menuButton.showsTouchWhenHighlighted = true
            menuBarView.addSubview(menuButton)
            addFlashCardView()
        }
    }

    func slideIn() {

        UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveLinear, animations: {

            if let window = UIApplication.shared.keyWindow {

                self.slideOutView.frame = CGRect(x: 0, y: window.bounds.height, width: window.bounds.width, height: window.bounds.height / 2)
            }
        }, completion: nil)
    }

    func slideOut() {

        UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveLinear, animations: {

            if let window = UIApplication.shared.keyWindow {

                self.slideOutView.frame = CGRect(x: 0, y: window.bounds.height / 2, width: window.bounds.width, height: window.bounds.height / 2)
            }
        }, completion: nil)
    }

    func addFlashCardView() {

        flashCardView.frame = slideOutView.frame
        slideOutView.addSubview(flashCardView)
    }
}

0 个答案:

没有答案