2016-06-15 30 views
0

我试图用自定义UIView显示一些内容弹出,到目前为止我做得很好,我想通过触摸弹出视图来释放弹出窗口,我无法将其视为成功,我对新语言很陌生,可以请你帮我这个,所有的代码只能从谷歌只复制,用xib自定义uiview创建的tapgesture和touch事件不起作用吗?

代码对于自定义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") 

    } 
+0

是你的函数_myHandleTap被调用吗? –

+0

那不叫我已经检查了断点。 –

回答

1

最后我得到了我的答案, 的主要问题是事件不会被接受,无论是其由厦门国际银行建立或编程

我已经改变了所有的代码下面

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。

0

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

+0

为什么它不能以编程方式创建? –