使用xib自定义uiview创建的Tapgesture和触摸事件不起作用?

时间:2016-06-15 05:55:32

标签: ios view swift2 uitapgesturerecognizer

我试图用弹出的自定义UIView显示一些内容,到目前为止我做得很好,我想通过触摸弹出视图释放弹出窗口,我无法将其视为成功,我是新手快速语言,请你帮我解决这个问题,所有代码仅从谷歌复制,

自定义代码UIView

import UIKit

class PopUpView: UIView {
    var view = UIView()
    var _myTap: UITapGestureRecognizer?
    // this name has to match your class file and your xib file
    @IBOutlet weak var lblContent: UILabel!

    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }
    override func awakeFromNib()
    {
        self.userInteractionEnabled = true
    }
    func _myHandleTap(sender: UITapGestureRecognizer) {
        if sender.state == .Ended {
            print("_myHandleTap(sender.state == .Ended)")
            sender.view!.backgroundColor
                = UIColor(red: CGFloat(drand48()), green: CGFloat(drand48()), blue: CGFloat(drand48()), alpha: 1.0);
            self .hideInView()
        }
    }
    func setup() {
        // setup the view from .xib
        view = loadViewFromNib()
        view .userInteractionEnabled = true
        self .userInteractionEnabled = true
        view.backgroundColor = UIColor.clearColor()
        _myTap = UITapGestureRecognizer(target: self
            , action: #selector(_myHandleTap(_:)))
        _myTap!.numberOfTapsRequired = 1
        view.addGestureRecognizer(_myTap!)

    }

    func loadViewFromNib() -> UIView {
        // grabs the appropriate bundle
        let bundle = NSBundle(forClass: self.dynamicType)
        let nib = UINib(nibName: "PopUpView", bundle: bundle)
        let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
        return view
    }
    func showInView(currentView:UIView,content:String) {
        view.frame = currentView.bounds
        currentView .userInteractionEnabled = true
        lblContent.text = content
        UIApplication.sharedApplication().keyWindow!.addSubview(view)
        view.alpha = 0
        UIView.animateWithDuration(0.2, delay: 0.2, options: [.CurveEaseInOut], animations: {
            self.view.alpha = 1
            }, completion: nil)

    }
    func hideInView() {
        view.alpha = 0
        UIView.animateWithDuration(0.2, delay: 0.2, options: [.Repeat, .CurveEaseInOut], animations: {
            self.view.alpha = 0
            self.view .removeFromSuperview()
            }, completion: nil)

    }
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self .hideInView()
    }
    func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {

        return true

    }
}

从我的UIViewController

访问它
@IBAction func doForgotAction(sender: AnyObject) {
//        warning: could not load any Objective-C class information. This will significantly reduce the quality of type information available.
        let  custom = PopUpView()
        custom .showInView(self .view, content: "welcome")

    }

2 个答案:

答案 0 :(得分:1)

最后我得到了答案, 主要问题是没有收到事件,无论是由xib还是以编程方式创建的

我已将所有代码更改为

import UIKit

class PopUpView: UIView {
    var view = UIView()
    // this name has to match your class file and your xib file
    @IBOutlet weak var containerView: UIView!
    @IBOutlet weak var lblContent: UILabel!
    @IBOutlet weak var _myTap: UITapGestureRecognizer!
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }
    override func awakeFromNib()
    {
        super.awakeFromNib()
    }
    @IBAction func tapGestureAction(sender: AnyObject) {
         self .hideInView()
    }
    func setup() {
        // setup the view from .xib
        view = loadViewFromNib()
        view.frame = bounds
        self .userInteractionEnabled = true
        view.backgroundColor = UIColor.greenColor()
        view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
        addSubview(view)
    }

    func loadViewFromNib() -> UIView {
        // grabs the appropriate bundle
        let bundle = NSBundle(forClass: self.dynamicType)
        let nib = UINib(nibName: "PopUpView", bundle: bundle)
        let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
        return view
    }
    func showInView(currentView:UIView,content:String) {
        self.frame = currentView.bounds
        self .layoutIfNeeded()
        lblContent.text = content
        UIApplication.sharedApplication().keyWindow!.addSubview(self)
        self.alpha = 0
        UIView.animateWithDuration(0.2, delay: 0.2, options: [.CurveEaseInOut], animations: {
            self.alpha = 1
            }, completion: nil)

    }
    func hideInView() {
        self.alpha = 1
        UIView.animateWithDuration(0.2, delay: 0.2, options: [.Repeat, .CurveEaseInOut], animations: {
            self.alpha = 0
            self .removeFromSuperview()
            }, completion: nil)

    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self .hideInView()
    }
    func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {

        return true

    }
}

更改此后续行后,所有工作正常

func setup() {
        // setup the view from .xib
        view = loadViewFromNib()
        view.frame = bounds
        self .userInteractionEnabled = true
        view.backgroundColor = UIColor.greenColor()
        view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
        addSubview(view)

}

现在我可以获得tapgesture和touchevents。

答案 1 :(得分:0)

您必须在自定义视图类中创建插座操作方法,并在此课程中触摸并点按手势。现在,为了获得这些触摸和点击的效果,您必须创建一个委托并将委托方法添加到视图控制器,以便接收触摸和点击。