添加子视图(ShareView)但触摸其上的按钮时没有任何反应

时间:2016-09-21 15:29:38

标签: ios swift events uiview touch

添加子视图(ShareView)但如果我shareVC.addsubView(self)

中的func showShareView (),则触摸按钮(coverBtn)时没有任何反应

但如果单独superView.addSubview(coverBtn)superView.addSubview(sharePanel),一切都会好的。

import UIKit

class ShareView: UIView {

    weak var shareVC: UINavigationController?


    // UI
    private lazy var coverView: UIView! = {
        let coverView = UIView(frame: UIScreen.mainScreen().bounds)
        return coverView
    }()

    // transluscent cover
    private lazy var coverBtn: UIButton! = {

        let bounds = UIScreen.mainScreen().bounds
        let frame = CGRectMake(0, 0, bounds.width, bounds.height)
        let coverBtn = UIButton(frame: frame)
        print ("UIScreen.mainScreen().bounds = \(UIScreen.mainScreen().bounds)")
        coverBtn.alpha = 0.2
        coverBtn.backgroundColor = UIColor.blackColor()
        coverBtn.addTarget(self, action: #selector(ShareView.pressCoverBtn), forControlEvents: .TouchUpInside)
        return coverBtn
    }()
    let panelHeight: CGFloat = 215
    // share panel
    private lazy var sharePanel: UIView! = {

        // panel size
        let bounds = UIScreen.mainScreen().bounds
        let h = 215
        let frame = CGRectMake(0, bounds.height, bounds.width, self.panelHeight)
        let sharePanel: UIView = UIView(frame: frame)
        sharePanel.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1.0)

        // label
        let labelHeight: CGFloat = 30
        let labelWidth: CGFloat = 100
        let labelY: CGFloat = 20
        let labelFrame = CGRectMake(sharePanel.frame.width/2-labelWidth/2, labelY, labelWidth, labelHeight)
        let label  = UILabel(frame: labelFrame)
        label.text = "分享到"
        label.textAlignment = .Center
        label.backgroundColor = UIColor.whiteColor()
        sharePanel.addSubview(label)


        // share buttons
        let marginW: CGFloat = 80
        let btnInv: CGFloat = 20
        let btnCnt: CGFloat = 3
        let btnsY = label.frame.maxY + 15
        let btnA = (sharePanel.frame.width - 2*marginW - (btnCnt-1)*btnInv)/btnCnt
        let wcFrame = CGRectMake(marginW, btnsY, btnA, btnA)
        let pyqFrame = CGRectMake(wcFrame.maxX+btnInv, btnsY, btnA, btnA)
        let wbFrame = CGRectMake(pyqFrame.maxX+btnInv, btnsY, btnA, btnA)

        let wcBtn = UIButton(frame: wcFrame)
        let pyqBtn = UIButton(frame: pyqFrame)
        let wbBtn = UIButton(frame: wbFrame)

        wcBtn.setImage(UIImage(named: "share_wx"), forState: .Normal)
        pyqBtn.setImage(UIImage(named: "share_pyq"), forState: .Normal)
        wbBtn.setImage(UIImage(named: "share_wb"), forState: .Normal)

        sharePanel.addSubview(wcBtn)
        sharePanel.addSubview(pyqBtn)
        sharePanel.addSubview(wbBtn)

        // cancel button
        let ccW = sharePanel.frame.width/2
        let ccH: CGFloat = 50
        let ccFrame = CGRectMake(sharePanel.frame.width/2-ccW/2, wcBtn.frame.maxY+10, ccW, ccH)
        let cancelBtn: UIButton = UIButton(frame: ccFrame)
        cancelBtn.setBackgroundImage(UIImage(named: "kkk"), forState: .Normal)
        cancelBtn.setTitle("取消", forState: .Normal)
        cancelBtn.setTitleColor(UIColor.blackColor(), forState: .Normal)
        cancelBtn.addTarget(self, action: #selector(ShareView.pressCoverBtn), forControlEvents: UIControlEvents.TouchUpInside)
        sharePanel.addSubview(cancelBtn)

        return sharePanel
    }()




    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */

//    override init(){
//        super.init()
//    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addCustomView(self)
    }

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

    func addCustomView (superView: UIView) {
        superView.addSubview(coverBtn)
        superView.addSubview(sharePanel)
    }

    @objc func pressCoverBtn() {
        print("press cover btn\n")
        hideShareView()
    }

    func showShareView () {
        self.addCustomView((shareVC?.view)!)
        UIView.animateWithDuration(0.4, animations: {
            () -> Void in
            self.sharePanel.frame = CGRectMake(0, UIScreen.mainScreen().bounds.height - self.panelHeight, self.sharePanel.frame.width, self.panelHeight)
            })

    }
    func hideShareView() {
        coverBtn.removeFromSuperview()
        UIView.animateWithDuration(0.4, animations: {
            () -> Void in
            self.sharePanel.frame  = CGRectMake(0, UIScreen.mainScreen().bounds.height, self.sharePanel.frame.width, self.panelHeight)
            }) { (finish)-> Void in
                self.removeFromSuperview()
        }


    }

}

1 个答案:

答案 0 :(得分:0)

您可以查看“调试视图层次结构”'专注于图层,以便您可以看到是否有按钮阻止它的视图 '调试视图层次结构'将为您提供每个图层的三维视图 它位于底栏

this image

此外,您要将这两个视图添加到超级视图,尝试添加' coverBtn'按钮到' sharePanel'查看然后将视图添加到超级视图,这将在' sharePanel'之上创建按钮层。图